2018-10-18 02:18:29 +03:00
|
|
|
import ModalComponent from 'ghost-admin/components/modal-base';
|
|
|
|
import {alias} from '@ember/object/computed';
|
|
|
|
import {A as emberA} from '@ember/array';
|
2021-03-04 23:49:15 +03:00
|
|
|
import {isHostLimitError} from 'ghost-admin/services/ajax';
|
2018-10-18 02:18:29 +03:00
|
|
|
import {isInvalidError} from 'ember-ajax/errors';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
import {task} from 'ember-concurrency';
|
|
|
|
|
|
|
|
export default ModalComponent.extend({
|
|
|
|
router: service(),
|
|
|
|
|
2021-03-04 23:49:15 +03:00
|
|
|
errorMessage: null,
|
|
|
|
|
2018-10-18 02:18:29 +03:00
|
|
|
confirm() {},
|
|
|
|
|
|
|
|
integration: alias('model'),
|
|
|
|
actions: {
|
|
|
|
updateName(name) {
|
|
|
|
this.integration.set('name', name);
|
|
|
|
this.integration.set('hasValidated', emberA());
|
|
|
|
this.integration.errors.clear();
|
|
|
|
},
|
|
|
|
|
|
|
|
confirm() {
|
|
|
|
return this.createIntegration.perform();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
createIntegration: task(function* () {
|
|
|
|
try {
|
|
|
|
let integration = yield this.confirm();
|
2021-01-28 18:25:21 +03:00
|
|
|
this.router.transitionTo('integration', integration);
|
2018-10-18 02:18:29 +03:00
|
|
|
} 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(/name/i)) {
|
|
|
|
this.get('integration.errors').add('name', message);
|
|
|
|
this.get('integration.hasValidated').pushObject('name');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 23:49:15 +03:00
|
|
|
if (isHostLimitError(error)) {
|
|
|
|
this.set('errorMessage', error.payload.errors[0].context);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-18 02:18:29 +03:00
|
|
|
// bubble up to the global error handler
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).drop()
|
|
|
|
});
|