mirror of
https://github.com/Unleash/unleash.git
synced 2025-04-19 01:17:18 +02:00
https://linear.app/unleash/issue/2-1510/create-message-banner-hooks-that-connect-to-the-new-api-endpoints Adds new message banner API hooks that will allow us to do CRUD operations for message banners in the UI.
39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import { useMemo } from 'react';
|
|
import { formatApiPath } from 'utils/formatPath';
|
|
import handleErrorResponses from '../httpErrorResponseHandler';
|
|
import { useConditionalSWR } from '../useConditionalSWR/useConditionalSWR';
|
|
import useUiConfig from '../useUiConfig/useUiConfig';
|
|
import { useUiFlag } from 'hooks/useUiFlag';
|
|
import { IInternalMessageBanner } from 'interfaces/messageBanner';
|
|
|
|
const ENDPOINT = 'api/admin/message-banners';
|
|
|
|
export const useMessageBanners = () => {
|
|
const { isEnterprise } = useUiConfig();
|
|
const internalMessageBanners = useUiFlag('internalMessageBanners');
|
|
|
|
const { data, error, mutate } = useConditionalSWR(
|
|
isEnterprise() && internalMessageBanners,
|
|
{ messageBanners: [] },
|
|
formatApiPath(ENDPOINT),
|
|
fetcher,
|
|
);
|
|
|
|
return useMemo(
|
|
() => ({
|
|
messageBanners: (data?.messageBanners ??
|
|
[]) as IInternalMessageBanner[],
|
|
loading: !error && !data,
|
|
refetch: () => mutate(),
|
|
error,
|
|
}),
|
|
[data, error, mutate],
|
|
);
|
|
};
|
|
|
|
const fetcher = (path: string) => {
|
|
return fetch(path)
|
|
.then(handleErrorResponses('Message Banners'))
|
|
.then((res) => res.json());
|
|
};
|