Skip to content

Commit

Permalink
feat: feature lifecycle getter hook (#6876)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaanus committed Apr 18, 2024
1 parent eec5469 commit f0ef7a6
Showing 1 changed file with 54 additions and 0 deletions.
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());
};

0 comments on commit f0ef7a6

Please sign in to comment.