Ghost/ghost/admin/app/controllers/settings/apps/slack.js
Kevin Ansfield b4cdc85a59 "400 Version Mismatch" error handling
refs https://github.com/TryGhost/Ghost/issues/6949

Handle version mismatch errors by:
- displaying an alert asking the user to copy any data and refresh
- disabling navigation so that unsaved data is not accidentally lost

Detailed changes:
- add `error` action to application route for global route-based error handling
- remove 404-handler mixin, move logic into app route error handler
- update `.catch` in validation-engine so that promises are rejected with the
  original error objects
- add `VersionMismatchError` and `isVersionMismatchError` to ajax service
- add `upgrade-status` service
  - has a method to trigger the alert and toggle the "upgrade required" mode
  - is injected into all routes by default so that it can be checked before
    transitioning
- add `Route` override
  - updates the `willTransition` hook to check the `upgrade-status` service
    and abort the transition if we're in "upgrade required" mode
- update notifications `showAPIError` method to handle version mismatch errors
- update any areas where we were catching ajax errors manually so that the
  version mismatch error handling is obeyed
- fix redirect tests in editor acceptance test
- fix mirage's handling of 404s for unknown posts in get post requests
- adjust alert z-index to to appear above modal backgrounds
2016-07-08 14:56:26 +01:00

75 lines
2.2 KiB
JavaScript

import Controller from 'ember-controller';
import {empty} from 'ember-computed';
import injectService from 'ember-service/inject';
import {invoke} from 'ember-invoke-action';
export default Controller.extend({
ghostPaths: injectService(),
ajax: injectService(),
notifications: injectService(),
// will be set by route
settings: null,
isSaving: false,
savePromise: null,
isSendingTest: false,
testNotificationDisabled: empty('model.url'),
actions: {
sendTestNotification() {
let notifications = this.get('notifications');
let slackApi = this.get('ghostPaths.url').api('slack', 'test');
if (this.get('isSendingTest')) {
return;
}
this.set('isSendingTest', true);
invoke(this, 'save').then(() => {
this.get('ajax').post(slackApi).then(() => {
notifications.showAlert('Check your slack channel test message.', {type: 'info', key: 'slack-test.send.success'});
}).catch((error) => {
notifications.showAPIError(error, {key: 'slack-test:send'});
throw error;
});
}).catch(() => {
// noop - error already handled in .save
}).finally(() => {
this.set('isSendingTest', false);
});
},
updateURL(value) {
this.set('model.url', value);
this.get('model.errors').clear();
},
save() {
let slack = this.get('model');
let settings = this.get('settings');
if (this.get('isSaving')) {
return;
}
return slack.validate().then(() => {
settings.get('slack').clear().pushObject(slack);
this.set('isSaving', true);
return settings.save().catch((err) => {
if (err && err.isAdapterError) {
this.get('notifications').showAPIError(err);
}
throw err;
}).finally(() => {
this.set('isSaving', false);
});
});
}
}
});