summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/confirm_danger_modal.js
blob: 1000c310e35eba7ff80f7ae613178e601d29d5b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import $ from 'jquery';
import { rstrip } from './lib/utils/common_utils';

function openConfirmDangerModal($form, text) {
  const $input = $('.js-confirm-danger-input');
  $input.val('');

  $('.js-confirm-text').text(text || '');
  $('#modal-confirm-danger').modal('show');

  const confirmTextMatch = $('.js-confirm-danger-match').text();
  const $submit = $('.js-confirm-danger-submit');
  $submit.disable();
  $input.focus();

  $('.js-confirm-danger-input')
    .off('input')
    .on('input', function handleInput() {
      const confirmText = rstrip($(this).val());
      if (confirmText === confirmTextMatch) {
        $submit.enable();
      } else {
        $submit.disable();
      }
    });
  $('.js-confirm-danger-submit')
    .off('click')
    .on('click', () => $form.submit());
}

export default function initConfirmDangerModal() {
  $(document).on('click', '.js-confirm-danger', e => {
    e.preventDefault();
    const $btn = $(e.target);
    const $form = $btn.closest('form');
    const text = $btn.data('confirmDangerMessage');
    openConfirmDangerModal($form, text);
  });
}