szurubooru/public_html/js/Presenters/UserAccountRemovalPresenter.js

80 lines
2 KiB
JavaScript
Raw Normal View History

var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.UserAccountRemovalPresenter = function(
jQuery,
util,
promise,
api,
auth,
router,
messagePresenter) {
var target;
2014-10-05 10:41:12 +02:00
var templates = {};
var user;
var privileges = {};
2014-09-17 22:37:49 +02:00
function init(args, loaded) {
user = args.user;
target = args.target;
2014-09-17 22:37:49 +02:00
privileges.canDeleteAccount =
auth.hasPrivilege(auth.privileges.deleteAllAccounts) ||
(auth.hasPrivilege(auth.privileges.deleteOwnAccount) && auth.isLoggedIn(user.name));
2014-09-17 22:37:49 +02:00
promise.wait(util.promiseTemplate('account-removal'))
2014-10-05 10:41:12 +02:00
.then(function(template) {
templates.accountRemoval = template;
render();
2014-09-17 22:37:49 +02:00
loaded();
});
}
function render() {
2014-09-07 09:57:01 +02:00
var $el = jQuery(target);
2014-10-05 10:41:12 +02:00
$el.html(templates.accountRemoval({
user: user,
canDeleteAccount: privileges.canDeleteAccount}));
$el.find('form').submit(accountRemovalFormSubmitted);
}
function getPrivileges() {
return privileges;
}
function accountRemovalFormSubmitted(e) {
e.preventDefault();
2014-09-07 09:57:01 +02:00
var $el = jQuery(target);
2014-09-08 22:02:28 +02:00
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;
}
2014-10-02 00:30:25 +02:00
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);
});
}
function mainPageLinkClicked(e) {
e.preventDefault();
router.navigateToMainPage();
}
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);