2020-05-20 16:55:41 +03:00
|
|
|
import Controller, {inject as controller} from '@ember/controller';
|
|
|
|
import EmberObject, {action, defineProperty} from '@ember/object';
|
2019-12-13 14:37:01 +03:00
|
|
|
import boundOneWay from 'ghost-admin/utils/bound-one-way';
|
2019-10-02 14:35:50 +03:00
|
|
|
import moment from 'moment';
|
2019-01-24 22:34:32 +03:00
|
|
|
import {alias} from '@ember/object/computed';
|
2019-02-22 14:31:45 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2020-05-20 16:55:41 +03:00
|
|
|
import {task} from 'ember-concurrency-decorators';
|
|
|
|
import {tracked} from '@glimmer/tracking';
|
2019-01-24 22:34:32 +03:00
|
|
|
|
2019-12-13 14:37:01 +03:00
|
|
|
const SCRATCH_PROPS = ['name', 'email', 'note'];
|
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
export default class MemberController extends Controller {
|
|
|
|
@controller members;
|
|
|
|
@service session;
|
|
|
|
@service dropdown;
|
|
|
|
@service notifications;
|
|
|
|
@service router;
|
|
|
|
@service store;
|
2019-02-22 14:31:45 +03:00
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
@tracked isLoading = false;
|
|
|
|
@tracked showDeleteMemberModal = false;
|
|
|
|
@tracked showImpersonateMemberModal = false;
|
|
|
|
@tracked showUnsavedChangesModal = false;
|
2020-02-27 06:50:15 +03:00
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
leaveScreenTransition = null;
|
2019-12-12 16:35:52 +03:00
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
// Computed properties -----------------------------------------------------
|
|
|
|
|
|
|
|
@alias('model') member;
|
|
|
|
|
|
|
|
get scratchMember() {
|
2019-12-13 14:37:01 +03:00
|
|
|
let scratchMember = EmberObject.create({member: this.member});
|
|
|
|
SCRATCH_PROPS.forEach(prop => defineProperty(scratchMember, prop, boundOneWay(`member.${prop}`)));
|
|
|
|
return scratchMember;
|
2020-05-20 16:55:41 +03:00
|
|
|
}
|
2019-12-13 14:37:01 +03:00
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
get subscribedAt() {
|
2020-05-20 18:39:06 +03:00
|
|
|
// member can be a proxy object in a sparse array so .get is required
|
|
|
|
let memberSince = moment(this.member.get('createdAtUTC')).from(moment());
|
|
|
|
let createdDate = moment(this.member.get('createdAtUTC')).format('MMM DD, YYYY');
|
2019-10-03 14:46:36 +03:00
|
|
|
return `${createdDate} (${memberSince})`;
|
2020-05-20 16:55:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Actions -----------------------------------------------------------------
|
|
|
|
|
|
|
|
@action
|
|
|
|
setProperty(propKey, value) {
|
|
|
|
this._saveMemberProperty(propKey, value);
|
|
|
|
}
|
2019-10-21 18:02:44 +03:00
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
@action
|
|
|
|
toggleDeleteMemberModal() {
|
|
|
|
this.showDeleteMemberModal = !this.showDeleteMemberModal;
|
|
|
|
}
|
2019-10-21 18:02:44 +03:00
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
@action
|
|
|
|
toggleImpersonateMemberModal() {
|
|
|
|
this.showImpersonateMemberModal = !this.showImpersonateMemberModal;
|
|
|
|
}
|
2019-10-21 18:02:44 +03:00
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
@action
|
|
|
|
save() {
|
|
|
|
return this.saveTask.perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
deleteMember() {
|
|
|
|
return this.member.destroyRecord().then(() => {
|
|
|
|
return this.transitionToRoute('members');
|
|
|
|
}, (error) => {
|
|
|
|
return this.notifications.showAPIError(error, {key: 'member.delete'});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleUnsavedChangesModal(transition) {
|
|
|
|
let leaveTransition = this.leaveScreenTransition;
|
|
|
|
|
|
|
|
if (!transition && this.showUnsavedChangesModal) {
|
|
|
|
this.leaveScreenTransition = null;
|
|
|
|
this.showUnsavedChangesModal = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!leaveTransition || transition.targetName === leaveTransition.targetName) {
|
|
|
|
this.leaveScreenTransition = transition;
|
|
|
|
|
|
|
|
// if a save is running, wait for it to finish then transition
|
|
|
|
if (this.save.isRunning) {
|
|
|
|
return this.save.last.then(() => {
|
|
|
|
transition.retry();
|
|
|
|
});
|
2019-10-10 15:36:33 +03:00
|
|
|
}
|
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
this.showUnsavedChangesModal = true;
|
2019-02-22 14:31:45 +03:00
|
|
|
}
|
2020-05-20 16:55:41 +03:00
|
|
|
}
|
2019-04-16 18:02:43 +03:00
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
@action
|
|
|
|
leaveScreen() {
|
|
|
|
this.member.rollbackAttributes();
|
|
|
|
return this.leaveScreenTransition.retry();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tasks -------------------------------------------------------------------
|
|
|
|
|
|
|
|
@task({drop: true})
|
|
|
|
*saveTask() {
|
2019-12-13 14:37:01 +03:00
|
|
|
let {member, scratchMember} = this;
|
|
|
|
|
|
|
|
// if Cmd+S is pressed before the field loses focus make sure we're
|
|
|
|
// saving the intended property values
|
|
|
|
let scratchProps = scratchMember.getProperties(SCRATCH_PROPS);
|
|
|
|
member.setProperties(scratchProps);
|
|
|
|
|
2019-10-10 14:59:35 +03:00
|
|
|
try {
|
2019-12-13 14:37:01 +03:00
|
|
|
yield member.save();
|
2020-02-14 12:34:01 +03:00
|
|
|
member.updateLabels();
|
2019-12-13 14:37:01 +03:00
|
|
|
// replace 'member.new' route with 'member' route
|
|
|
|
this.replaceRoute('member', member);
|
|
|
|
|
|
|
|
return member;
|
2019-10-10 14:59:35 +03:00
|
|
|
} catch (error) {
|
|
|
|
if (error) {
|
|
|
|
this.notifications.showAPIError(error, {key: 'member.save'});
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 16:55:41 +03:00
|
|
|
}
|
2019-10-10 14:59:35 +03:00
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
@task
|
|
|
|
*fetchMemberTask(memberId) {
|
|
|
|
this.isLoading = true;
|
2019-12-12 16:35:52 +03:00
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
this.member = yield this.store.findRecord('member', memberId, {
|
2019-04-16 18:02:43 +03:00
|
|
|
reload: true
|
|
|
|
});
|
2020-02-27 06:50:15 +03:00
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
this.isLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private -----------------------------------------------------------------
|
2019-04-16 18:02:43 +03:00
|
|
|
|
2019-12-13 14:37:01 +03:00
|
|
|
_saveMemberProperty(propKey, newValue) {
|
2019-12-16 20:01:51 +03:00
|
|
|
let currentValue = this.member.get(propKey);
|
|
|
|
|
|
|
|
if (newValue) {
|
|
|
|
newValue = newValue.trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
// avoid modifying empty values and triggering inadvertant unsaved changes modals
|
|
|
|
if (newValue !== false && !newValue && !currentValue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.member.set(propKey, newValue);
|
2019-12-13 14:37:01 +03:00
|
|
|
}
|
2020-05-20 16:55:41 +03:00
|
|
|
}
|