szurubooru/public_html/js/Presenters/UserActivationPresenter.js

117 lines
2.9 KiB
JavaScript
Raw Normal View History

2014-09-07 19:49:11 +02:00
var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.UserActivationPresenter = function(
jQuery,
promise,
util,
auth,
api,
router,
topNavigationPresenter,
messagePresenter) {
2014-09-07 19:49:11 +02:00
var $el = jQuery('#content');
var $messages = $el;
2014-10-05 10:41:12 +02:00
var templates = {};
var formHidden = false;
var operation;
2014-09-07 19:49:11 +02:00
2014-09-17 22:37:49 +02:00
function init(args, loaded) {
2014-09-07 19:49:11 +02:00
topNavigationPresenter.select('login');
topNavigationPresenter.changeTitle('Account recovery');
2014-09-17 22:37:49 +02:00
reinit(args, loaded);
}
2014-09-17 22:37:49 +02:00
function reinit(args, loaded) {
operation = args.operation;
2014-09-17 22:37:49 +02:00
promise.wait(util.promiseTemplate('user-query-form'))
2014-10-05 10:41:12 +02:00
.then(function(template) {
templates.userQuery = template;
2014-09-17 22:37:49 +02:00
if (args.token) {
hideForm();
confirmToken(args.token);
} else {
showForm();
}
render();
loaded();
});
2014-09-07 19:49:11 +02:00
}
function render() {
2014-10-05 10:41:12 +02:00
$el.html(templates.userQuery());
$messages = $el.find('.messages');
2014-09-08 22:02:28 +02:00
if (formHidden) {
$el.find('form').hide();
2014-09-08 22:02:28 +02:00
}
$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();
2014-09-08 22:02:28 +02:00
if (userNameOrEmail.length === 0) {
messagePresenter.showError($messages, 'Field cannot be blank.');
return;
}
2014-09-08 22:02:28 +02:00
var url = operation === 'passwordReset' ?
'/password-reset/' + userNameOrEmail :
'/activation/' + userNameOrEmail;
2014-10-02 00:30:25 +02:00
promise.wait(api.post(url))
2014-09-17 22:37:49 +02:00
.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);
2014-09-08 22:02:28 +02:00
var url = operation === 'passwordReset' ?
'/finish-password-reset/' + token :
'/finish-activation/' + token;
2014-10-02 00:30:25 +02:00
promise.wait(api.post(url))
2014-09-17 22:37:49 +02:00
.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,
reinit: reinit,
2014-09-07 19:49:11 +02:00
render: render,
};
};
2014-10-05 10:41:12 +02:00
App.DI.register('userActivationPresenter', ['jQuery', 'promise', 'util', 'auth', 'api', 'router', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.UserActivationPresenter);