Ghost/ghost/admin/app/routes/settings/design/index.js
Kevin Ansfield 4c5144cb61 🐛 Fixed close button on "unsaved changes" modal not always behaving like "Stay" button
no issue

The close button in the unsaved changes modal used the `@close` action directly compared to the "Stay" button which used `(fn @close false)`. The close button version without explicit arguments resulted in the first argument being a `PointerEvent` instance which if checked as a modal return value as `if (shouldClose)` would evaluate as truthy therefore matching the "Leave" behaviour instead of the "Stay" behaviour.

- changed modal to explicitly call `(fn @close false)` when the close button is clicked
- updated design settings route to have an explicit `shouldLeave === true` check so default behaviour is always "Stay"
2022-04-14 12:39:10 +01:00

59 lines
1.8 KiB
JavaScript

import AuthenticatedRoute from 'ghost-admin/routes/authenticated';
import ConfirmUnsavedChangesModal from '../../../components/modals/confirm-unsaved-changes';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
export default class SettingsDesignIndexRoute extends AuthenticatedRoute {
@service customThemeSettings;
@service modals;
@service settings;
confirmModal = null;
hasConfirmed = false;
@action
willTransition(transition) {
if (this.hasConfirmed) {
return true;
}
// always abort when not confirmed because Ember's router doesn't automatically wait on promises
transition.abort();
this.confirmUnsavedChanges().then((shouldLeave) => {
if (shouldLeave === true) {
this.hasConfirmed = true;
return transition.retry();
}
});
}
deactivate() {
this.confirmModal = null;
this.hasConfirmed = false;
this.controllerFor('settings.design.index').reset();
}
confirmUnsavedChanges() {
if (!this.settings.get('hasDirtyAttributes') && !this.customThemeSettings.isDirty) {
return Promise.resolve(true);
}
if (!this.confirmModal) {
this.confirmModal = this.modals.open(ConfirmUnsavedChangesModal)
.then((discardChanges) => {
if (discardChanges === true) {
this.settings.rollbackAttributes();
this.customThemeSettings.rollback();
}
return discardChanges;
}).finally(() => {
this.confirmModal = null;
});
}
return this.confirmModal;
}
}