1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-19 00:15:43 +01:00
unleash.unleash/frontend/src/hooks/api/getters/useFeature/useFeature.ts
olav 49a63173f8 fix: avoid constraint accordion close on focus (#907)
* fix: avoid constraint accordion close on focus

* refactor: fix mutate cache key mismatch
2022-04-26 09:54:16 +02:00

74 lines
1.7 KiB
TypeScript

import useSWR, { SWRConfiguration } from 'swr';
import { useCallback } from 'react';
import { emptyFeature } from './emptyFeature';
import handleErrorResponses from '../httpErrorResponseHandler';
import { formatApiPath } from 'utils/formatPath';
import { IFeatureToggle } from 'interfaces/featureToggle';
export interface IUseFeatureOutput {
feature: IFeatureToggle;
refetchFeature: () => void;
loading: boolean;
status?: number;
error?: Error;
}
export interface IFeatureResponse {
status: number;
body?: IFeatureToggle;
}
export const useFeature = (
projectId: string,
featureId: string,
options?: SWRConfiguration
): IUseFeatureOutput => {
const path = formatFeatureApiPath(projectId, featureId);
const { data, error, mutate } = useSWR<IFeatureResponse>(
['useFeature', path],
() => featureFetcher(path),
options
);
const refetchFeature = useCallback(() => {
mutate().catch(console.warn);
}, [mutate]);
return {
feature: data?.body || emptyFeature,
refetchFeature,
loading: !error && !data,
status: data?.status,
error,
};
};
export const featureFetcher = async (
path: string
): Promise<IFeatureResponse> => {
const res = await fetch(path);
if (res.status === 404) {
return { status: 404 };
}
if (!res.ok) {
await handleErrorResponses('Feature toggle data')(res);
}
return {
status: res.status,
body: await res.json(),
};
};
export const formatFeatureApiPath = (
projectId: string,
featureId: string
): string => {
return formatApiPath(
`api/admin/projects/${projectId}/features/${featureId}`
);
};