1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-04 00:18:01 +01:00

feat: feature lifecycle getter hook (#6876)

This commit is contained in:
Jaanus Sellin 2024-04-18 09:02:03 +03:00 committed by GitHub
parent eec5469f43
commit f0ef7a6f31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,54 @@
import useSWR, { mutate, type SWRConfiguration } from 'swr';
import { useCallback } from 'react';
import { formatApiPath } from 'utils/formatPath';
import handleErrorResponses from '../httpErrorResponseHandler';
import type { FeatureLifecycleSchema } from 'openapi';
interface IUseFeatureLifecycleDataOutput {
lifecycle: FeatureLifecycleSchema;
refetchLifecycle: () => void;
loading: boolean;
error?: Error;
}
export const formatLifecycleApiPath = (
projectId: string,
featureId: string,
): string => {
return formatApiPath(
`api/admin/projects/${projectId}/features/${featureId}/lifecycle`,
);
};
export const useFeatureLifecycle = (
projectId: string,
featureId: string,
options?: SWRConfiguration,
): IUseFeatureLifecycleDataOutput => {
const path = formatLifecycleApiPath(projectId, featureId);
const { data, error } = useSWR<FeatureLifecycleSchema>(
path,
fetchFeatureLifecycle,
options,
);
const refetchLifecycle = useCallback(() => {
mutate(path).catch(console.warn);
}, [path]);
return {
lifecycle: data || [],
refetchLifecycle,
loading: !error && !data,
error,
};
};
const fetchFeatureLifecycle = (
path: string,
): Promise<FeatureLifecycleSchema> => {
return fetch(path)
.then(handleErrorResponses('Feature Lifecycle Data'))
.then((res) => res.json());
};