Ghost/core/client/app/controllers/modals/invite-new-user.js
Kevin Ansfield a0ec07f797 Ember-cli, Ember, & Ember Data 1.13.x upgrades
closes #5630
- upgrade ember-cli to latest version
- upgrade ember to latest 1.13.x release
- upgrade ember data to latest 1.13.x release
    - update custom adapters and serialisers for new internal JSON-API compatible formats [(docs)][1]
    - update all store queries to use new standardised query methods [(docs)][2]
    - add ember-data-filter addon ready for store.filter removal in ember-data 2.0 [(docs)][3]
- remove use of prototype extensions for computed properties and observers
- consolidate pagination into a single route mixin and simplify configuration

[1]: http://emberjs.com/blog/2015/06/18/ember-data-1-13-released.html#toc_transition-to-the-new-jsonserializer-and-restserializer-apis
[2]: http://emberjs.com/blog/2015/06/18/ember-data-1-13-released.html#toc_simplified-find-methods
[3]: http://emberjs.com/blog/2015/06/18/ember-data-1-13-released.html#toc_ds-store-filter-moved-to-an-addon
2015-10-06 16:09:05 +01:00

105 lines
3.8 KiB
JavaScript

import Ember from 'ember';
import ValidationEngine from 'ghost/mixins/validation-engine';
export default Ember.Controller.extend(ValidationEngine, {
notifications: Ember.inject.service(),
validationType: 'signup',
role: null,
authorRole: null,
roles: Ember.computed(function () {
return this.store.query('role', {permissions: 'assign'});
}),
// Used to set the initial value for the dropdown
authorRoleObserver: Ember.observer('roles.@each.role', function () {
var self = this;
this.get('roles').then(function (roles) {
var authorRole = roles.findBy('name', 'Author');
self.set('authorRole', authorRole);
if (!self.get('role')) {
self.set('role', authorRole);
}
});
}),
confirm: {
accept: {
text: 'send invitation now'
},
reject: {
buttonClass: 'hidden'
}
},
actions: {
setRole: function (role) {
this.set('role', role);
},
confirmAccept: function () {
var email = this.get('email'),
role = this.get('role'),
validationErrors = this.get('errors.messages'),
self = this,
newUser;
// reset the form and close the modal
this.set('email', '');
this.set('role', self.get('authorRole'));
this.store.findAll('user', {reload: true}).then(function (result) {
var invitedUser = result.findBy('email', email);
if (invitedUser) {
if (invitedUser.get('status') === 'invited' || invitedUser.get('status') === 'invited-pending') {
self.get('notifications').showAlert('A user with that email address was already invited.', {type: 'warn'});
} else {
self.get('notifications').showAlert('A user with that email address already exists.', {type: 'warn'});
}
} else {
newUser = self.store.createRecord('user', {
email: email,
status: 'invited',
role: role
});
newUser.save().then(function () {
var notificationText = 'Invitation sent! (' + email + ')';
// 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 (newUser.get('status') === 'invited-pending') {
self.get('notifications').showAlert('Invitation email was not sent. Please try resending.', {type: 'error'});
} else {
self.get('notifications').showNotification(notificationText);
}
}).catch(function (errors) {
newUser.deleteRecord();
// TODO: user model includes ValidationEngine mixin so
// save is overridden in order to validate, we probably
// want to use inline-validations here and only show an
// alert if we have an actual error
if (errors) {
self.get('notifications').showErrors(errors);
} else if (validationErrors) {
self.get('notifications').showAlert(validationErrors.toString(), {type: 'error'});
}
}).finally(function () {
self.get('errors').clear();
});
}
});
},
confirmReject: function () {
return false;
}
}
});