2018-01-11 01:57:43 +03:00
|
|
|
/* eslint-disable ghost/ember/alias-model-in-controller */
|
2017-08-22 10:53:26 +03:00
|
|
|
import Controller from '@ember/controller';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2020-05-11 13:37:35 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2017-08-02 10:05:59 +03:00
|
|
|
|
|
|
|
export default Controller.extend({
|
2017-10-30 12:38:01 +03:00
|
|
|
notifications: service(),
|
|
|
|
settings: service(),
|
2017-08-02 10:05:59 +03:00
|
|
|
|
2017-10-31 18:27:25 +03:00
|
|
|
leaveSettingsTransition: null,
|
2017-08-02 10:05:59 +03:00
|
|
|
|
|
|
|
actions: {
|
2021-02-18 03:50:27 +03:00
|
|
|
update(value) {
|
|
|
|
this.settings.set('unsplash', value);
|
2017-08-02 10:05:59 +03:00
|
|
|
},
|
|
|
|
|
2021-02-18 03:50:27 +03:00
|
|
|
save() {
|
|
|
|
this.save.perform();
|
2017-10-31 18:27:25 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
toggleLeaveSettingsModal(transition) {
|
2019-03-06 16:53:54 +03:00
|
|
|
let leaveTransition = this.leaveSettingsTransition;
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
if (!transition && this.showLeaveSettingsModal) {
|
2017-10-31 18:27:25 +03:00
|
|
|
this.set('leaveSettingsTransition', null);
|
|
|
|
this.set('showLeaveSettingsModal', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!leaveTransition || transition.targetName === leaveTransition.targetName) {
|
|
|
|
this.set('leaveSettingsTransition', transition);
|
|
|
|
|
|
|
|
// if a save is running, wait for it to finish then transition
|
2020-05-11 13:37:35 +03:00
|
|
|
if (this.save.isRunning) {
|
|
|
|
return this.save.last.then(() => {
|
2017-10-31 18:27:25 +03:00
|
|
|
transition.retry();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
this.set('showLeaveSettingsModal', true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
leaveSettings() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let transition = this.leaveSettingsTransition;
|
2021-02-18 03:50:27 +03:00
|
|
|
let settings = this.settings;
|
2017-10-31 18:27:25 +03:00
|
|
|
|
|
|
|
if (!transition) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAlert('Sorry, there was an error in the application. Please let the Ghost team know what happened.', {type: 'error'});
|
2017-10-31 18:27:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-18 03:50:27 +03:00
|
|
|
// roll back changes on settings model
|
|
|
|
settings.rollbackAttributes();
|
2017-10-31 18:27:25 +03:00
|
|
|
|
|
|
|
return transition.retry();
|
2017-08-02 10:05:59 +03:00
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
},
|
|
|
|
|
2020-05-11 13:37:35 +03:00
|
|
|
save: task(function* () {
|
2018-01-11 20:43:23 +03:00
|
|
|
try {
|
2021-02-18 03:50:27 +03:00
|
|
|
yield this.settings.validate();
|
|
|
|
return yield this.settings.save();
|
2018-01-11 20:43:23 +03:00
|
|
|
} catch (error) {
|
2021-02-18 03:50:27 +03:00
|
|
|
this.notifications.showAPIError(error);
|
|
|
|
throw error;
|
2018-01-11 20:43:23 +03:00
|
|
|
}
|
|
|
|
}).drop()
|
2017-08-02 10:05:59 +03:00
|
|
|
});
|