szurubooru/public_html/js/Presenters/RegistrationPresenter.js

91 lines
2.2 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(
jQuery,
2014-09-02 09:36:42 +02:00
util,
2014-08-31 23:22:56 +02:00
topNavigationPresenter,
messagePresenter,
api) {
var $el = jQuery('#content');
2014-09-02 09:36:42 +02:00
var template;
2014-09-02 23:07:50 +02:00
function init() {
topNavigationPresenter.select('register');
util.loadTemplate('registration-form').then(function(html) {
template = _.template(html);
render();
});
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);
registrationData = {
userName: $el.find('[name=user]').val(),
password: $el.find('[name=password1]').val(),
passwordConfirmation: $el.find('[name=password2]').val(),
email: $el.find('[name=email]').val(),
};
if (!validateRegistrationData(registrationData))
return;
api.post('/users', registrationData)
.then(function(response) {
registrationSuccess(response);
}).catch(function(response) {
registrationFailure(response);
});
}
function registrationSuccess(apiResponse) {
//todo: tell user if it turned out that he needs to confirm his e-mail
$el.find('form').slideUp(function() {
var message = 'Registration complete! ';
message += '<a href="#/login">Click here</a> to login.';
messagePresenter.showInfo($messages, message);
});
}
function registrationFailure(apiResponse) {
messagePresenter.showError($messages, apiResponse.json && apiResponse.json.error || apiResponse);
}
2014-08-31 23:22:56 +02:00
2014-09-01 19:36:34 +02:00
function validateRegistrationData(registrationData) {
if (registrationData.userName.length == 0) {
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
}
if (registrationData.password.length == 0) {
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
}
if (registrationData.password != registrationData.passwordConfirmation) {
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-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', App.Presenters.RegistrationPresenter);