mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-28 22:43:30 +03:00
296e35493d
requires https://github.com/TryGhost/Ghost/pull/10033 - added `settings.integration.webhooks.edit` route - `/integrations/:integration_id/webhooks/:webhook_id` - added error handling to the webhook form modal that copes with the actual errors we get back from the server - added `event-name` helper to display humanised event names in the webhooks list - added delete button to the webhooks list and associated confirmation modal
80 lines
2.3 KiB
JavaScript
80 lines
2.3 KiB
JavaScript
import ModalComponent from 'ghost-admin/components/modal-base';
|
|
import Webhook from 'ghost-admin/models/webhook';
|
|
import {AVAILABLE_EVENTS} from 'ghost-admin/helpers/event-name';
|
|
import {alias} from '@ember/object/computed';
|
|
import {camelize} from '@ember/string';
|
|
import {isInvalidError} from 'ember-ajax/errors';
|
|
import {inject as service} from '@ember/service';
|
|
import {task} from 'ember-concurrency';
|
|
|
|
export default ModalComponent.extend({
|
|
router: service(),
|
|
|
|
availableEvents: null,
|
|
error: null,
|
|
buttonText: 'Save',
|
|
successText: 'Saved',
|
|
|
|
confirm() {},
|
|
|
|
webhook: alias('model'),
|
|
|
|
init() {
|
|
this._super(...arguments);
|
|
this.availableEvents = AVAILABLE_EVENTS;
|
|
},
|
|
|
|
didReceiveAttrs() {
|
|
if (this.webhook.isNew) {
|
|
this.set('buttonText', 'Create');
|
|
this.set('successText', 'Created');
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
selectEvent(value) {
|
|
this.webhook.set('event', value);
|
|
this.webhook.validate({property: 'event'});
|
|
},
|
|
|
|
confirm() {
|
|
this.saveWebhook.perform();
|
|
}
|
|
},
|
|
|
|
saveWebhook: task(function* () {
|
|
this.set('error', null);
|
|
|
|
try {
|
|
let webhook = yield this.confirm();
|
|
let integration = yield webhook.get('integration');
|
|
this.router.transitionTo('settings.integration', integration);
|
|
} 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 attrs = Array.from(Webhook.attributes.keys());
|
|
|
|
error.payload.errors.forEach((error) => {
|
|
let {message, property} = error;
|
|
property = camelize(property);
|
|
|
|
if (property && attrs.includes(property)) {
|
|
this.webhook.errors.add(property, message);
|
|
this.webhook.hasValidated.pushObject(property);
|
|
} else {
|
|
this.set('error', message);
|
|
}
|
|
});
|
|
|
|
return;
|
|
}
|
|
|
|
// bubble up to the global error handler
|
|
if (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
})
|
|
});
|