Passed
Push — feature/experience-form-modals ( 7402c2...37703b )
by Tristan
03:49
created

resources/assets/js/managerRegistration.ts   A

Complexity

Total Complexity 7
Complexity/F 0

Size

Lines of Code 45
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 7
mnd 7
bc 7
fnc 0
bpm 0
cpm 0
noi 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
    if (value !== null && value !== "" && value !== NOT_IN_GOV) {
34
      showGovEmail();
35
    } else {
36
      hideGovEmail();
37
    }
38
  }
39
};
40
41
const deptInput = getDeptInput();
42
if (deptInput !== null) {
43
  deptInput.addEventListener("change", handleDeptChange);
44
}
45