2014-09-07 19:49:11 +02:00
|
|
|
var App = App || {};
|
|
|
|
App.Presenters = App.Presenters || {};
|
|
|
|
|
|
|
|
App.Presenters.UserActivationPresenter = function(
|
|
|
|
jQuery,
|
2014-09-08 13:06:32 +02:00
|
|
|
promise,
|
|
|
|
util,
|
|
|
|
auth,
|
|
|
|
api,
|
|
|
|
router,
|
|
|
|
topNavigationPresenter,
|
|
|
|
messagePresenter) {
|
2014-09-07 19:49:11 +02:00
|
|
|
|
|
|
|
var $el = jQuery('#content');
|
2014-09-08 13:06:32 +02:00
|
|
|
var $messages = $el;
|
|
|
|
var template;
|
|
|
|
var formHidden = false;
|
|
|
|
var operation;
|
2014-09-07 19:49:11 +02:00
|
|
|
|
|
|
|
function init(args) {
|
2014-09-08 13:06:32 +02:00
|
|
|
if (auth.isLoggedIn()) {
|
|
|
|
router.navigateToMainPage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-07 19:49:11 +02:00
|
|
|
topNavigationPresenter.select('login');
|
2014-09-08 13:06:32 +02:00
|
|
|
reinit(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
function reinit(args) {
|
|
|
|
operation = args.operation;
|
|
|
|
console.log(operation);
|
|
|
|
promise.wait(util.promiseTemplate('user-query-form')).then(function(html) {
|
|
|
|
template = _.template(html);
|
|
|
|
if (args.token) {
|
|
|
|
hideForm();
|
|
|
|
confirmToken(args.token);
|
|
|
|
} else {
|
|
|
|
showForm();
|
|
|
|
}
|
|
|
|
render();
|
|
|
|
});
|
2014-09-07 19:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function render() {
|
2014-09-08 13:06:32 +02:00
|
|
|
$el.html(template());
|
|
|
|
$messages = $el.find('.messages');
|
|
|
|
if (formHidden)
|
|
|
|
$el.find('form').hide();
|
|
|
|
$el.find('form').submit(userQueryFormSubmitted);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hideForm() {
|
|
|
|
formHidden = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function showForm() {
|
|
|
|
formHidden = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function userQueryFormSubmitted(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
messagePresenter.hideMessages($messages);
|
|
|
|
|
|
|
|
var userNameOrEmail = $el.find('form input[name=user]').val();
|
|
|
|
if (userNameOrEmail.length == 0) {
|
|
|
|
messagePresenter.showError($messages, 'Field cannot be blank.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var url = operation == 'passwordReset'
|
|
|
|
? '/password-reset/' + userNameOrEmail
|
|
|
|
: '/activation/' + userNameOrEmail;
|
|
|
|
|
|
|
|
api.post(url).then(function(response) {
|
|
|
|
var message = operation == 'passwordReset'
|
|
|
|
? 'Password reset request sent.'
|
|
|
|
: 'Activation e-mail resent.';
|
|
|
|
message += ' Check your inbox.<br/>If e-mail doesn\'t show up, check your spam folder.';
|
|
|
|
|
|
|
|
$el.find('#user-query-form').slideUp(function() {
|
|
|
|
messagePresenter.showInfo($messages, message);
|
|
|
|
});
|
|
|
|
}).fail(function(response) {
|
|
|
|
messagePresenter.showError($messages, response.json && response.json.error || response);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function confirmToken(token) {
|
|
|
|
messagePresenter.hideMessages($messages);
|
|
|
|
|
|
|
|
var url = operation == 'passwordReset'
|
|
|
|
? '/finish-password-reset/' + token
|
|
|
|
: '/finish-activation/' + token;
|
|
|
|
|
|
|
|
api.post(url).then(function(response) {
|
|
|
|
var message = operation == 'passwordReset'
|
|
|
|
? 'Your new password is <strong>' + response.json.newPassword + '</strong>.'
|
|
|
|
: 'E-mail activation successful.';
|
|
|
|
|
|
|
|
$el.find('#user-query-form').slideUp(function() {
|
|
|
|
messagePresenter.showInfo($messages, message);
|
|
|
|
});
|
|
|
|
}).fail(function(response) {
|
|
|
|
messagePresenter.showError($messages, response.json && response.json.error || response);
|
|
|
|
});
|
|
|
|
}
|
2014-09-07 19:49:11 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
init: init,
|
2014-09-08 13:06:32 +02:00
|
|
|
reinit: reinit,
|
2014-09-07 19:49:11 +02:00
|
|
|
render: render,
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
App.DI.register('userActivationPresenter', App.Presenters.UserActivationPresenter);
|