This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
modal-webhook-form.js
79 lines (65 loc) · 2.31 KB
/
modal-webhook-form.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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 {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
export default ModalComponent.extend({
config: service(),
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 && error.payload && error.payload.errors) {
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', `Error: ${message}`);
}
});
return;
}
// bubble up to the global error handler
if (error) {
throw error;
}
}
})
});