Ghost/ghost/admin/app/components/gh-user-invited.js
Kevin Ansfield 352c4af1d7 Refactored usage of .get('property') with es5 getters
no issue
- ran [es5-getter-ember-codemod](https://github.com/rondale-sc/es5-getter-ember-codemod)
- [es5 getters RFC](https://github.com/emberjs/rfcs/blob/master/text/0281-es5-getters.md)
- updates the majority of `object.get('property')` with `object.property` with exceptions:
  - `.get('nested.property')` - it's not possible to determine if this is relying on "safe" path chaining for when `nested` doesn't exist
  - `.get('config.x')` and `.get('settings.x')` - both our `config` and `settings` services are proxy objects which do not support es5 getters
- this PR is not exhaustive, there are still a number of places where `.get('service.foo')` and similar could be replaced but it gets us a long way there in a quick and automated fashion
2019-03-06 13:54:14 +00:00

92 lines
3.4 KiB
JavaScript

import Component from '@ember/component';
import moment from 'moment';
import {computed} from '@ember/object';
import {isNotFoundError} from 'ember-ajax/errors';
import {inject as service} from '@ember/service';
export default Component.extend({
notifications: service(),
store: service(),
tagName: '',
invite: null,
isSending: false,
createdAt: computed('invite.createdAtUTC', function () {
let createdAtUTC = this.get('invite.createdAtUTC');
return createdAtUTC ? moment(createdAtUTC).fromNow() : '';
}),
expiresAt: computed('invite.expires', function () {
let expires = this.get('invite.expires');
return expires ? moment(expires).fromNow() : '';
}),
isExpired: computed('invite.expires', function () {
let expires = this.get('invite.expires');
let now = (new Date()).valueOf();
return expires < now;
}),
actions: {
resend() {
let invite = this.invite;
let notifications = this.notifications;
this.set('isSending', true);
invite.resend().then((result) => {
let notificationText = `Invitation resent! (${invite.get('email')})`;
// the server deletes the old record and creates a new one when
// resending so we need to update the store accordingly
invite.unloadRecord();
this.store.pushPayload('invite', result);
// If sending the invitation email fails, the API will still return a status of 201
// but the invite's status in the response object will be 'invited-pending'.
if (result.invites[0].status === 'invited-pending') {
notifications.showAlert('Invitation email was not sent. Please try resending.', {type: 'error', key: 'invite.resend.not-sent'});
} else {
notifications.showNotification(notificationText, {key: 'invite.resend.success'});
}
}).catch((error) => {
notifications.showAPIError(error, {key: 'invite.resend'});
}).finally(() => {
this.set('isSending', false);
});
},
revoke() {
let invite = this.invite;
let email = invite.get('email');
let notifications = this.notifications;
// reload the invite to get the most up-to-date information
invite.reload().then(() => {
invite.destroyRecord().then(() => {
let notificationText = `Invitation revoked. (${email})`;
notifications.showNotification(notificationText, {key: 'invite.revoke.success'});
}).catch((error) => {
notifications.showAPIError(error, {key: 'invite.revoke'});
});
}).catch((error) => {
if (isNotFoundError(error)) {
// if the invite no longer exists, then show a warning and reload the route
let action = this.reload;
if (action) {
action();
}
notifications.showAlert('This invite has been revoked or a user has already accepted the invitation.', {type: 'error', delayed: true, key: 'invite.revoke.already-accepted'});
} else {
throw error;
}
});
}
}
});