Ghost/ghost/admin/app/routes/member.js
Scott Beinlich 08b31ebadb 🐛 Fixed impersonate modal not closing correctly when navigating away from member page. (#1753)
closes https://github.com/TryGhost/Ghost/issues/12214

- previously, when navigating back from the members page with the impersonate modal open, opening a new member showed the impersonate modal.
2022-08-03 10:13:16 +01:00

79 lines
2.5 KiB
JavaScript

import AdminRoute from 'ghost-admin/routes/admin';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
export default class MembersRoute extends AdminRoute {
@service feature;
@service router;
_requiresBackgroundRefresh = true;
constructor() {
super(...arguments);
this.router.on('routeWillChange', (transition) => {
this.showUnsavedChangesModal(transition);
this.closeImpersonateModal(transition);
});
}
model(params) {
this._requiresBackgroundRefresh = false;
if (params.member_id) {
return this.store.queryRecord('member', {id: params.member_id, include: 'tiers'});
} else {
return this.store.createRecord('member');
}
}
setupController(controller, member) {
super.setupController(...arguments);
if (this._requiresBackgroundRefresh) {
// `member` is passed directly in `<LinkTo>` so it can be a proxy
// object used by the sparse list requiring the use of .get()
controller.fetchMemberTask.perform(member.get('id'));
}
}
deactivate() {
super.deactivate(...arguments);
// clean up newly created records and revert unsaved changes to existing
this.controller.member.rollbackAttributes();
this._requiresBackgroundRefresh = true;
}
@action
save() {
this.controller.save();
}
titleToken() {
return this.controller.member.name;
}
showUnsavedChangesModal(transition) {
if (transition.from && transition.from.name === this.routeName && transition.targetName) {
let {controller} = this;
// member.changedAttributes is always true for new members but number of changed attrs is reliable
let isChanged = Object.keys(controller.member.changedAttributes()).length > 0;
if (!controller.member.isDeleted && isChanged) {
transition.abort();
controller.toggleUnsavedChangesModal(transition);
return;
}
}
}
closeImpersonateModal(transition) {
// If user navigates away with forward or back button, ensure returning to page
// hides modal
if (transition.from && transition.from.name === this.routeName && transition.targetName) {
let {controller} = this;
controller.closeImpersonateMemberModal(transition);
}
}
}