2022-01-17 13:05:27 +03:00
|
|
|
import AdminRoute from 'ghost-admin/routes/admin';
|
2022-09-09 17:05:35 +03:00
|
|
|
import ConfirmUnsavedChangesModal from '../components/modals/confirm-unsaved-changes';
|
2020-05-20 16:55:41 +03:00
|
|
|
import {action} from '@ember/object';
|
2019-10-10 15:36:33 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
|
2022-01-17 13:05:27 +03:00
|
|
|
export default class MembersRoute extends AdminRoute {
|
2022-01-26 13:55:05 +03:00
|
|
|
@service feature;
|
2022-09-09 17:05:35 +03:00
|
|
|
@service modals;
|
2020-05-20 16:55:41 +03:00
|
|
|
@service router;
|
2019-10-10 15:36:33 +03:00
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
_requiresBackgroundRefresh = true;
|
2019-12-12 16:35:52 +03:00
|
|
|
|
2021-08-31 11:37:44 +03:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
2019-10-10 15:36:33 +03:00
|
|
|
this.router.on('routeWillChange', (transition) => {
|
2022-08-03 12:13:16 +03:00
|
|
|
this.closeImpersonateModal(transition);
|
2019-10-10 15:36:33 +03:00
|
|
|
});
|
2020-05-20 16:55:41 +03:00
|
|
|
}
|
2019-10-10 15:36:33 +03:00
|
|
|
|
2019-04-16 18:02:43 +03:00
|
|
|
model(params) {
|
2019-12-12 16:35:52 +03:00
|
|
|
this._requiresBackgroundRefresh = false;
|
|
|
|
|
|
|
|
if (params.member_id) {
|
2022-05-11 20:11:54 +03:00
|
|
|
return this.store.queryRecord('member', {id: params.member_id, include: 'tiers'});
|
2019-12-12 16:35:52 +03:00
|
|
|
} else {
|
|
|
|
return this.store.createRecord('member');
|
|
|
|
}
|
2020-05-20 16:55:41 +03:00
|
|
|
}
|
2019-04-16 18:02:43 +03:00
|
|
|
|
2019-12-12 16:35:52 +03:00
|
|
|
setupController(controller, member) {
|
2020-05-20 16:55:41 +03:00
|
|
|
super.setupController(...arguments);
|
2019-12-12 16:35:52 +03:00
|
|
|
if (this._requiresBackgroundRefresh) {
|
2021-08-31 11:37:44 +03:00
|
|
|
// `member` is passed directly in `<LinkTo>` so it can be a proxy
|
|
|
|
// object used by the sparse list requiring the use of .get()
|
2020-05-20 16:55:41 +03:00
|
|
|
controller.fetchMemberTask.perform(member.get('id'));
|
2019-04-16 18:02:43 +03:00
|
|
|
}
|
2020-05-20 16:55:41 +03:00
|
|
|
}
|
2019-04-16 18:02:43 +03:00
|
|
|
|
|
|
|
deactivate() {
|
2019-12-12 16:35:52 +03:00
|
|
|
this._requiresBackgroundRefresh = true;
|
2022-09-09 17:05:35 +03:00
|
|
|
|
|
|
|
this.confirmModal = null;
|
|
|
|
this.hasConfirmed = false;
|
2020-05-20 16:55:41 +03:00
|
|
|
}
|
2019-01-24 22:34:32 +03:00
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
@action
|
|
|
|
save() {
|
|
|
|
this.controller.save();
|
|
|
|
}
|
2019-10-10 15:36:33 +03:00
|
|
|
|
2022-09-09 17:05:35 +03:00
|
|
|
@action
|
|
|
|
async willTransition(transition) {
|
|
|
|
if (this.hasConfirmed) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-10 15:36:33 +03:00
|
|
|
|
2022-09-09 17:05:35 +03:00
|
|
|
transition.abort();
|
2019-10-10 15:36:33 +03:00
|
|
|
|
2022-09-09 17:05:35 +03:00
|
|
|
// wait for any existing confirm modal to be closed before allowing transition
|
|
|
|
if (this.confirmModal) {
|
|
|
|
return;
|
|
|
|
}
|
2019-12-12 16:35:52 +03:00
|
|
|
|
2022-09-09 17:05:35 +03:00
|
|
|
if (this.controller.saveTask?.isRunning) {
|
|
|
|
await this.controller.saveTask.last;
|
2019-10-10 15:36:33 +03:00
|
|
|
}
|
2022-09-09 17:05:35 +03:00
|
|
|
|
|
|
|
const shouldLeave = await this.confirmUnsavedChanges();
|
|
|
|
|
|
|
|
if (shouldLeave) {
|
|
|
|
this.controller.model.rollbackAttributes();
|
|
|
|
this.hasConfirmed = true;
|
|
|
|
return transition.retry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async confirmUnsavedChanges() {
|
|
|
|
if (this.controller.model?.hasDirtyAttributes) {
|
|
|
|
this.confirmModal = this.modals
|
|
|
|
.open(ConfirmUnsavedChangesModal)
|
|
|
|
.finally(() => {
|
|
|
|
this.confirmModal = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
return this.confirmModal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2019-01-24 22:34:32 +03:00
|
|
|
}
|
2022-08-03 12:13:16 +03:00
|
|
|
|
|
|
|
closeImpersonateModal(transition) {
|
2022-09-09 17:05:35 +03:00
|
|
|
// If user navigates away with forward or back button, ensure returning to page
|
2022-08-03 12:13:16 +03:00
|
|
|
// hides modal
|
|
|
|
if (transition.from && transition.from.name === this.routeName && transition.targetName) {
|
|
|
|
let {controller} = this;
|
|
|
|
|
|
|
|
controller.closeImpersonateMemberModal(transition);
|
|
|
|
}
|
|
|
|
}
|
2022-09-09 17:05:35 +03:00
|
|
|
|
|
|
|
titleToken() {
|
|
|
|
return this.controller.member.name;
|
|
|
|
}
|
2020-05-20 16:55:41 +03:00
|
|
|
}
|