diff --git a/frontend/src/hooks/api/getters/useFeatureLifecycle/useFeatureLifecycle.ts b/frontend/src/hooks/api/getters/useFeatureLifecycle/useFeatureLifecycle.ts new file mode 100644 index 0000000000..99b4263fc9 --- /dev/null +++ b/frontend/src/hooks/api/getters/useFeatureLifecycle/useFeatureLifecycle.ts @@ -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( + 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 => { + return fetch(path) + .then(handleErrorResponses('Feature Lifecycle Data')) + .then((res) => res.json()); +};