<commit_before>import Rails from '@rails/ujs';

import I18n from 'retrospring/i18n';
import { showNotification, showErrorNotification } from 'utilities/notifications';

export function banCheckboxHandler(event: Event): void {
  const checkbox = event.target as HTMLInputElement;

  if (checkbox.checked) {
    document.querySelector('#ban-controls').classList.remove('d-none');
  } else {
    document.querySelector('#ban-controls').classList.add('d-none');
  }
}

export function permanentBanCheckboxHandler(event: Event): void {
  const checkbox = event.target as HTMLInputElement;

  if (checkbox.checked) {
    document.querySelector('#ban-controls-time').classList.add('d-none');
  } else {
    document.querySelector('#ban-controls-time').classList.remove('d-none');
  }
}

export function banFormHandler(event: Event): void {
  const form = event.target as HTMLFormElement;
  const checkbox = document.querySelector<HTMLInputElement>('[name="ban"][type="checkbox"]');
  const permaCheckbox = document.querySelector<HTMLInputElement>('[name="permaban"][type="checkbox"]');
  event.preventDefault();

  const data = {
    ban: '0',
    user: form.elements['user'].value,
  };

  if (checkbox && checkbox.checked) {
    data['ban'] = '1';
    data['reason'] = form.elements['reason'].value;

    if (!permaCheckbox.checked) {
      data['duration'] = form.elements['duration'].value.trim();
      data['duration_unit'] = form.elements['duration_unit'].value.trim();
    }
  }

  Rails.ajax({
    url: '/ajax/mod/ban',
    type: 'POST',
    data: new URLSearchParams(data).toString(),
    success: (data) => {
      showNotification(data.message, data.success);
    },
    error: (data, status, xhr) => {
      console.log(data, status, xhr);
      showErrorNotification(I18n.translate('frontend.error.message'));
    }
  });
}<commit_msg>Refactor user banning to use request.js<commit_after>import { post } from '@rails/request.js';

import I18n from 'retrospring/i18n';
import { showNotification, showErrorNotification } from 'utilities/notifications';

export function banCheckboxHandler(event: Event): void {
  const checkbox = event.target as HTMLInputElement;

  if (checkbox.checked) {
    document.querySelector('#ban-controls').classList.remove('d-none');
  } else {
    document.querySelector('#ban-controls').classList.add('d-none');
  }
}

export function permanentBanCheckboxHandler(event: Event): void {
  const checkbox = event.target as HTMLInputElement;

  if (checkbox.checked) {
    document.querySelector('#ban-controls-time').classList.add('d-none');
  } else {
    document.querySelector('#ban-controls-time').classList.remove('d-none');
  }
}

export function banFormHandler(event: Event): void {
  const form = event.target as HTMLFormElement;
  const checkbox = document.querySelector<HTMLInputElement>('[name="ban"][type="checkbox"]');
  const permaCheckbox = document.querySelector<HTMLInputElement>('[name="permaban"][type="checkbox"]');
  event.preventDefault();

  const data = {
    ban: '0',
    user: form.elements['user'].value,
  };

  if (checkbox && checkbox.checked) {
    data['ban'] = '1';
    data['reason'] = form.elements['reason'].value;

    if (!permaCheckbox.checked) {
      data['duration'] = form.elements['duration'].value.trim();
      data['duration_unit'] = form.elements['duration_unit'].value.trim();
    }
  }

  post('/ajax/mod/ban', {
    body: data,
    contentType: 'application/json'
  })
    .then(async response => {
      const data = await response.json;

      showNotification(data.message, data.success);
    })
    .catch(err => {
      console.log(err);
      showErrorNotification(I18n.translate('frontend.error.message'));
    });
}
{'fraction_non_alphanumeric': 0.16370010787486516, 'fraction_numerical': 0.004314994606256742, 'mean_word_length': 4.383164005805515, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1081868', 'n_tokens_mistral': 1125, 'n_tokens_neox': 1112, 'n_words': 199}