mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-03 06:47:29 +03:00
Use test seams instead of DI in useTimeoutToggle
This commit is contained in:
parent
6926afbac1
commit
7a7884f38d
3 changed files with 13 additions and 14 deletions
|
@ -6,8 +6,5 @@
|
|||
"parserOptions": {
|
||||
"project": "./tsconfig.json"
|
||||
},
|
||||
"ignorePatterns": ["src/service*.ts"],
|
||||
"rules": {
|
||||
"react-hooks/rules-of-hooks": "off"
|
||||
}
|
||||
"ignorePatterns": ["src/service*.ts"]
|
||||
}
|
||||
|
|
|
@ -1,18 +1,22 @@
|
|||
import { parseQuery } from '@shlinkio/shlink-frontend-kit';
|
||||
import { useRef, useState } from 'react';
|
||||
import { useCallback, useRef, useState } from 'react';
|
||||
import { useLocation, useNavigate } from 'react-router-dom';
|
||||
|
||||
const DEFAULT_DELAY = 2000;
|
||||
|
||||
export type TimeoutToggle = (initialValue?: boolean, delay?: number) => [boolean, () => void];
|
||||
export type TimeoutToggle = typeof useTimeoutToggle;
|
||||
|
||||
export const useTimeoutToggle = (
|
||||
setTimeout: (callback: Function, timeout: number) => number,
|
||||
clearTimeout: (timer: number) => void,
|
||||
): TimeoutToggle => (initialValue = false, delay = DEFAULT_DELAY) => {
|
||||
initialValue = false,
|
||||
delay = DEFAULT_DELAY,
|
||||
|
||||
// Test seams
|
||||
setTimeout = window.setTimeout,
|
||||
clearTimeout = window.clearTimeout,
|
||||
): [boolean, () => void] => {
|
||||
const [flag, setFlag] = useState<boolean>(initialValue);
|
||||
const timeout = useRef<number | undefined>(undefined);
|
||||
const callback = () => {
|
||||
const callback = useCallback(() => {
|
||||
setFlag(!initialValue);
|
||||
|
||||
if (timeout.current) {
|
||||
|
@ -20,7 +24,7 @@ export const useTimeoutToggle = (
|
|||
}
|
||||
|
||||
timeout.current = setTimeout(() => setFlag(initialValue), delay);
|
||||
};
|
||||
}, [clearTimeout, delay, initialValue, setTimeout]);
|
||||
|
||||
return [flag, callback];
|
||||
};
|
||||
|
|
|
@ -12,7 +12,5 @@ export const provideServices = (bottle: Bottle) => {
|
|||
bottle.constant('csvToJson', csvToJson);
|
||||
bottle.constant('jsonToCsv', jsonToCsv);
|
||||
|
||||
bottle.constant('setTimeout', window.setTimeout);
|
||||
bottle.constant('clearTimeout', window.clearTimeout);
|
||||
bottle.serviceFactory('useTimeoutToggle', useTimeoutToggle, 'setTimeout', 'clearTimeout');
|
||||
bottle.serviceFactory('useTimeoutToggle', () => useTimeoutToggle);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue