2019-01-24 22:34:32 +03:00
|
|
|
import Controller from '@ember/controller';
|
2019-12-13 14:37:01 +03:00
|
|
|
import EmberObject from '@ember/object';
|
|
|
|
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-12-13 14:37:01 +03:00
|
|
|
import {computed, defineProperty} from '@ember/object';
|
2019-02-22 14:31:45 +03:00
|
|
|
import {inject as controller} from '@ember/controller';
|
|
|
|
import {inject as service} from '@ember/service';
|
2019-04-16 18:02:43 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2019-01-24 22:34:32 +03:00
|
|
|
|
2019-12-13 14:37:01 +03:00
|
|
|
const SCRATCH_PROPS = ['name', 'email', 'note'];
|
|
|
|
|
2019-01-24 22:34:32 +03:00
|
|
|
export default Controller.extend({
|
2019-02-22 14:31:45 +03:00
|
|
|
members: controller(),
|
2019-10-10 14:59:35 +03:00
|
|
|
notifications: service(),
|
2019-12-12 16:35:52 +03:00
|
|
|
router: service(),
|
|
|
|
store: service(),
|
2019-02-22 14:31:45 +03:00
|
|
|
|
2019-10-10 14:59:35 +03:00
|
|
|
member: alias('model'),
|
2019-12-12 16:35:52 +03:00
|
|
|
|
2019-12-13 14:37:01 +03:00
|
|
|
scratchMember: computed('member', function () {
|
|
|
|
let scratchMember = EmberObject.create({member: this.member});
|
|
|
|
SCRATCH_PROPS.forEach(prop => defineProperty(scratchMember, prop, boundOneWay(`member.${prop}`)));
|
|
|
|
return scratchMember;
|
|
|
|
}),
|
|
|
|
|
2019-11-28 14:30:21 +03:00
|
|
|
subscribedAt: computed('member.createdAtUTC', function () {
|
|
|
|
let memberSince = moment(this.member.createdAtUTC).from(moment());
|
|
|
|
let createdDate = moment(this.member.createdAtUTC).format('MMM DD, YYYY');
|
2019-10-03 14:46:36 +03:00
|
|
|
return `${createdDate} (${memberSince})`;
|
2019-10-02 14:35:50 +03:00
|
|
|
}),
|
|
|
|
|
2019-02-22 14:31:45 +03:00
|
|
|
actions: {
|
2019-10-10 14:59:35 +03:00
|
|
|
setProperty(propKey, value) {
|
|
|
|
this._saveMemberProperty(propKey, value);
|
2019-10-02 07:00:03 +03:00
|
|
|
},
|
2019-12-12 16:35:52 +03:00
|
|
|
|
2019-10-10 15:36:33 +03:00
|
|
|
toggleDeleteMemberModal() {
|
2019-10-02 07:00:03 +03:00
|
|
|
this.toggleProperty('showDeleteMemberModal');
|
|
|
|
},
|
2019-12-12 16:35:52 +03:00
|
|
|
|
|
|
|
save() {
|
|
|
|
return this.save.perform();
|
|
|
|
},
|
|
|
|
|
2019-12-13 14:38:37 +03:00
|
|
|
deleteMember() {
|
|
|
|
return this.member.destroyRecord().then(() => {
|
|
|
|
return this.transitionToRoute('members');
|
|
|
|
}, (error) => {
|
|
|
|
return this.notifications.showAPIError(error, {key: 'member.delete'});
|
|
|
|
});
|
2019-10-10 15:36:33 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
toggleUnsavedChangesModal(transition) {
|
|
|
|
let leaveTransition = this.leaveScreenTransition;
|
2019-10-21 18:02:44 +03:00
|
|
|
|
2019-10-10 15:36:33 +03:00
|
|
|
if (!transition && this.showUnsavedChangesModal) {
|
|
|
|
this.set('leaveScreenTransition', null);
|
|
|
|
this.set('showUnsavedChangesModal', false);
|
|
|
|
return;
|
|
|
|
}
|
2019-10-21 18:02:44 +03:00
|
|
|
|
2019-10-10 15:36:33 +03:00
|
|
|
if (!leaveTransition || transition.targetName === leaveTransition.targetName) {
|
|
|
|
this.set('leaveScreenTransition', transition);
|
2019-10-21 18:02:44 +03:00
|
|
|
|
2019-10-10 15:36:33 +03:00
|
|
|
// 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-21 18:02:44 +03:00
|
|
|
|
2019-10-10 15:36:33 +03:00
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
this.set('showUnsavedChangesModal', true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
leaveScreen() {
|
|
|
|
this.member.rollbackAttributes();
|
2019-12-12 16:35:52 +03:00
|
|
|
return this.leaveScreenTransition.retry();
|
2019-02-22 14:31:45 +03:00
|
|
|
}
|
2019-04-16 18:02:43 +03:00
|
|
|
},
|
|
|
|
|
2019-10-10 14:59:35 +03:00
|
|
|
save: task(function* () {
|
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();
|
|
|
|
|
|
|
|
// 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'});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).drop(),
|
|
|
|
|
2019-04-16 18:02:43 +03:00
|
|
|
fetchMember: task(function* (memberId) {
|
2019-10-07 15:26:32 +03:00
|
|
|
this.set('isLoading', true);
|
2019-12-12 16:35:52 +03:00
|
|
|
|
2019-04-16 18:02:43 +03:00
|
|
|
yield this.store.findRecord('member', memberId, {
|
|
|
|
reload: true
|
2019-12-12 16:35:52 +03:00
|
|
|
}).then((member) => {
|
|
|
|
this.set('member', member);
|
2019-10-07 15:26:32 +03:00
|
|
|
this.set('isLoading', false);
|
2019-12-12 16:35:52 +03:00
|
|
|
return member;
|
2019-04-16 18:02:43 +03:00
|
|
|
});
|
2019-12-13 14:37:01 +03:00
|
|
|
}),
|
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
|
|
|
}
|
2019-01-24 22:34:32 +03:00
|
|
|
});
|