1
0
Fork 0
mirror of https://github.com/TryGhost/Ghost-Admin.git synced 2023-12-14 02:33:04 +01:00
Ghost-Admin/app/controllers/settings/integrations/unsplash.js
Kevin Ansfield 69e5cdb5e9 Reduced unnecessary waiting in tests
no issue

- fixed `<GhTaskButton>` not resetting after an externally triggered task run such as when pressing Cmd+S
- cleaned up manual timeouts/resets where button reset is now fully handled by `<GhTaskButton>` (these were causing 2.5s waits each time a save occurred in acceptance tests)
- where manual timeouts were required, reduce testing time from >2.5s to 50ms
2020-05-11 11:37:35 +01:00

88 lines
2.7 KiB
JavaScript

/* eslint-disable ghost/ember/alias-model-in-controller */
import Controller from '@ember/controller';
import {alias} from '@ember/object/computed';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
export default Controller.extend({
notifications: service(),
settings: service(),
dirtyAttributes: null,
rollbackValue: null,
leaveSettingsTransition: null,
unsplashSettings: alias('settings.unsplash'),
actions: {
save() {
this.save.perform();
},
update(value) {
if (!this.dirtyAttributes) {
this.set('rollbackValue', this.get('unsplashSettings.isActive'));
}
this.set('unsplashSettings.isActive', value);
this.set('dirtyAttributes', true);
},
toggleLeaveSettingsModal(transition) {
let leaveTransition = this.leaveSettingsTransition;
if (!transition && this.showLeaveSettingsModal) {
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
if (this.save.isRunning) {
return this.save.last.then(() => {
transition.retry();
});
}
// we genuinely have unsaved data, show the modal
this.set('showLeaveSettingsModal', true);
}
},
leaveSettings() {
let transition = this.leaveSettingsTransition;
if (!transition) {
this.notifications.showAlert('Sorry, there was an error in the application. Please let the Ghost team know what happened.', {type: 'error'});
return;
}
// roll back changes on model props
this.set('unsplashSettings.isActive', this.rollbackValue);
this.set('dirtyAttributes', false);
this.set('rollbackValue', null);
return transition.retry();
}
},
save: task(function* () {
let unsplash = this.unsplashSettings;
let settings = this.settings;
try {
settings.set('unsplash', unsplash);
this.set('dirtyAttributes', false);
this.set('rollbackValue', null);
return yield settings.save();
} catch (error) {
if (error) {
this.notifications.showAPIError(error);
throw error;
}
}
}).drop()
});