Ghost/ghost/admin/app/components/modals/new-subscriber.js
Kevin Ansfield f1512d12c2 success/failure state spinner buttons (#566)
refs https://github.com/TryGhost/Ghost/issues/7515
- changes to `gh-task-button`:
  - can take `buttonText` (default: "Save"), `runningText` ("Saving"), `successText` ("Saved"), and `failureText` ("Retry") params
  - positional param for `buttonText`
  - default button display can be overridden by passing in a block, in that scenario the component will yield a hash containing all states to be used in this fashion:
    ```
    {{#gh-task-button task=myTask as |task|}}
    {{if task.isIdle "Save me"}}
    {{if task.isRunning "Saving"}}
    {{if task.isSuccess "Thank you!"}}
    {{if task.isFailure "Nooooooo!"}}
    {{/gh-task-button}}
    ```
- update existing uses of `gh-task-button` to match new component signature
2017-03-07 10:28:52 -07:00

45 lines
1.4 KiB
JavaScript

import {A as emberA} from 'ember-array/utils';
import ModalComponent from 'ghost-admin/components/modals/base';
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.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();
}
}
});