szurubooru/public_html/js/Presenters/UserAccountRemovalPresenter.js

83 lines
2.4 KiB
JavaScript
Raw Normal View History

var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.UserAccountRemovalPresenter = function(
2015-06-28 10:07:11 +02:00
jQuery,
util,
promise,
api,
auth,
router,
messagePresenter) {
2015-06-28 10:07:11 +02:00
var target;
var templates = {};
var user;
var privileges = {};
2015-06-28 10:07:11 +02:00
function init(params, loaded) {
user = params.user;
target = params.target;
2015-06-28 10:07:11 +02:00
privileges.canDeleteAccount =
auth.hasPrivilege(auth.privileges.deleteAllAccounts) ||
(auth.hasPrivilege(auth.privileges.deleteOwnAccount) && auth.isLoggedIn(user.name));
2015-06-28 10:07:11 +02:00
promise.wait(util.promiseTemplate('account-removal'))
.then(function(template) {
templates.accountRemoval = template;
render();
loaded();
}).fail(function() {
console.log(arguments);
loaded();
});
}
2015-06-28 10:07:11 +02:00
function render() {
var $el = jQuery(target);
$el.html(templates.accountRemoval({
user: user,
canDeleteAccount: privileges.canDeleteAccount}));
2015-06-28 10:07:11 +02:00
$el.find('form').submit(accountRemovalFormSubmitted);
}
2015-06-28 10:07:11 +02:00
function getPrivileges() {
return privileges;
}
2015-06-28 10:07:11 +02:00
function accountRemovalFormSubmitted(e) {
e.preventDefault();
var $el = jQuery(target);
var $messages = $el.find('.messages');
messagePresenter.hideMessages($messages);
if (!$el.find('input[name=confirmation]:visible').prop('checked')) {
messagePresenter.showError($messages, 'Must confirm to proceed.');
return;
}
promise.wait(api.delete('/users/' + user.name))
.then(function() {
auth.logout();
var $messageDiv = messagePresenter.showInfo($messages, 'Account deleted. <a href="">Back to main page</a>');
$messageDiv.find('a').click(mainPageLinkClicked);
}).fail(function(response) {
messagePresenter.showError($messages, response.json && response.json.error || response);
});
}
2015-06-28 10:07:11 +02:00
function mainPageLinkClicked(e) {
e.preventDefault();
router.navigateToMainPage();
}
2015-06-28 10:07:11 +02:00
return {
init: init,
render: render,
getPrivileges: getPrivileges
};
2014-09-08 22:02:28 +02:00
};
2014-10-05 10:41:12 +02:00
App.DI.register('userAccountRemovalPresenter', ['jQuery', 'util', 'promise', 'api', 'auth', 'router', 'messagePresenter'], App.Presenters.UserAccountRemovalPresenter);