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-02-22 14:31:45 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2022-02-09 13:49:38 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2020-05-20 16:55:41 +03:00
|
|
|
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;
|
2020-05-26 19:17:35 +03:00
|
|
|
@service membersStats;
|
2020-05-20 16:55:41 +03:00
|
|
|
@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;
|
2021-09-08 20:29:41 +03:00
|
|
|
@tracked modalLabel = null;
|
|
|
|
@tracked showLabelModal = 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
|
|
|
|
2021-09-08 20:29:41 +03:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this._availableLabels = this.store.peekAll('label');
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
// Computed properties -----------------------------------------------------
|
|
|
|
|
2020-10-23 11:58:55 +03:00
|
|
|
get member() {
|
|
|
|
return this.model;
|
|
|
|
}
|
|
|
|
|
2021-09-08 20:29:41 +03:00
|
|
|
get labelModalData() {
|
|
|
|
let label = this.modalLabel;
|
|
|
|
let labels = this.availableLabels;
|
|
|
|
|
|
|
|
return {
|
|
|
|
label,
|
|
|
|
labels
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
get availableLabels() {
|
|
|
|
let labels = this._availableLabels
|
|
|
|
.filter(label => !label.isNew)
|
|
|
|
.filter(label => label.id !== null)
|
|
|
|
.sort((labelA, labelB) => labelA.name.localeCompare(labelB.name, undefined, {ignorePunctuation: true}));
|
|
|
|
let options = labels.toArray();
|
|
|
|
|
|
|
|
options.unshiftObject({name: 'All labels', slug: null});
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2020-10-23 11:58:55 +03:00
|
|
|
set member(member) {
|
|
|
|
this.model = member;
|
|
|
|
}
|
2020-05-20 16:55:41 +03:00
|
|
|
|
|
|
|
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());
|
2020-07-06 18:53:25 +03:00
|
|
|
let createdDate = moment(this.member.get('createdAtUTC')).format('D MMM YYYY');
|
2019-10-03 14:46:36 +03:00
|
|
|
return `${createdDate} (${memberSince})`;
|
2020-05-20 16:55:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Actions -----------------------------------------------------------------
|
|
|
|
|
2021-09-08 20:29:41 +03:00
|
|
|
@action
|
|
|
|
toggleLabelModal() {
|
|
|
|
this.showLabelModal = !this.showLabelModal;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
editLabel(label, e) {
|
|
|
|
if (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
let modalLabel = this.availableLabels.findBy('slug', label);
|
|
|
|
this.modalLabel = modalLabel;
|
|
|
|
this.showLabelModal = !this.showLabelModal;
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
@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
|
|
|
|
2022-08-03 12:13:16 +03:00
|
|
|
@action
|
|
|
|
closeImpersonateMemberModal() {
|
|
|
|
this.showImpersonateMemberModal = false;
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:55:41 +03:00
|
|
|
@action
|
|
|
|
save() {
|
|
|
|
return this.saveTask.perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2020-07-24 17:02:42 +03:00
|
|
|
deleteMember(cancelSubscriptions = false) {
|
|
|
|
let options = {
|
|
|
|
adapterOptions: {
|
|
|
|
cancel: cancelSubscriptions
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return this.member.destroyRecord(options).then(() => {
|
2020-06-05 10:53:58 +03:00
|
|
|
this.members.refreshData();
|
2021-08-30 10:40:46 +03:00
|
|
|
this.transitionToRoute('members');
|
|
|
|
return;
|
2020-05-20 16:55:41 +03:00
|
|
|
}, (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();
|
2020-06-05 11:00:40 +03:00
|
|
|
this.members.refreshData();
|
|
|
|
|
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) {
|
2022-09-08 19:29:48 +03:00
|
|
|
if (error === undefined) {
|
|
|
|
// Validation error
|
|
|
|
return;
|
2019-10-10 14:59:35 +03:00
|
|
|
}
|
2022-09-08 19:29:48 +03:00
|
|
|
|
|
|
|
if (error.payload && error.payload.errors) {
|
|
|
|
for (const payloadError of error.payload.errors) {
|
|
|
|
if (payloadError.type === 'ValidationError' && payloadError.property && (payloadError.context || payloadError.message)) {
|
|
|
|
member.errors.add(payloadError.property, payloadError.context || payloadError.message);
|
|
|
|
member.hasValidated.pushObject(payloadError.property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
2019-10-10 14:59:35 +03:00
|
|
|
}
|
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
|
|
|
|
2021-04-26 19:28:39 +03:00
|
|
|
this.member = yield this.store.queryRecord('member', {
|
|
|
|
id: memberId,
|
2022-05-11 20:11:54 +03:00
|
|
|
include: 'tiers'
|
2019-04-16 18:02:43 +03:00
|
|
|
});
|
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);
|
|
|
|
|
2021-05-19 20:05:55 +03:00
|
|
|
if (newValue && typeof newValue === 'string') {
|
2019-12-16 20:01:51 +03:00
|
|
|
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
|
|
|
}
|