All files / app/assets/javascripts/pages/admin/application_settings account_and_limits.js

86.84% Statements 33/38
80% Branches 12/15
87.5% Functions 7/8
89.18% Lines 33/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85    1x 1x         5x     5x 5x 1x 1x   4x 4x           4x 4x 4x 1x                   4x     4x   4x   4x       4x   4x 5x 5x       4x             4x 6x 6x 5x         4x   4x 4x     4x 4x       4x 4x    
import { __ } from '~/locale';
 
export const PLACEHOLDER_USER_EXTERNAL_DEFAULT_TRUE = __('Regex pattern');
export const PLACEHOLDER_USER_EXTERNAL_DEFAULT_FALSE = __(
  'To define internal users, first enable new users set to external',
);
 
function setUserInternalRegexPlaceholder(checkbox) {
  const userInternalRegex = document.getElementById(
    'application_setting_user_default_internal_regex',
  );
  Eif (checkbox && userInternalRegex) {
    if (checkbox.checked) {
      userInternalRegex.readOnly = false;
      userInternalRegex.placeholder = PLACEHOLDER_USER_EXTERNAL_DEFAULT_TRUE;
    } else {
      userInternalRegex.readOnly = true;
      userInternalRegex.placeholder = PLACEHOLDER_USER_EXTERNAL_DEFAULT_FALSE;
    }
  }
}
 
function initUserInternalRegexPlaceholder() {
  const checkbox = document.getElementById('application_setting_user_default_external');
  setUserInternalRegexPlaceholder(checkbox);
  checkbox.addEventListener('change', () => {
    setUserInternalRegexPlaceholder(checkbox);
  });
}
 
/**
 * Sets up logic inside "Dormant users" subsection:
 * - checkbox enables/disables additional input
 * - shows/hides an inline error on input validation
 */
function initDeactivateDormantUsersPeriodInputSection() {
  const DISPLAY_NONE_CLASS = 'gl-display-none';
 
  /** @type {HTMLInputElement} */
  const checkbox = document.getElementById('application_setting_deactivate_dormant_users');
  /** @type {HTMLInputElement} */
  const input = document.getElementById('application_setting_deactivate_dormant_users_period');
  /** @type {HTMLDivElement} */
  const errorLabel = document.getElementById(
    'application_setting_deactivate_dormant_users_period_error',
  );
 
  Iif (!checkbox || !input || !errorLabel) return;
 
  const hideInputErrorLabel = () => {
    Eif (input.checkValidity()) {
      errorLabel.classList.add(DISPLAY_NONE_CLASS);
    }
  };
 
  const handleInputInvalidState = (event) => {
    event.preventDefault();
    event.stopImmediatePropagation();
    errorLabel.classList.remove(DISPLAY_NONE_CLASS);
    return false;
  };
 
  const updateInputDisabledState = () => {
    input.disabled = !checkbox.checked;
    if (input.disabled) {
      hideInputErrorLabel();
    }
  };
 
  // Show error when input is invalid
  input.addEventListener('invalid', handleInputInvalidState);
  // Hide error when input changes
  input.addEventListener('input', hideInputErrorLabel);
  input.addEventListener('change', hideInputErrorLabel);
 
  // Handle checkbox change and set initial state
  checkbox.addEventListener('change', updateInputDisabledState);
  updateInputDisabledState();
}
 
export default function initAccountAndLimitsSection() {
  initUserInternalRegexPlaceholder();
  initDeactivateDormantUsersPeriodInputSection();
}