3 var Marionette = require('backbone.marionette');
4 var log = require('../../utils/log');
5 var context = require('gitter-web-client-context');
6 var appEvents = require('../../utils/appevents');
7 var ModalView = require('./modal');
8 var apiClient = require('../../components/api-client');
9 var DelayLock = require('../../models/delay-lock-model');
10 var template = require('./tmpl/delete-account-view.hbs');
12 var View = Marionette.ItemView.extend({
14 attributes: { style: '' },
16 ghostUserCheckbox: '.js-delete-account-ghost-user-checkbox'
19 'input @ui.ghostUserCheckbox': 'onGhostCheckboxChanged'
24 initialize: function() {
25 this.model.set('ghostUsername', `ghost~${context.getUserId()}`);
28 onGhostCheckboxChanged: function() {
29 this.model.set('ghost', this.ui.ghostUserCheckbox.is(':checked'));
33 var Modal = ModalView.extend({
34 initialize: function(options) {
35 options = options || {};
36 options.title = 'Careful Now...';
37 var username = context.user().get('username');
42 text: `Delete ${username}`,
43 className: 'modal--default__footer__btn--negative'
47 this.lockModel = new DelayLock();
49 this.listenTo(this.lockModel, 'change:locked', function() {
50 this.setButtonState('delete', true);
53 ModalView.prototype.initialize.call(this, options);
54 this.view = new View({
58 this.listenTo(this, 'menuItemClicked', this.menuItemClicked);
60 menuItemClicked: function(button) {
63 // Notify others, that they shouldn't redirect while we are trying to logout
64 appEvents.trigger('account.delete-start');
66 // Disable the button so they can't click it again
67 this.setButtonState('delete', false);
69 this.lockModel.set('deletionRequestLoading', true);
70 this.lockModel.set('deletionRequestSucceeded', false);
71 this.lockModel.set('deletionRequestError', undefined);
75 ghost: this.lockModel.get('ghost') || false
78 this.lockModel.set('deletionRequestSucceeded', true);
79 // Redirect back to the homepage
80 window.location.href = '/';
83 log.error('Error while deleting account', { exception: err });
85 if (err.statusText === 'timeout') {
87 'deletionRequestError',
88 `The request timed out but your account is probably still in the process of being deleted on our end (especially if you joined a lot of rooms). Please wait a couple hours before trying again : ${err} (status: ${err.status})`
92 'deletionRequestError',
93 `Error while deleting account: ${err} (status: ${err.status})`
97 // We only trigger a stop on error as they could decide to not delete
98 // their account after they see an error and continue using the app
100 // Otherwise, the delete flow redirect should continue
101 appEvents.trigger('account.delete-stop');
104 this.lockModel.set('deletionRequestLoading', false);
115 module.exports = Modal;