szurubooru/public_html/js/Presenters/RegistrationPresenter.js

101 lines
2.5 KiB
JavaScript
Raw Normal View History

2014-08-31 23:22:56 +02:00
var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.RegistrationPresenter = function(
2014-09-08 22:02:28 +02:00
_,
2014-08-31 23:22:56 +02:00
jQuery,
2014-09-02 09:36:42 +02:00
util,
2014-09-04 18:06:25 +02:00
promise,
2014-09-04 19:06:12 +02:00
api,
2014-08-31 23:22:56 +02:00
topNavigationPresenter,
2014-09-04 19:06:12 +02:00
messagePresenter) {
2014-08-31 23:22:56 +02:00
var $el = jQuery('#content');
2014-09-02 09:36:42 +02:00
var template;
2014-09-08 22:02:28 +02:00
var $messages;
2014-09-02 09:36:42 +02:00
2014-09-17 22:37:49 +02:00
function init(args, loaded) {
topNavigationPresenter.select('register');
topNavigationPresenter.changeTitle('Registration');
2014-09-17 22:37:49 +02:00
promise.wait(util.promiseTemplate('registration-form'))
.then(function(html) {
template = _.template(html);
render();
loaded();
});
2014-09-02 23:07:50 +02:00
}
2014-08-31 23:22:56 +02:00
function render() {
$el.html(template());
2014-09-02 23:07:50 +02:00
$el.find('form').submit(registrationFormSubmitted);
2014-08-31 23:22:56 +02:00
$messages = $el.find('.messages');
$messages.width($el.find('form').width());
2014-09-02 23:07:50 +02:00
}
function registrationFormSubmitted(e) {
e.preventDefault();
messagePresenter.hideMessages($messages);
2014-09-08 22:02:28 +02:00
var formData = {
userName: $el.find('[name=userName]').val(),
password: $el.find('[name=password]').val(),
passwordConfirmation: $el.find('[name=passwordConfirmation]').val(),
2014-09-02 23:07:50 +02:00
email: $el.find('[name=email]').val(),
};
2014-09-08 22:02:28 +02:00
if (!validateRegistrationFormData(formData)) {
2014-09-02 23:07:50 +02:00
return;
2014-09-08 22:02:28 +02:00
}
2014-09-02 23:07:50 +02:00
2014-10-02 00:30:25 +02:00
promise.wait(api.post('/users', formData))
2014-09-02 23:07:50 +02:00
.then(function(response) {
registrationSuccess(response);
}).fail(function(response) {
2014-09-02 23:07:50 +02:00
registrationFailure(response);
});
}
function registrationSuccess(apiResponse) {
$el.find('form').slideUp(function() {
var message = 'Registration complete! ';
if (!apiResponse.json.confirmed) {
message += '<br/>Check your inbox for activation e-mail.<br/>If e-mail doesn\'t show up, check your spam folder.';
} else {
message += '<a href="#/login">Click here</a> to login.';
}
2014-09-02 23:07:50 +02:00
messagePresenter.showInfo($messages, message);
});
}
function registrationFailure(apiResponse) {
messagePresenter.showError($messages, apiResponse.json && apiResponse.json.error || apiResponse);
}
2014-08-31 23:22:56 +02:00
function validateRegistrationFormData(formData) {
2014-09-08 22:02:28 +02:00
if (formData.userName.length === 0) {
2014-09-01 19:36:34 +02:00
messagePresenter.showError($messages, 'User name cannot be empty.');
2014-09-01 21:54:22 +02:00
return false;
2014-09-01 19:36:34 +02:00
}
2014-09-08 22:02:28 +02:00
if (formData.password.length === 0) {
2014-09-01 19:36:34 +02:00
messagePresenter.showError($messages, 'Password cannot be empty.');
2014-09-01 21:54:22 +02:00
return false;
2014-09-01 19:36:34 +02:00
}
2014-09-08 22:02:28 +02:00
if (formData.password !== formData.passwordConfirmation) {
2014-09-01 19:36:34 +02:00
messagePresenter.showError($messages, 'Passwords must be the same.');
2014-09-01 21:54:22 +02:00
return false;
2014-09-01 19:36:34 +02:00
}
2014-09-01 21:54:22 +02:00
return true;
2014-09-08 22:02:28 +02:00
}
2014-09-01 19:36:34 +02:00
2014-08-31 23:22:56 +02:00
return {
init: init,
2014-08-31 23:22:56 +02:00
render: render,
};
};
App.DI.register('registrationPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.RegistrationPresenter);