From 2e68e56e44ef1617b1226d8a2a6727fb0f01f097 Mon Sep 17 00:00:00 2001 From: Bogdan Bodnar Date: Fri, 15 Nov 2019 12:54:12 +0100 Subject: [PATCH 1/3] Add hook for API polling. --- src/api/hooks.js | 24 +++++++++++++++++++++++- src/index.js | 7 ++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/api/hooks.js b/src/api/hooks.js index da236d0..f3f5746 100644 --- a/src/api/hooks.js +++ b/src/api/hooks.js @@ -5,7 +5,9 @@ * See /LICENSE for more information. */ -import { useCallback, useReducer } from "react"; +import { + useCallback, useEffect, useReducer, useState, +} from "react"; import { ForisURLs } from "forisUrls"; import { @@ -90,3 +92,23 @@ const useAPIDelete = createAPIHook("DELETE"); export { useAPIGet, useAPIPost, useAPIPatch, useAPIPut, useAPIDelete, }; + +export function useAPIPolling(endpoint, delay) { + const [state, setState] = useState({ state: API_STATE.INIT }); + const [getState, get] = useAPIGet(endpoint); + + useEffect(() => { + if (getState.state === API_STATE.SUCCESS) { + setState(getState); + } + }, [getState]); + + useEffect(() => { + if (delay !== null) { + const interval = setInterval(get, delay); + return () => clearInterval(interval); + } + }, [delay, get]); + + return [state]; +} diff --git a/src/index.js b/src/index.js index 5f755d9..9a51089 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,12 @@ // API export { - useAPIGet, useAPIPost, useAPIPatch, useAPIPut, useAPIDelete, + useAPIGet, + useAPIPost, + useAPIPatch, + useAPIPut, + useAPIDelete, + useAPIPolling, } from "api/hooks"; export { API_STATE } from "api/utils"; From 73e213c46788be024823afa90df88ecc444a028e Mon Sep 17 00:00:00 2001 From: Bogdan Bodnar Date: Fri, 15 Nov 2019 14:00:07 +0100 Subject: [PATCH 2/3] Add default delay. --- src/api/hooks.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/api/hooks.js b/src/api/hooks.js index f3f5746..e873ef7 100644 --- a/src/api/hooks.js +++ b/src/api/hooks.js @@ -5,14 +5,10 @@ * See /LICENSE for more information. */ -import { - useCallback, useEffect, useReducer, useState, -} from "react"; +import { useCallback, useEffect, useReducer, useState, } from "react"; import { ForisURLs } from "forisUrls"; -import { - API_ACTIONS, API_METHODS, API_STATE, getErrorPayload, HEADERS, TIMEOUT, -} from "./utils"; +import { API_ACTIONS, API_METHODS, API_STATE, getErrorPayload, HEADERS, TIMEOUT, } from "./utils"; const DATA_METHODS = ["POST", "PATCH", "PUT"]; @@ -93,7 +89,7 @@ export { useAPIGet, useAPIPost, useAPIPatch, useAPIPut, useAPIDelete, }; -export function useAPIPolling(endpoint, delay) { +export function useAPIPolling(endpoint, delay = 1000) { // delay ms const [state, setState] = useState({ state: API_STATE.INIT }); const [getState, get] = useAPIGet(endpoint); From 429814ebb587b76e22072cb3ecab31b3a2d5a0e3 Mon Sep 17 00:00:00 2001 From: Bogdan Bodnar Date: Fri, 15 Nov 2019 14:16:37 +0100 Subject: [PATCH 3/3] Add until param to API polling hook. --- src/api/hooks.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/api/hooks.js b/src/api/hooks.js index e873ef7..47cc71d 100644 --- a/src/api/hooks.js +++ b/src/api/hooks.js @@ -5,10 +5,14 @@ * See /LICENSE for more information. */ -import { useCallback, useEffect, useReducer, useState, } from "react"; +import { + useCallback, useEffect, useReducer, useState, +} from "react"; import { ForisURLs } from "forisUrls"; -import { API_ACTIONS, API_METHODS, API_STATE, getErrorPayload, HEADERS, TIMEOUT, } from "./utils"; +import { + API_ACTIONS, API_METHODS, API_STATE, getErrorPayload, HEADERS, TIMEOUT, +} from "./utils"; const DATA_METHODS = ["POST", "PATCH", "PUT"]; @@ -89,7 +93,7 @@ export { useAPIGet, useAPIPost, useAPIPatch, useAPIPut, useAPIDelete, }; -export function useAPIPolling(endpoint, delay = 1000) { // delay ms +export function useAPIPolling(endpoint, delay = 1000, until) { // delay ms const [state, setState] = useState({ state: API_STATE.INIT }); const [getState, get] = useAPIGet(endpoint); @@ -100,11 +104,11 @@ export function useAPIPolling(endpoint, delay = 1000) { // delay ms }, [getState]); useEffect(() => { - if (delay !== null) { + if (until) { const interval = setInterval(get, delay); return () => clearInterval(interval); } - }, [delay, get]); + }, [until, delay, get]); return [state]; }