2020-05-20 16:55:41 +03:00
|
|
|
import Controller, {inject as controller} from '@ember/controller';
|
2022-09-24 19:18:34 +03:00
|
|
|
import DeleteMemberModal from '../components/members/modals/delete-member';
|
2020-05-20 16:55:41 +03:00
|
|
|
import EmberObject, {action, defineProperty} from '@ember/object';
|
2019-12-13 14:37:01 +03:00
|
|
|
import boundOneWay from 'ghost-admin/utils/bound-one-way';
|
2022-09-23 20:15:08 +03:00
|
|
|
import moment from 'moment-timezone';
|
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;
|
2022-09-24 19:18:34 +03:00
|
|
|
@service modals;
|
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 showImpersonateMemberModal = false;
|
2021-09-08 20:29:41 +03:00
|
|
|
@tracked modalLabel = null;
|
|
|
|
@tracked showLabelModal = false;
|
2020-02-27 06:50:15 +03:00
|
|
|
|
2022-10-21 23:05:14 +03:00
|
|
|
_previousLabels = null;
|
|
|
|
_previousNewsletters = null;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-24 19:18:34 +03:00
|
|
|
set member(member) {
|
|
|
|
this.model = member;
|
|
|
|
}
|
|
|
|
|
2022-10-21 23:05:14 +03:00
|
|
|
get dirtyAttributes() {
|
|
|
|
return this._hasDirtyAttributes();
|
|
|
|
}
|
|
|
|
|
|
|
|
get _labels() {
|
|
|
|
return this.member.get('labels').map(label => label.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
get _newsletters() {
|
|
|
|
return this.member.get('newsletters').map(newsletter => newsletter.id);
|
|
|
|
}
|
|
|
|
|
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-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() {
|
2022-10-07 19:03:45 +03:00
|
|
|
let memberSince = moment(this.member.createdAtUTC).from(moment());
|
|
|
|
let createdDate = moment(this.member.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 -----------------------------------------------------------------
|
|
|
|
|
2022-10-21 23:05:14 +03:00
|
|
|
@action
|
|
|
|
setInitialRelationshipValues() {
|
|
|
|
this._previousLabels = this._labels;
|
|
|
|
this._previousNewsletters = this._newsletters;
|
|
|
|
}
|
|
|
|
|
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
|
2022-09-24 19:18:34 +03:00
|
|
|
confirmDeleteMember() {
|
|
|
|
this.modals.open(DeleteMemberModal, {
|
|
|
|
member: this.member,
|
|
|
|
afterDelete: () => {
|
|
|
|
this.membersStats.invalidate();
|
|
|
|
this.members.refreshData();
|
|
|
|
this.transitionToRoute('members');
|
|
|
|
}
|
|
|
|
});
|
2020-05-20 16:55:41 +03:00
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2022-10-07 19:03:45 +03:00
|
|
|
Object.assign(member, scratchProps);
|
2019-12-13 14:37:01 +03:00
|
|
|
|
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();
|
|
|
|
|
2022-10-21 23:05:14 +03:00
|
|
|
this.setInitialRelationshipValues();
|
|
|
|
|
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
|
|
|
|
2022-10-21 23:05:14 +03:00
|
|
|
this.setInitialRelationshipValues();
|
|
|
|
|
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) {
|
2022-10-07 19:03:45 +03:00
|
|
|
let currentValue = this.member[propKey];
|
2019-12-16 20:01:51 +03:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-10-07 19:03:45 +03:00
|
|
|
this.member[propKey] = newValue;
|
2019-12-13 14:37:01 +03:00
|
|
|
}
|
2022-10-21 23:05:14 +03:00
|
|
|
|
|
|
|
_hasDirtyAttributes() {
|
|
|
|
let member = this.member;
|
|
|
|
|
|
|
|
if (!member) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// member.labels is an array so hasDirtyAttributes doesn't pick up
|
|
|
|
// changes unless the array ref is changed.
|
|
|
|
// use sort() to sort of detect same item is re-added
|
|
|
|
let currentLabels = (this._labels.sort() || []).join(', ');
|
|
|
|
let previousLabels = (this._previousLabels.sort() || []).join(', ');
|
|
|
|
if (currentLabels !== previousLabels) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// member.newsletters is an array so hasDirtyAttributes doesn't pick up
|
|
|
|
// changes unless the array ref is changed
|
|
|
|
// use sort() to sort of detect same item is re-enabled
|
|
|
|
let currentNewsletters = (this._newsletters.sort() || []).join(', ');
|
|
|
|
let previousNewsletters = (this._previousNewsletters.sort() || []).join(', ');
|
|
|
|
if (currentNewsletters !== previousNewsletters) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we've covered all the non-tracked cases we care about so fall
|
|
|
|
// back on Ember Data's default dirty attribute checks
|
|
|
|
let {hasDirtyAttributes} = member;
|
|
|
|
|
|
|
|
return hasDirtyAttributes;
|
|
|
|
}
|
2020-05-20 16:55:41 +03:00
|
|
|
}
|