1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-29 01:15:48 +02:00
unleash.unleash/frontend/src/hooks/api/getters/useUiBootstrap/useUiBootstrap.ts
Fredrik Strand Oseberg c99decf5e0 remove: redux (#696)
* remove: redux

* fix: add sass back

* fix: update lock

* fix: remove fake access store import

'
2022-02-11 11:19:55 +01:00

42 lines
1.2 KiB
TypeScript

import handleErrorResponses from '../httpErrorResponseHandler';
import useSWR, { mutate, SWRConfiguration } from 'swr';
import { useState, useEffect } from 'react';
import { formatApiPath } from '../../../../utils/format-path';
const useUiBootstrap = (options: SWRConfiguration = {}) => {
// The point of the bootstrap is to get multiple datasets in one call. Therefore,
// this needs to be refactored to seed other hooks with the correct data.
const BOOTSTRAP_CACHE_KEY = `api/admin/ui-bootstrap`;
const fetcher = () => {
const path = formatApiPath(`api/admin/ui-bootstrap`);
return fetch(path, {
method: 'GET',
credentials: 'include',
})
.then(handleErrorResponses('ui bootstrap'))
.then(res => res.json());
};
const { data, error } = useSWR(BOOTSTRAP_CACHE_KEY, fetcher, options);
const [loading, setLoading] = useState(!error && !data);
const refetchUiBootstrap = () => {
mutate(BOOTSTRAP_CACHE_KEY);
};
useEffect(() => {
setLoading(!error && !data);
}, [data, error]);
return {
bootstrap: data,
error,
loading,
refetchUiBootstrap,
};
};
export default useUiBootstrap;