Finished registration frontend

This commit is contained in:
Marcin Kurczewski 2014-09-01 19:36:34 +02:00
parent a55a8a825c
commit f59968a062

View file

@ -18,36 +18,35 @@ App.Presenters.RegistrationPresenter = function(
e.preventDefault(); e.preventDefault();
messagePresenter.hideMessages($messages); messagePresenter.hideMessages($messages);
var userName = $el.find('[name=user]').val(); registrationData = {
var password = $el.find('[name=password1]').val(); userName: $el.find('[name=user]').val(),
var passwordConfirmation = $el.find('[name=password2]').val(); password: $el.find('[name=password1]').val(),
var email = $el.find('[name=email]').val(); passwordConfirmation: $el.find('[name=password2]').val(),
email: $el.find('[name=email]').val(),
};
if (userName.length == 0) { validateRegistrationData(registrationData);
messagePresenter.showError($messages, 'User name cannot be empty.');
return;
}
if (password.length == 0) { api.post('/users', registrationData)
messagePresenter.showError($messages, 'Password cannot be empty.');
return;
}
if (password != passwordConfirmation) {
messagePresenter.showError($messages, 'Passwords must be the same.');
return;
}
api.post('/users', {userName: userName, password: password, email: email})
.then(function(response) { .then(function(response) {
//todo: show message about registration success eventHandlers.registrationSuccess(response);
//if it turned out that user needs to confirm his e-mail, notify about it
//also, show link to login
}).catch(function(response) { }).catch(function(response) {
messagePresenter.showError($messages, response.json && response.json.error || response); eventHandlers.registrationFailure(response);
}); });
} },
registrationSuccess: function(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);
});
},
registrationFailure: function(apiResponse) {
messagePresenter.showError($messages, apiResponse.json && apiResponse.json.error || apiResponse);
},
}; };
render(); render();
@ -59,6 +58,23 @@ App.Presenters.RegistrationPresenter = function(
$messages.width($el.find('form').width()); $messages.width($el.find('form').width());
}; };
function validateRegistrationData(registrationData) {
if (registrationData.userName.length == 0) {
messagePresenter.showError($messages, 'User name cannot be empty.');
return;
}
if (registrationData.password.length == 0) {
messagePresenter.showError($messages, 'Password cannot be empty.');
return;
}
if (registrationData.password != registrationData.passwordConfirmation) {
messagePresenter.showError($messages, 'Passwords must be the same.');
return;
}
};
return { return {
render: render, render: render,
}; };