2016-06-30 13:21:47 +03:00
|
|
|
import {A as emberA} from 'ember-array/utils';
|
2016-05-24 15:06:59 +03:00
|
|
|
import ModalComponent from 'ghost-admin/components/modals/base';
|
2016-09-26 19:59:04 +03:00
|
|
|
import {isInvalidError} from 'ember-ajax/errors';
|
2017-01-19 14:40:31 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2016-04-15 17:45:50 +03:00
|
|
|
|
|
|
|
export default ModalComponent.extend({
|
2017-01-19 14:40:31 +03:00
|
|
|
|
|
|
|
addSubscriber: task(function* () {
|
|
|
|
try {
|
|
|
|
yield this.get('confirm')();
|
|
|
|
this.send('closeModal');
|
|
|
|
} catch (error) {
|
|
|
|
// TODO: server-side validation errors should be serialized
|
2017-03-07 20:28:52 +03:00
|
|
|
// properly so that errors are added to model.errors automatically
|
2017-01-19 14:40:31 +03:00
|
|
|
if (error && isInvalidError(error)) {
|
|
|
|
let [firstError] = error.errors;
|
|
|
|
let {message} = firstError;
|
|
|
|
|
|
|
|
if (message && message.match(/email/i)) {
|
|
|
|
this.get('model.errors').add('email', message);
|
|
|
|
this.get('model.hasValidated').pushObject('email');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 20:28:52 +03:00
|
|
|
// route action so it should bubble up to the global error handler
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
2017-01-19 14:40:31 +03:00
|
|
|
}
|
|
|
|
}).drop(),
|
|
|
|
|
2016-04-15 17:45:50 +03:00
|
|
|
actions: {
|
|
|
|
updateEmail(newEmail) {
|
|
|
|
this.set('model.email', newEmail);
|
2016-06-11 19:52:36 +03:00
|
|
|
this.set('model.hasValidated', emberA());
|
2016-04-15 17:45:50 +03:00
|
|
|
this.get('model.errors').clear();
|
|
|
|
},
|
|
|
|
|
|
|
|
confirm() {
|
2017-01-19 14:40:31 +03:00
|
|
|
this.get('addSubscriber').perform();
|
2016-04-15 17:45:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|