2019-01-24 22:34:32 +03:00
|
|
|
import Controller from '@ember/controller';
|
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-04-16 18:02:43 +03:00
|
|
|
import {computed} 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
|
|
|
|
|
|
|
export default Controller.extend({
|
2019-02-22 14:31:45 +03:00
|
|
|
members: controller(),
|
2019-04-16 18:02:43 +03:00
|
|
|
store: service(),
|
|
|
|
|
2019-02-22 14:31:45 +03:00
|
|
|
router: service(),
|
|
|
|
|
2019-10-10 14:59:35 +03:00
|
|
|
notifications: service(),
|
2019-02-22 14:31:45 +03:00
|
|
|
|
2019-10-10 14:59:35 +03:00
|
|
|
member: alias('model'),
|
2019-10-02 14:35:50 +03:00
|
|
|
subscribedAt: computed('member.createdAt', function () {
|
2019-10-03 14:46:36 +03:00
|
|
|
let memberSince = moment(this.member.createdAt).from(moment());
|
2019-10-02 14:35:50 +03:00
|
|
|
let createdDate = moment(this.member.createdAt).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-10-10 15:36:33 +03:00
|
|
|
toggleDeleteMemberModal() {
|
2019-10-02 07:00:03 +03:00
|
|
|
this.toggleProperty('showDeleteMemberModal');
|
|
|
|
},
|
2019-02-22 14:31:45 +03:00
|
|
|
finaliseDeletion() {
|
|
|
|
// decrememnt the total member count manually so there's no flash
|
|
|
|
// when transitioning back to the members list
|
2019-10-19 15:36:38 +03:00
|
|
|
if (this.members.memberCount) {
|
|
|
|
this.members.decrementProperty('memberCount');
|
2019-02-22 14:31:45 +03:00
|
|
|
}
|
|
|
|
this.router.transitionTo('members');
|
2019-10-10 15:36:33 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
toggleUnsavedChangesModal(transition) {
|
|
|
|
let leaveTransition = this.leaveScreenTransition;
|
|
|
|
|
|
|
|
if (!transition && this.showUnsavedChangesModal) {
|
|
|
|
this.set('leaveScreenTransition', null);
|
|
|
|
this.set('showUnsavedChangesModal', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!leaveTransition || transition.targetName === leaveTransition.targetName) {
|
|
|
|
this.set('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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
this.set('showUnsavedChangesModal', true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
leaveScreen() {
|
|
|
|
let transition = this.leaveScreenTransition;
|
|
|
|
|
|
|
|
if (!transition) {
|
|
|
|
this.notifications.showAlert('Sorry, there was an error in the application. Please let the Ghost team know what happened.', {type: 'error'});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// roll back changes on model props
|
|
|
|
this.member.rollbackAttributes();
|
|
|
|
|
|
|
|
return transition.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* () {
|
|
|
|
let member = this.member;
|
|
|
|
try {
|
|
|
|
return yield member.save();
|
|
|
|
} catch (error) {
|
|
|
|
if (error) {
|
|
|
|
this.notifications.showAPIError(error, {key: 'member.save'});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).drop(),
|
|
|
|
|
|
|
|
_saveMemberProperty(propKey, newValue) {
|
|
|
|
let member = this.member;
|
|
|
|
member.set(propKey, newValue);
|
|
|
|
},
|
|
|
|
|
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-04-16 18:02:43 +03:00
|
|
|
yield this.store.findRecord('member', memberId, {
|
|
|
|
reload: true
|
|
|
|
}).then((data) => {
|
|
|
|
this.set('member', data);
|
2019-10-07 15:26:32 +03:00
|
|
|
this.set('isLoading', false);
|
2019-04-16 18:02:43 +03:00
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2019-01-24 22:34:32 +03:00
|
|
|
});
|