2017-08-22 10:53:26 +03:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import {computed} from '@ember/object';
|
|
|
|
import {isEmpty} from '@ember/utils';
|
|
|
|
import {notEmpty} from '@ember/object/computed';
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
/**
|
|
|
|
* Renders one random error message when passed a DS.Errors object
|
|
|
|
* and a property name. The message will be one of the ones associated with
|
|
|
|
* that specific property. If there are no errors associated with the property,
|
|
|
|
* nothing will be rendered.
|
|
|
|
* @param {DS.Errors} errors The DS.Errors object
|
|
|
|
* @param {string} property The property name
|
|
|
|
*/
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Component.extend({
|
2015-06-07 06:19:19 +03:00
|
|
|
tagName: 'p',
|
|
|
|
classNames: ['response'],
|
|
|
|
|
|
|
|
errors: null,
|
|
|
|
property: '',
|
|
|
|
|
2016-06-30 21:14:25 +03:00
|
|
|
isVisible: notEmpty('errors'),
|
2015-06-07 06:19:19 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
message: computed('errors.[]', 'property', function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let property = this.property;
|
|
|
|
let errors = this.errors;
|
2015-10-28 14:36:45 +03:00
|
|
|
let messages = [];
|
|
|
|
let index;
|
2015-06-07 06:19:19 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
if (!isEmpty(errors) && errors.get(property)) {
|
|
|
|
errors.get(property).forEach((error) => {
|
2015-06-07 06:19:19 +03:00
|
|
|
messages.push(error);
|
|
|
|
});
|
|
|
|
index = Math.floor(Math.random() * messages.length);
|
|
|
|
return messages[index].message;
|
|
|
|
}
|
2019-06-24 18:33:21 +03:00
|
|
|
|
|
|
|
return '';
|
2015-06-07 06:19:19 +03:00
|
|
|
})
|
|
|
|
});
|