<commit_before>import Rails from '@rails/ujs';
import { showNotification, showErrorNotification } from 'utilities/notifications';
import I18n from 'retrospring/i18n';

export function userActionHandler(event: Event): void {
  event.preventDefault();
  const button: HTMLButtonElement = event.target as HTMLButtonElement;
  const target = button.dataset.target;
  const action = button.dataset.action;

  let targetURL, relationshipType;

  switch (action) {
    case 'follow':
      targetURL = '/ajax/create_relationship';
      relationshipType = 'follow';
      break;
    case 'unfollow':
      targetURL = '/ajax/destroy_relationship';
      relationshipType = 'follow';
      break;
    case 'block':
      targetURL = '/ajax/create_relationship';
      relationshipType = 'block';
      break;
    case 'unblock':
      targetURL = '/ajax/destroy_relationship';
      relationshipType = 'block';
      break;
  }
  let success = false;

  Rails.ajax({
    url: targetURL,
    type: 'POST',
    data: new URLSearchParams({
      screen_name: target,
      type: relationshipType,
    }).toString(),
    success: (data) => {
      success = data.success;
      showNotification(data.message, data.success);
    },
    error: (data, status, xhr) => {
      console.log(data, status, xhr);
      showErrorNotification(I18n.translate('frontend.error.message'));
    },
    complete: () => {
      if (!success) return;

      switch (action) {
        case 'follow':
          button.dataset.action = 'unfollow';
          button.innerText = I18n.translate('views.actions.unfollow');
          button.classList.remove('btn-primary');
          button.classList.add('btn-default');
          break;
        case 'unfollow':
          button.dataset.action = 'follow';
          button.innerText = I18n.translate('views.actions.follow');
          button.classList.remove('btn-default');
          button.classList.add('btn-primary');
          break;
        case 'block':
          button.dataset.action = 'unblock';
          button.querySelector('span').innerText = I18n.translate('views.actions.unblock');
          button.classList.remove('btn-primary');
          button.classList.add('btn-default');
          break;
        case 'unblock':
          button.dataset.action = 'block';
          button.querySelector('span').innerText = I18n.translate('views.actions.block');
          button.classList.remove('btn-default');
          button.classList.add('btn-primary');
          break;
      }
     }
  });
}<commit_msg>Reset follow button after blocking a user<commit_after>import Rails from '@rails/ujs';
import { showNotification, showErrorNotification } from 'utilities/notifications';
import I18n from 'retrospring/i18n';

export function userActionHandler(event: Event): void {
  event.preventDefault();
  const button: HTMLButtonElement = event.target as HTMLButtonElement;
  const target = button.dataset.target;
  const action = button.dataset.action;

  let targetURL, relationshipType;

  switch (action) {
    case 'follow':
      targetURL = '/ajax/create_relationship';
      relationshipType = 'follow';
      break;
    case 'unfollow':
      targetURL = '/ajax/destroy_relationship';
      relationshipType = 'follow';
      break;
    case 'block':
      targetURL = '/ajax/create_relationship';
      relationshipType = 'block';
      break;
    case 'unblock':
      targetURL = '/ajax/destroy_relationship';
      relationshipType = 'block';
      break;
  }
  let success = false;

  Rails.ajax({
    url: targetURL,
    type: 'POST',
    data: new URLSearchParams({
      screen_name: target,
      type: relationshipType,
    }).toString(),
    success: (data) => {
      success = data.success;
      showNotification(data.message, data.success);
    },
    error: (data, status, xhr) => {
      console.log(data, status, xhr);
      showErrorNotification(I18n.translate('frontend.error.message'));
    },
    complete: () => {
      if (!success) return;

      switch (action) {
        case 'follow':
          button.dataset.action = 'unfollow';
          button.innerText = I18n.translate('views.actions.unfollow');
          button.classList.remove('btn-primary');
          button.classList.add('btn-default');
          break;
        case 'unfollow':
          resetFollowButton(button);
          break;
        case 'block':
          button.dataset.action = 'unblock';
          button.querySelector('span').innerText = I18n.translate('views.actions.unblock');
          button.classList.remove('btn-primary');
          button.classList.add('btn-default');
          resetFollowButton(document.querySelector<HTMLButtonElement>('button[data-action="unfollow"]'));
          break;
        case 'unblock':
          button.dataset.action = 'block';
          button.querySelector('span').innerText = I18n.translate('views.actions.block');
          button.classList.remove('btn-default');
          button.classList.add('btn-primary');
          break;
      }
     }
  });
}

function resetFollowButton(button: HTMLButtonElement) {
  button.dataset.action = 'follow';
  button.innerText = I18n.translate('views.actions.follow');
  button.classList.remove('btn-default');
  button.classList.add('btn-primary');
}
{'fraction_non_alphanumeric': 0.13465195891974135, 'fraction_numerical': 0.005325218714340053, 'mean_word_length': 2.791636625811103, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 10, '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': '929002', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1461, 'n_words': 283}