2015-03-29 21:10:53 +03:00
|
|
|
import Ember from 'ember';
|
2015-05-27 23:10:47 +03:00
|
|
|
import {request as ajax} from 'ic-ajax';
|
2015-03-29 21:10:53 +03:00
|
|
|
import ValidationEngine from 'ghost/mixins/validation-engine';
|
|
|
|
|
2015-05-27 23:10:47 +03:00
|
|
|
export default Ember.Controller.extend(ValidationEngine, {
|
2015-03-29 21:10:53 +03:00
|
|
|
size: 90,
|
|
|
|
blogTitle: null,
|
|
|
|
name: null,
|
|
|
|
email: '',
|
2015-07-16 00:03:32 +03:00
|
|
|
validEmail: '',
|
2015-03-29 21:10:53 +03:00
|
|
|
password: null,
|
|
|
|
image: null,
|
2015-07-10 02:54:02 +03:00
|
|
|
blogCreated: false,
|
2015-08-10 18:43:49 +03:00
|
|
|
submitting: false,
|
2015-08-10 20:45:50 +03:00
|
|
|
flowErrors: '',
|
2015-03-29 21:10:53 +03:00
|
|
|
|
2015-05-27 23:10:47 +03:00
|
|
|
ghostPaths: Ember.inject.service('ghost-paths'),
|
|
|
|
notifications: Ember.inject.service(),
|
2015-05-28 16:58:52 +03:00
|
|
|
application: Ember.inject.controller(),
|
2015-07-10 17:57:09 +03:00
|
|
|
config: Ember.inject.service(),
|
2015-05-27 23:10:47 +03:00
|
|
|
|
2015-03-29 21:10:53 +03:00
|
|
|
// ValidationEngine settings
|
|
|
|
validationType: 'setup',
|
|
|
|
|
2015-05-28 08:52:41 +03:00
|
|
|
/**
|
|
|
|
* Uploads the given data image, then sends the changed user image property to the server
|
|
|
|
* @param {Object} user User object, returned from the 'setup' api call
|
|
|
|
* @return {Ember.RSVP.Promise} A promise that takes care of both calls
|
|
|
|
*/
|
|
|
|
sendImage: function (user) {
|
|
|
|
var self = this,
|
|
|
|
image = this.get('image');
|
|
|
|
|
|
|
|
return new Ember.RSVP.Promise(function (resolve, reject) {
|
|
|
|
image.formData = {};
|
|
|
|
image.submit()
|
|
|
|
.success(function (response) {
|
|
|
|
user.image = response;
|
|
|
|
ajax({
|
|
|
|
url: self.get('ghostPaths.url').api('users', user.id.toString()),
|
|
|
|
type: 'PUT',
|
|
|
|
data: {
|
|
|
|
users: [user]
|
|
|
|
}
|
|
|
|
}).then(resolve).catch(reject);
|
|
|
|
})
|
|
|
|
.error(reject);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-03-29 21:10:53 +03:00
|
|
|
actions: {
|
2015-08-27 22:49:36 +03:00
|
|
|
preValidate: function (model) {
|
2015-08-27 23:28:41 +03:00
|
|
|
// Only triggers validation if a value has been entered, preventing empty errors on focusOut
|
2015-08-27 22:49:36 +03:00
|
|
|
if (this.get(model)) {
|
|
|
|
if (model === 'email') {
|
2015-08-27 23:28:41 +03:00
|
|
|
this.send('handleEmail');
|
2015-08-27 22:49:36 +03:00
|
|
|
}
|
|
|
|
this.validate({property: model});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-29 21:10:53 +03:00
|
|
|
setup: function () {
|
|
|
|
var self = this,
|
2015-08-27 23:28:41 +03:00
|
|
|
setupProperties = ['blogTitle', 'name', 'email', 'password', 'image'],
|
|
|
|
data = self.getProperties(setupProperties),
|
2015-07-10 17:57:09 +03:00
|
|
|
notifications = this.get('notifications'),
|
2015-07-10 02:54:02 +03:00
|
|
|
config = this.get('config'),
|
2015-07-16 17:12:03 +03:00
|
|
|
method = this.get('blogCreated') ? 'PUT' : 'POST';
|
2015-03-29 21:10:53 +03:00
|
|
|
|
2015-08-10 18:43:49 +03:00
|
|
|
this.toggleProperty('submitting');
|
2015-08-10 20:45:50 +03:00
|
|
|
this.set('flowErrors', '');
|
2015-08-10 18:43:49 +03:00
|
|
|
|
2015-08-27 23:28:41 +03:00
|
|
|
this.get('hasValidated').addObjects(setupProperties);
|
2015-06-07 06:19:19 +03:00
|
|
|
this.validate().then(function () {
|
2015-03-29 21:10:53 +03:00
|
|
|
ajax({
|
|
|
|
url: self.get('ghostPaths.url').api('authentication', 'setup'),
|
2015-07-10 02:54:02 +03:00
|
|
|
type: method,
|
2015-03-29 21:10:53 +03:00
|
|
|
data: {
|
|
|
|
setup: [{
|
|
|
|
name: data.name,
|
|
|
|
email: data.email,
|
|
|
|
password: data.password,
|
|
|
|
blogTitle: data.blogTitle
|
|
|
|
}]
|
|
|
|
}
|
2015-05-28 08:52:41 +03:00
|
|
|
}).then(function (result) {
|
2015-07-10 17:57:09 +03:00
|
|
|
config.set('blogTitle', data.blogTitle);
|
2015-05-28 16:58:52 +03:00
|
|
|
// Don't call the success handler, otherwise we will be redirected to admin
|
|
|
|
self.get('application').set('skipAuthSuccessHandler', true);
|
2015-08-28 18:00:34 +03:00
|
|
|
self.get('session').authenticate('ghost-authenticator:oauth2-password-grant', {
|
2015-03-29 21:10:53 +03:00
|
|
|
identification: self.get('email'),
|
|
|
|
password: self.get('password')
|
2015-05-28 16:58:52 +03:00
|
|
|
}).then(function () {
|
2015-07-10 02:54:02 +03:00
|
|
|
self.set('blogCreated', true);
|
2015-05-28 08:52:41 +03:00
|
|
|
if (data.image) {
|
|
|
|
self.sendImage(result.users[0])
|
|
|
|
.then(function () {
|
2015-08-10 18:43:49 +03:00
|
|
|
self.toggleProperty('submitting');
|
2015-05-28 08:52:41 +03:00
|
|
|
self.transitionToRoute('setup.three');
|
|
|
|
}).catch(function (resp) {
|
2015-08-10 18:43:49 +03:00
|
|
|
self.toggleProperty('submitting');
|
2015-05-28 08:52:41 +03:00
|
|
|
notifications.showAPIError(resp);
|
|
|
|
});
|
|
|
|
} else {
|
2015-08-10 18:43:49 +03:00
|
|
|
self.toggleProperty('submitting');
|
2015-05-28 08:52:41 +03:00
|
|
|
self.transitionToRoute('setup.three');
|
|
|
|
}
|
2015-03-29 21:10:53 +03:00
|
|
|
});
|
|
|
|
}).catch(function (resp) {
|
2015-08-10 18:43:49 +03:00
|
|
|
self.toggleProperty('submitting');
|
2015-08-10 20:45:50 +03:00
|
|
|
if (resp && resp.jqXHR && resp.jqXHR.responseJSON && resp.jqXHR.responseJSON.errors) {
|
|
|
|
self.set('flowErrors', resp.jqXHR.responseJSON.errors[0].message);
|
|
|
|
} else {
|
|
|
|
notifications.showAPIError(resp);
|
|
|
|
}
|
2015-03-29 21:10:53 +03:00
|
|
|
});
|
2015-06-07 06:19:19 +03:00
|
|
|
}).catch(function () {
|
2015-08-10 18:43:49 +03:00
|
|
|
self.toggleProperty('submitting');
|
2015-08-10 20:45:50 +03:00
|
|
|
self.set('flowErrors', 'Please fill out the form to setup your blog.');
|
2015-03-29 21:10:53 +03:00
|
|
|
});
|
2015-05-28 08:52:41 +03:00
|
|
|
},
|
|
|
|
setImage: function (image) {
|
|
|
|
this.set('image', image);
|
2015-07-16 00:03:32 +03:00
|
|
|
},
|
|
|
|
handleEmail: function () {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.validate({property: 'email'}).then(function () {
|
|
|
|
self.set('validEmail', self.get('email'));
|
|
|
|
});
|
2015-03-29 21:10:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|