1
0
Fork 0
mirror of https://github.com/TryGhost/Ghost-Admin.git synced 2023-12-14 02:33:04 +01:00
Ghost-Admin/app/components/modal-new-subscriber.js
Kevin Ansfield ddc7b857b5
Bump ember-ajax dependency (#902)
no issue
- upgrade `ember-ajax` to 3.0.0
- `ember-ajax` [now passes the payload through directly](https://github.com/ember-cli/ember-ajax/releases/tag/v3.0.0) rather than trying to normalize it so all our error handling needed to be updated
2017-11-03 22:59:39 +00:00

44 lines
1.4 KiB
JavaScript

import ModalComponent from 'ghost-admin/components/modal-base';
import {A as emberA} from '@ember/array';
import {isInvalidError} from 'ember-ajax/errors';
import {task} from 'ember-concurrency';
export default ModalComponent.extend({
addSubscriber: task(function* () {
try {
yield this.get('confirm')();
this.send('closeModal');
} catch (error) {
// TODO: server-side validation errors should be serialized
// properly so that errors are added to model.errors automatically
if (error && isInvalidError(error)) {
let [firstError] = error.payload.errors;
let {message} = firstError;
if (message && message.match(/email/i)) {
this.get('model.errors').add('email', message);
this.get('model.hasValidated').pushObject('email');
return;
}
}
// route action so it should bubble up to the global error handler
if (error) {
throw error;
}
}
}).drop(),
actions: {
updateEmail(newEmail) {
this.set('model.email', newEmail);
this.set('model.hasValidated', emberA());
this.get('model.errors').clear();
},
confirm() {
this.get('addSubscriber').perform();
}
}
});