2015-06-13 17:34:09 +03:00
|
|
|
import Ember from 'ember';
|
|
|
|
|
2016-01-19 16:03:27 +03:00
|
|
|
const {
|
|
|
|
Component,
|
|
|
|
computed,
|
|
|
|
inject: {service}
|
|
|
|
} = Ember;
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
export default Component.extend({
|
2015-06-13 17:34:09 +03:00
|
|
|
tagName: '',
|
|
|
|
|
|
|
|
user: null,
|
2015-08-20 12:25:30 +03:00
|
|
|
isSending: false,
|
2015-06-13 17:34:09 +03:00
|
|
|
|
2016-01-19 16:03:27 +03:00
|
|
|
notifications: service(),
|
2015-06-13 17:34:09 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
createdAt: computed('user.created_at', function () {
|
|
|
|
let createdAt = this.get('user.created_at');
|
2015-06-13 17:34:09 +03:00
|
|
|
|
|
|
|
return createdAt ? createdAt.fromNow() : '';
|
|
|
|
}),
|
|
|
|
|
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
resend() {
|
|
|
|
let user = this.get('user');
|
|
|
|
let notifications = this.get('notifications');
|
2015-06-13 17:34:09 +03:00
|
|
|
|
2015-08-20 12:25:30 +03:00
|
|
|
this.set('isSending', true);
|
2015-10-28 14:36:45 +03:00
|
|
|
user.resendInvite().then((result) => {
|
|
|
|
let notificationText = `Invitation resent! (${user.get('email')})`;
|
2015-06-13 17:34:09 +03:00
|
|
|
|
|
|
|
// If sending the invitation email fails, the API will still return a status of 201
|
|
|
|
// but the user's status in the response object will be 'invited-pending'.
|
|
|
|
if (result.users[0].status === 'invited-pending') {
|
2015-10-07 17:44:23 +03:00
|
|
|
notifications.showAlert('Invitation email was not sent. Please try resending.', {type: 'error', key: 'invite.resend.not-sent'});
|
2015-06-13 17:34:09 +03:00
|
|
|
} else {
|
|
|
|
user.set('status', result.users[0].status);
|
2015-11-18 13:50:48 +03:00
|
|
|
notifications.showNotification(notificationText, {key: 'invite.resend.success'});
|
2015-06-13 17:34:09 +03:00
|
|
|
}
|
2015-10-28 14:36:45 +03:00
|
|
|
}).catch((error) => {
|
2015-10-07 17:44:23 +03:00
|
|
|
notifications.showAPIError(error, {key: 'invite.resend'});
|
2015-10-28 14:36:45 +03:00
|
|
|
}).finally(() => {
|
|
|
|
this.set('isSending', false);
|
2015-06-13 17:34:09 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
revoke() {
|
|
|
|
let user = this.get('user');
|
|
|
|
let email = user.get('email');
|
|
|
|
let notifications = this.get('notifications');
|
2015-06-13 17:34:09 +03:00
|
|
|
|
|
|
|
// reload the user to get the most up-to-date information
|
2015-10-28 14:36:45 +03:00
|
|
|
user.reload().then(() => {
|
2015-06-13 17:34:09 +03:00
|
|
|
if (user.get('invited')) {
|
2015-10-28 14:36:45 +03:00
|
|
|
user.destroyRecord().then(() => {
|
|
|
|
let notificationText = `Invitation revoked. (${email})`;
|
2015-11-18 13:50:48 +03:00
|
|
|
notifications.showNotification(notificationText, {key: 'invite.revoke.success'});
|
2015-10-28 14:36:45 +03:00
|
|
|
}).catch((error) => {
|
2015-10-07 17:44:23 +03:00
|
|
|
notifications.showAPIError(error, {key: 'invite.revoke'});
|
2015-06-13 17:34:09 +03:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// if the user is no longer marked as "invited", then show a warning and reload the route
|
2015-10-28 14:36:45 +03:00
|
|
|
this.sendAction('reload');
|
2015-10-07 17:44:23 +03:00
|
|
|
notifications.showAlert('This user has already accepted the invitation.', {type: 'error', delayed: true, key: 'invite.revoke.already-accepted'});
|
2015-06-13 17:34:09 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|