-
-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: feature lifecycle getter hook (#6876)
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
frontend/src/hooks/api/getters/useFeatureLifecycle/useFeatureLifecycle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
}; |