2022-01-17 13:05:27 +03:00
|
|
|
import AdminRoute from 'ghost-admin/routes/admin';
|
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;
|
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) => {
|
|
|
|
this.showUnsavedChangesModal(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() {
|
2020-05-20 16:55:41 +03:00
|
|
|
super.deactivate(...arguments);
|
2019-12-12 16:35:52 +03:00
|
|
|
// clean up newly created records and revert unsaved changes to existing
|
|
|
|
this.controller.member.rollbackAttributes();
|
|
|
|
this._requiresBackgroundRefresh = true;
|
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
|
|
|
|
2019-01-24 22:34:32 +03:00
|
|
|
titleToken() {
|
2020-05-20 16:55:41 +03:00
|
|
|
return this.controller.member.name;
|
|
|
|
}
|
2019-10-10 15:36:33 +03:00
|
|
|
|
|
|
|
showUnsavedChangesModal(transition) {
|
2019-12-12 16:35:52 +03:00
|
|
|
if (transition.from && transition.from.name === this.routeName && transition.targetName) {
|
2019-10-10 15:36:33 +03:00
|
|
|
let {controller} = this;
|
|
|
|
|
2019-12-12 16:35:52 +03:00
|
|
|
// 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) {
|
2019-10-10 15:36:33 +03:00
|
|
|
transition.abort();
|
2020-05-20 16:55:41 +03:00
|
|
|
controller.toggleUnsavedChangesModal(transition);
|
2019-10-10 15:36:33 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-01-24 22:34:32 +03:00
|
|
|
}
|
2022-08-03 12:13:16 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 16:55:41 +03:00
|
|
|
}
|