Passed
Push — master ( 17fae3...e5a7e5 )
by Jesús
02:17
created

validation.ts ➔ clearInputError   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
1
import { elements, resetBoxes, setStateFromIndex, state } from '../../bip39';
2
import { updateDisplay } from '../../display';
3
import { showToast } from '../../display';
4
import { currentTranslations } from '../../language';
5
import { getWordIndex, isWordInWordlist } from '../domain/wordInputHelpers';
6
7
let errorClearTimeout: NodeJS.Timeout | null = null;
8
9
export function validateWordInput(): void {
10
  const value = elements.wordInput.value.trim().toLowerCase();
11
12
  if (!value) {
13
    clearInputError();
14
    return;
15
  }
16
17
  const wordExists = isWordInWordlist(value, state.wordlist);
18
19
  if (wordExists) {
20
    handleValidWord(value);
21
  } else {
22
    handleInvalidWord();
23
  }
24
}
25
26
function clearInputError(): void {
27
  elements.wordInput.classList.remove('error');
28
}
29
30
function handleValidWord(value: string): void {
31
  clearInputError();
32
  const wordIndex = getWordIndex(value, state.wordlist);
33
  if (wordIndex !== -1) {
34
    setStateFromIndex(wordIndex);
35
    updateDisplay();
36
  }
37
}
38
39
function handleInvalidWord(): void {
40
  elements.wordInput.classList.add('error');
41
  resetBoxes();
42
  updateDisplay();
43
  showToast('invalid-word-toast', currentTranslations.invalidWordMessage);
44
  scheduleErrorAutoClear();
45
}
46
47
function scheduleErrorAutoClear(): void {
48
  if (errorClearTimeout) {
49
    clearTimeout(errorClearTimeout);
50
  }
51
  errorClearTimeout = setTimeout(() => {
52
    elements.wordInput.classList.remove('error');
53
  }, 3500);
54
}
55