Passed
Push — task/security-fixes ( 62effe...fd3b60 )
by Grant
05:50
created

resources/assets/js/managerRegistration.ts   A

Complexity

Total Complexity 8
Complexity/F 0

Size

Lines of Code 49
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 37
mnd 8
bc 8
fnc 0
dl 0
loc 49
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
const getDeptInput = (): HTMLSelectElement | null =>
2
  document.querySelector("select#department");
3
const getGovEmailWrapper = () => document.querySelector("#gov_email_wrapper");
4
const getGovEmailInput = (): HTMLInputElement | null =>
5
  document.querySelector("input#gov_email");
6
7
const hideGovEmail = (): void => {
8
  const wrapper = getGovEmailWrapper();
9
  if (wrapper !== null) {
10
    wrapper.setAttribute("data-c-visibility", "hidden");
11
  }
12
  const input = getGovEmailInput();
13
  if (input !== null) {
14
    input.removeAttribute("required");
15
  }
16
};
17
18
const showGovEmail = (): void => {
19
  const wrapper = getGovEmailWrapper();
20
  if (wrapper !== null) {
21
    wrapper.removeAttribute("data-c-visibility");
22
  }
23
  const input = getGovEmailInput();
24
  if (input !== null) {
25
    input.setAttribute("required", "");
26
  }
27
};
28
29
const handleDeptChange = (event: Event): void => {
30
  if (event.target instanceof HTMLSelectElement) {
31
    const { value } = event.target;
32
    const NOT_IN_GOV = "0";
33
    const govEmailInput = getGovEmailInput();
34
    if (value !== null && value !== "" && value !== NOT_IN_GOV) {
35
      showGovEmail();
36
    } else {
37
      hideGovEmail();
38
      if (govEmailInput !== null) {
39
        govEmailInput.value = "";
40
      }
41
    }
42
  }
43
};
44
45
const deptInput = getDeptInput();
46
if (deptInput !== null) {
47
  deptInput.addEventListener("change", handleDeptChange);
48
}
49