mirror of
https://gitlab.nic.cz/turris/reforis/foris-js.git
synced 2024-12-26 00:21:36 +01:00
Merge branch 'api-polling' into 'dev'
Add hook for API polling. See merge request turris/reforis/foris-js!41
This commit is contained in:
commit
a6866a0673
|
@ -5,7 +5,9 @@
|
||||||
* See /LICENSE for more information.
|
* See /LICENSE for more information.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { useCallback, useReducer } from "react";
|
import {
|
||||||
|
useCallback, useEffect, useReducer, useState,
|
||||||
|
} from "react";
|
||||||
|
|
||||||
import { ForisURLs } from "forisUrls";
|
import { ForisURLs } from "forisUrls";
|
||||||
import {
|
import {
|
||||||
|
@ -90,3 +92,23 @@ const useAPIDelete = createAPIHook("DELETE");
|
||||||
export {
|
export {
|
||||||
useAPIGet, useAPIPost, useAPIPatch, useAPIPut, useAPIDelete,
|
useAPIGet, useAPIPost, useAPIPatch, useAPIPut, useAPIDelete,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function useAPIPolling(endpoint, delay = 1000, until) { // delay ms
|
||||||
|
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 (until) {
|
||||||
|
const interval = setInterval(get, delay);
|
||||||
|
return () => clearInterval(interval);
|
||||||
|
}
|
||||||
|
}, [until, delay, get]);
|
||||||
|
|
||||||
|
return [state];
|
||||||
|
}
|
||||||
|
|
|
@ -7,7 +7,12 @@
|
||||||
|
|
||||||
// API
|
// API
|
||||||
export {
|
export {
|
||||||
useAPIGet, useAPIPost, useAPIPatch, useAPIPut, useAPIDelete,
|
useAPIGet,
|
||||||
|
useAPIPost,
|
||||||
|
useAPIPatch,
|
||||||
|
useAPIPut,
|
||||||
|
useAPIDelete,
|
||||||
|
useAPIPolling,
|
||||||
} from "api/hooks";
|
} from "api/hooks";
|
||||||
export { API_STATE } from "api/utils";
|
export { API_STATE } from "api/utils";
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user