Updated stateFlagTimeout to get the setTimeout function injected as a dependency

This commit is contained in:
Alejandro Celaya
2019-01-08 20:49:47 +01:00
parent 00d386f19f
commit 8b8be2d7ca
8 changed files with 26 additions and 13 deletions

View File

@@ -1,4 +1,5 @@
import axios from 'axios';
import { stateFlagTimeout } from '../utils';
import Storage from './Storage';
import ColorGenerator from './ColorGenerator';
import buildShlinkApiClient from './ShlinkApiClientBuilder';
@@ -10,6 +11,9 @@ const provideServices = (bottle) => {
bottle.constant('axios', axios);
bottle.serviceFactory('buildShlinkApiClient', buildShlinkApiClient, 'axios');
bottle.constant('setTimeout', global.setTimeout);
bottle.serviceFactory('stateFlagTimeout', stateFlagTimeout, 'setTimeout');
};
export default provideServices;

View File

@@ -5,7 +5,12 @@ import markerShadow from 'leaflet/dist/images/marker-shadow.png';
const DEFAULT_TIMEOUT_DELAY = 2000;
export const stateFlagTimeout = (setState, flagName, initialValue = true, delay = DEFAULT_TIMEOUT_DELAY) => {
export const stateFlagTimeout = (setTimeout) => (
setState,
flagName,
initialValue = true,
delay = DEFAULT_TIMEOUT_DELAY
) => {
setState({ [flagName]: initialValue });
setTimeout(() => setState({ [flagName]: !initialValue }), delay);
};