Passed
Push — task/job-get-classification ( 8819e7...eb11bc )
by
unknown
05:34
created

resources/assets/js/components/ApplicantSkills/SkillsWordCounter.tsx   A

Complexity

Total Complexity 4
Complexity/F 0

Size

Lines of Code 97
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 72
c 0
b 0
f 0
dl 0
loc 97
rs 10
mnd 4
bc 4
fnc 0
bpm 0
cpm 0
noi 0
1
/* eslint-disable @typescript-eslint/camelcase */
2
import * as React from "react";
3
import ReactDOM from "react-dom";
4
import { defineMessages, useIntl } from "react-intl";
5
import IntlContainer from "../../IntlContainer";
6
import WordCounter, { WordCounterProps } from "../WordCounter/WordCounter";
7
8
type SkillsWordCounterProps = WordCounterProps;
9
10
const messages = defineMessages({
11
  underLimit: {
12
    id: "skillWordCounter.wordsLeft",
13
    defaultMessage: "words left",
14
    description:
15
      "Message displayed on word counter when users under/matching the limit.",
16
  },
17
  overLimit: {
18
    id: "skillWordCounter.afterText",
19
    defaultMessage: "words over limit",
20
    description:
21
      "Message displayed on word counter when user passes the limit.",
22
  },
23
});
24
25
const SkillsWordCounter: React.FunctionComponent<SkillsWordCounterProps> = ({
26
  elementId,
27
  minWords,
28
  maxWords,
29
}): React.ReactElement => {
30
  const intl = useIntl();
31
  return (
32
    <WordCounter
33
      elementId={elementId}
34
      minWords={minWords}
35
      maxWords={maxWords}
36
      absoluteValue
37
      beforeText="( "
38
      underMaxMessage={`${intl.formatMessage(messages.underLimit)} )`}
39
      overMaxMessage={`${intl.formatMessage(messages.overLimit)} )`}
40
    />
41
  );
42
};
43
44
const addSoftSkillButton: HTMLElement | null = document.getElementById(
45
  "add-soft-skill",
46
);
47
const addHardSkillButton: HTMLElement | null = document.getElementById(
48
  "add-hard-skill",
49
);
50
51
const updateWordCounters = (): void => {
52
  if (addSoftSkillButton) {
53
    addSoftSkillButton.addEventListener("click", () => {
54
      setTimeout(updateWordCounters, 1000);
55
    });
56
  }
57
58
  if (addHardSkillButton) {
59
    addHardSkillButton.addEventListener("click", () => {
60
      setTimeout(updateWordCounters, 1000);
61
    });
62
  }
63
64
  // Find all skills textarea elements
65
  if (document.querySelectorAll("span[data-word-counter-id]")) {
66
    const wordCounters = document.querySelectorAll(
67
      "span[data-word-counter-id]",
68
    );
69
70
    wordCounters.forEach((wordCounter): void => {
71
      if (
72
        wordCounter !== null &&
73
        wordCounter.hasAttribute("data-word-counter-id")
74
      ) {
75
        const elementId = String(wordCounter.id.replace("wordCounter", ""));
76
        const maxWords = Number(wordCounter.getAttribute("data-max-words"));
77
        const minWords = Number(wordCounter.getAttribute("data-min-words"));
78
        const locale = document.documentElement.lang;
79
        ReactDOM.render(
80
          <IntlContainer locale={locale}>
81
            <SkillsWordCounter
82
              elementId={elementId}
83
              maxWords={maxWords}
84
              minWords={minWords}
85
            />
86
          </IntlContainer>,
87
          wordCounter,
88
        );
89
      }
90
    });
91
  }
92
};
93
94
updateWordCounters();
95
96
export default SkillsWordCounter;
97