<commit_before>import Alert from '../../common/components/Alert';
import Button from '../../common/components/Button';
import Separator from '../../common/components/Separator';
import EditUserModal from '../components/EditUserModal';
import UserPage from '../components/UserPage';
import ItemList from '../../common/utils/ItemList';

/**
 * The `UserControls` utility constructs a list of buttons for a user which
 * perform actions on it.
 */
export default {
  /**
   * Get a list of controls for a user.
   *
   * @param {User} user
   * @param {*} context The parent component under which the controls menu will
   *     be displayed.
   * @return {ItemList}
   * @public
   */
  controls(user, context) {
    const items = new ItemList();

    ['user', 'moderation', 'destructive'].forEach(section => {
      const controls = this[section + 'Controls'](user, context).toArray();
      if (controls.length) {
        controls.forEach(item => items.add(item.itemName, item));
        items.add(section + 'Separator', Separator.component());
      }
    });

    return items;
  },

  /**
   * Get controls for a user pertaining to the current user (e.g. poke, follow).
   *
   * @param {User} user
   * @param {*} context The parent component under which the controls menu will
   *     be displayed.
   * @return {ItemList}
   * @protected
   */
  userControls() {
    return new ItemList();
  },

  /**
   * Get controls for a user pertaining to moderation (e.g. suspend, edit).
   *
   * @param {User} user
   * @param {*} context The parent component under which the controls menu will
   *     be displayed.
   * @return {ItemList}
   * @protected
   */
  moderationControls(user) {
    const items = new ItemList();

    if (user.canEdit()) {
      items.add('edit', Button.component({
        icon: 'fas fa-pencil-alt',
        children: app.translator.trans('core.forum.user_controls.edit_button'),
        onclick: this.editAction.bind(user)
      }));
    }

    return items;
  },

  /**
   * Get controls for a user which are destructive (e.g. delete).
   *
   * @param {User} user
   * @param {*} context The parent component under which the controls menu will
   *     be displayed.
   * @return {ItemList}
   * @protected
   */
  destructiveControls(user) {
    const items = new ItemList();

    if (user.id() !== '1' && user.canDelete()) {
      items.add('delete', Button.component({
        icon: 'fas fa-times',
        children: app.translator.trans('core.forum.user_controls.delete_button'),
        onclick: this.deleteAction.bind(user)
      }));
    }

    return items;
  },

  /**
   * Delete the user.
   */
  deleteAction() {
    if (!confirm(app.translator.trans('core.forum.user_controls.delete_confirmation'))) {
      return;
    }

    this.delete().then(() => {
      this.showDeletionAlert('success');
      if (app.current instanceof UserPage && app.current.user === this) {
        app.history.back();
      } else {
        window.location.reload();
      }
    }).catch(() => this.showDeletionAlert('error'));
  },

  showDeletionAlert(type) {
    const { username, email } = this.data.attributes;
    const message = {
      success: 'core.forum.user_controls.delete_success_message',
      error: 'core.forum.user_controls.delete_error_message',
    }[type];

    app.alerts.show(new Alert({
      type,
      children: app.translator.trans(
        message, { username, email }
      )
    }));
  },

  /**
   * Edit the user.
   */
  editAction() {
    app.modal.show(new EditUserModal({user: this}));
  }
};<commit_msg>Fix errors caused by deletion alert when deleting users (#1883)<commit_after>import Alert from '../../common/components/Alert';
import Button from '../../common/components/Button';
import Separator from '../../common/components/Separator';
import EditUserModal from '../components/EditUserModal';
import UserPage from '../components/UserPage';
import ItemList from '../../common/utils/ItemList';

/**
 * The `UserControls` utility constructs a list of buttons for a user which
 * perform actions on it.
 */
export default {
  /**
   * Get a list of controls for a user.
   *
   * @param {User} user
   * @param {*} context The parent component under which the controls menu will
   *     be displayed.
   * @return {ItemList}
   * @public
   */
  controls(user, context) {
    const items = new ItemList();

    ['user', 'moderation', 'destructive'].forEach(section => {
      const controls = this[section + 'Controls'](user, context).toArray();
      if (controls.length) {
        controls.forEach(item => items.add(item.itemName, item));
        items.add(section + 'Separator', Separator.component());
      }
    });

    return items;
  },

  /**
   * Get controls for a user pertaining to the current user (e.g. poke, follow).
   *
   * @param {User} user
   * @param {*} context The parent component under which the controls menu will
   *     be displayed.
   * @return {ItemList}
   * @protected
   */
  userControls() {
    return new ItemList();
  },

  /**
   * Get controls for a user pertaining to moderation (e.g. suspend, edit).
   *
   * @param {User} user
   * @param {*} context The parent component under which the controls menu will
   *     be displayed.
   * @return {ItemList}
   * @protected
   */
  moderationControls(user) {
    const items = new ItemList();

    if (user.canEdit()) {
      items.add('edit', Button.component({
        icon: 'fas fa-pencil-alt',
        children: app.translator.trans('core.forum.user_controls.edit_button'),
        onclick: this.editAction.bind(this, user)
      }));
    }

    return items;
  },

  /**
   * Get controls for a user which are destructive (e.g. delete).
   *
   * @param {User} user
   * @param {*} context The parent component under which the controls menu will
   *     be displayed.
   * @return {ItemList}
   * @protected
   */
  destructiveControls(user) {
    const items = new ItemList();

    if (user.id() !== '1' && user.canDelete()) {
      items.add('delete', Button.component({
        icon: 'fas fa-times',
        children: app.translator.trans('core.forum.user_controls.delete_button'),
        onclick: this.deleteAction.bind(this, user)
      }));
    }

    return items;
  },

  /**
   * Delete the user.
   *
   * @param {User} user
   */
  deleteAction(user) {
    if (!confirm(app.translator.trans('core.forum.user_controls.delete_confirmation'))) {
      return;
    }

    user.delete().then(() => {
      this.showDeletionAlert(user, 'success');
      if (app.current instanceof UserPage && app.current.user === user) {
        app.history.back();
      } else {
        window.location.reload();
      }
    }).catch(() => this.showDeletionAlert(user, 'error'));
  },

  /**
   * Show deletion alert of user.
   *
   * @param {User} user
   * @param {string} type
   */
  showDeletionAlert(user, type) {
    const { username, email } = user.data.attributes;
    const message = {
      success: 'core.forum.user_controls.delete_success_message',
      error: 'core.forum.user_controls.delete_error_message',
    }[type];

    app.alerts.show(new Alert({
      type,
      children: app.translator.trans(
        message, { username, email }
      )
    }));
  },

  /**
   * Edit the user.
   *
   * @param {User} user
   */
  editAction(user) {
    app.modal.show(new EditUserModal({ user }));
  }
};
{'fraction_non_alphanumeric': 0.148665130776528, 'fraction_numerical': 0.0008131183087139179, 'mean_word_length': 2.640848544647262, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4313236', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2211, 'n_words': 577}