2015-03-29 21:10:53 +03:00
|
|
|
import Ember from 'ember';
|
2015-07-07 20:14:23 +03:00
|
|
|
import DS from 'ember-data';
|
2015-03-29 21:10:53 +03:00
|
|
|
|
2015-07-16 17:12:03 +03:00
|
|
|
export default Ember.Controller.extend({
|
2015-05-28 16:58:52 +03:00
|
|
|
notifications: Ember.inject.service(),
|
2015-08-10 07:40:27 +03:00
|
|
|
two: Ember.inject.controller('setup/two'),
|
|
|
|
|
2015-07-07 20:14:23 +03:00
|
|
|
errors: DS.Errors.create(),
|
2015-08-27 23:28:41 +03:00
|
|
|
hasValidated: Ember.A(),
|
2015-03-29 21:10:53 +03:00
|
|
|
users: '',
|
2015-08-10 07:40:27 +03:00
|
|
|
ownerEmail: Ember.computed.alias('two.email'),
|
2015-08-10 18:43:49 +03:00
|
|
|
submitting: false,
|
2015-08-27 11:41:24 +03:00
|
|
|
|
2015-03-29 21:10:53 +03:00
|
|
|
usersArray: Ember.computed('users', function () {
|
2015-08-27 11:41:24 +03:00
|
|
|
var errors = this.get('errors'),
|
|
|
|
users = this.get('users').split('\n').filter(function (email) {
|
2015-05-28 16:58:52 +03:00
|
|
|
return email.trim().length > 0;
|
|
|
|
});
|
|
|
|
|
2015-08-27 11:41:24 +03:00
|
|
|
// remove "no users to invite" error if we have users
|
|
|
|
if (users.uniq().length > 0 && errors.get('users.length') === 1) {
|
|
|
|
if (errors.get('users.firstObject').message.match(/no users/i)) {
|
|
|
|
errors.remove('users');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 16:58:52 +03:00
|
|
|
return users.uniq();
|
|
|
|
}),
|
2015-08-27 11:41:24 +03:00
|
|
|
|
|
|
|
validUsersArray: Ember.computed('usersArray', 'ownerEmail', function () {
|
|
|
|
var ownerEmail = this.get('ownerEmail');
|
|
|
|
|
2015-05-28 16:58:52 +03:00
|
|
|
return this.get('usersArray').filter(function (user) {
|
2015-08-27 11:41:24 +03:00
|
|
|
return validator.isEmail(user) && user !== ownerEmail;
|
2015-03-29 21:10:53 +03:00
|
|
|
});
|
|
|
|
}),
|
2015-05-28 16:58:52 +03:00
|
|
|
|
2015-08-27 11:41:24 +03:00
|
|
|
invalidUsersArray: Ember.computed('usersArray', 'ownerEmail', function () {
|
|
|
|
var ownerEmail = this.get('ownerEmail');
|
|
|
|
|
|
|
|
return this.get('usersArray').reject(function (user) {
|
|
|
|
return validator.isEmail(user) || user === ownerEmail;
|
|
|
|
});
|
2015-05-28 16:58:52 +03:00
|
|
|
}),
|
2015-08-27 11:41:24 +03:00
|
|
|
|
|
|
|
validationResult: Ember.computed('invalidUsersArray', function () {
|
|
|
|
var errors = [];
|
|
|
|
|
|
|
|
this.get('invalidUsersArray').forEach(function (user) {
|
|
|
|
errors.push({
|
|
|
|
user: user,
|
|
|
|
error: 'email'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (errors.length === 0) {
|
|
|
|
// ensure we aren't highlighting fields when everything is fine
|
|
|
|
this.get('errors').clear();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return errors;
|
|
|
|
}
|
2015-05-28 16:58:52 +03:00
|
|
|
}),
|
|
|
|
|
2015-08-27 11:41:24 +03:00
|
|
|
validate: function () {
|
|
|
|
var errors = this.get('errors'),
|
2015-08-27 23:28:41 +03:00
|
|
|
validationResult = this.get('validationResult'),
|
|
|
|
property = 'users';
|
2015-08-27 11:41:24 +03:00
|
|
|
|
|
|
|
errors.clear();
|
|
|
|
|
2015-08-27 23:28:41 +03:00
|
|
|
// If property isn't in the `hasValidated` array, add it to mark that this field can show a validation result
|
|
|
|
this.get('hasValidated').addObject(property);
|
|
|
|
|
2015-08-27 11:41:24 +03:00
|
|
|
if (validationResult === true) { return true; }
|
|
|
|
|
|
|
|
validationResult.forEach(function (error) {
|
|
|
|
// Only one error type here so far, but one day the errors might be more detailed
|
|
|
|
switch (error.error) {
|
|
|
|
case 'email':
|
2015-08-27 23:28:41 +03:00
|
|
|
errors.add(property, error.user + ' is not a valid email.');
|
2015-08-27 11:41:24 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
buttonText: Ember.computed('errors.users', 'validUsersArray', 'invalidUsersArray', function () {
|
|
|
|
var usersError = this.get('errors.users.firstObject.message'),
|
|
|
|
validNum = this.get('validUsersArray').length,
|
|
|
|
invalidNum = this.get('invalidUsersArray').length,
|
|
|
|
userCount;
|
|
|
|
|
|
|
|
if (usersError && usersError.match(/no users/i)) {
|
|
|
|
return usersError;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invalidNum > 0) {
|
|
|
|
userCount = invalidNum === 1 ? 'email address' : 'email addresses';
|
|
|
|
return `${invalidNum} invalid ${userCount}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (validNum > 0) {
|
|
|
|
userCount = validNum === 1 ? 'user' : 'users';
|
|
|
|
userCount = validNum + ' ' + userCount;
|
2015-05-28 16:58:52 +03:00
|
|
|
} else {
|
2015-08-27 11:41:24 +03:00
|
|
|
userCount = 'some users';
|
2015-05-28 16:58:52 +03:00
|
|
|
}
|
|
|
|
|
2015-08-27 11:41:24 +03:00
|
|
|
return 'Invite ' + userCount;
|
2015-03-29 21:10:53 +03:00
|
|
|
}),
|
2015-08-27 11:41:24 +03:00
|
|
|
|
|
|
|
buttonClass: Ember.computed('validationResult', 'usersArray.length', function () {
|
|
|
|
if (this.get('validationResult') === true && this.get('usersArray.length') > 0) {
|
2015-05-28 16:58:52 +03:00
|
|
|
return 'btn-green';
|
|
|
|
} else {
|
|
|
|
return 'btn-minor';
|
|
|
|
}
|
2015-03-29 21:10:53 +03:00
|
|
|
}),
|
2015-08-27 11:41:24 +03:00
|
|
|
|
2015-05-28 16:58:52 +03:00
|
|
|
authorRole: Ember.computed(function () {
|
|
|
|
return this.store.find('role').then(function (roles) {
|
|
|
|
return roles.findBy('name', 'Author');
|
|
|
|
});
|
2015-03-29 21:10:53 +03:00
|
|
|
}),
|
2015-08-27 11:41:24 +03:00
|
|
|
|
2015-03-29 21:10:53 +03:00
|
|
|
actions: {
|
2015-08-27 11:41:24 +03:00
|
|
|
validate: function () {
|
|
|
|
this.validate();
|
|
|
|
},
|
|
|
|
|
2015-03-29 21:10:53 +03:00
|
|
|
invite: function () {
|
2015-05-28 16:58:52 +03:00
|
|
|
var self = this,
|
|
|
|
users = this.get('usersArray'),
|
|
|
|
notifications = this.get('notifications'),
|
|
|
|
invitationsString;
|
2015-03-29 21:10:53 +03:00
|
|
|
|
2015-08-27 11:41:24 +03:00
|
|
|
if (this.validate() && users.length > 0) {
|
2015-08-10 18:43:49 +03:00
|
|
|
this.toggleProperty('submitting');
|
2015-05-28 16:58:52 +03:00
|
|
|
this.get('authorRole').then(function (authorRole) {
|
|
|
|
Ember.RSVP.Promise.all(
|
|
|
|
users.map(function (user) {
|
|
|
|
var newUser = self.store.createRecord('user', {
|
|
|
|
email: user,
|
|
|
|
status: 'invited',
|
|
|
|
role: authorRole
|
|
|
|
});
|
2015-03-29 21:10:53 +03:00
|
|
|
|
2015-05-28 16:58:52 +03:00
|
|
|
return newUser.save().then(function () {
|
|
|
|
return {
|
|
|
|
email: user,
|
|
|
|
success: newUser.get('status') === 'invited'
|
|
|
|
};
|
|
|
|
}).catch(function () {
|
|
|
|
return {
|
|
|
|
email: user,
|
|
|
|
success: false
|
|
|
|
};
|
|
|
|
});
|
|
|
|
})
|
|
|
|
).then(function (invites) {
|
|
|
|
var successCount = 0,
|
|
|
|
erroredEmails = [],
|
|
|
|
message;
|
2015-03-29 21:10:53 +03:00
|
|
|
|
2015-05-28 16:58:52 +03:00
|
|
|
invites.forEach(function (invite) {
|
|
|
|
if (invite.success) {
|
|
|
|
successCount++;
|
|
|
|
} else {
|
|
|
|
erroredEmails.push(invite.email);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (erroredEmails.length > 0) {
|
2015-08-27 20:17:13 +03:00
|
|
|
invitationsString = erroredEmails.length > 1 ? ' invitations: ' : ' invitation: ';
|
|
|
|
message = 'Failed to send ' + erroredEmails.length + invitationsString;
|
2015-05-28 16:58:52 +03:00
|
|
|
message += erroredEmails.join(', ');
|
2015-06-19 00:56:18 +03:00
|
|
|
notifications.showAlert(message, {type: 'error', delayed: successCount > 0});
|
2015-05-28 16:58:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (successCount > 0) {
|
|
|
|
// pluralize
|
|
|
|
invitationsString = successCount > 1 ? 'invitations' : 'invitation';
|
2015-06-19 00:56:18 +03:00
|
|
|
notifications.showAlert(successCount + ' ' + invitationsString + ' sent!', {type: 'success', delayed: true});
|
2015-05-28 16:58:52 +03:00
|
|
|
}
|
2015-08-28 14:35:40 +03:00
|
|
|
self.send('loadServerNotifications');
|
2015-08-10 18:43:49 +03:00
|
|
|
self.toggleProperty('submitting');
|
2015-08-28 14:35:40 +03:00
|
|
|
self.transitionToRoute('posts.index');
|
2015-05-28 16:58:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} else if (users.length === 0) {
|
2015-08-27 11:41:24 +03:00
|
|
|
this.get('errors').add('users', 'No users to invite');
|
2015-05-28 16:58:52 +03:00
|
|
|
}
|
2015-08-10 07:40:27 +03:00
|
|
|
},
|
2015-08-27 11:41:24 +03:00
|
|
|
|
2015-08-10 07:40:27 +03:00
|
|
|
skipInvite: function () {
|
|
|
|
this.send('loadServerNotifications');
|
2015-08-25 12:54:39 +03:00
|
|
|
this.transitionToRoute('posts.index');
|
2015-03-29 21:10:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|