Completed
Push — master ( d231ed...aa59a9 )
by Alejandro
59s queued 56s
created

src/utils/helpers/hooks.js   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 27
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 46.15%

Importance

Changes 0
Metric Value
wmc 1
eloc 21
mnd 1
bc 1
fnc 0
dl 0
loc 27
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
ccs 6
cts 13
cp 0.4615
1
import { useState, useRef } from 'react';
2
3 6
const DEFAULT_DELAY = 2000;
4
5 6
export const useStateFlagTimeout = (setTimeout, clearTimeout) => (initialValue = false, delay = DEFAULT_DELAY) => {
6
  const [ flag, setFlag ] = useState(initialValue);
7
  const timeout = useRef(undefined);
8
  const callback = () => {
9
    setFlag(!initialValue);
10
11 2
    if (timeout.current) {
12
      clearTimeout(timeout.current);
13
    }
14
15
    timeout.current = setTimeout(() => setFlag(initialValue), delay);
16
  };
17
18
  return [ flag, callback ];
19
};
20
21
// Return [ flag, toggle, enable, disable ]
22 6
export const useToggle = (initialValue = false) => {
23 149
  const [ flag, setFlag ] = useState(initialValue);
24
25 149
  return [ flag, () => setFlag(!flag), () => setFlag(true), () => setFlag(false) ];
26
};
27