diff --git a/frontend/src/hooks/api/getters/useActionableChangeRequests/useActionableChangeRequests.ts b/frontend/src/hooks/api/getters/useActionableChangeRequests/useActionableChangeRequests.ts new file mode 100644 index 000000000000..4f0648037c0d --- /dev/null +++ b/frontend/src/hooks/api/getters/useActionableChangeRequests/useActionableChangeRequests.ts @@ -0,0 +1,43 @@ +import useSWR from 'swr'; +import { useMemo } from 'react'; +import { formatApiPath } from 'utils/formatPath'; +import handleErrorResponses from '../httpErrorResponseHandler'; +import type { IUser } from 'interfaces/user'; +import type { IRole } from 'interfaces/role'; +import type { ActionableChangeRequestsSchema } from 'openapi/models/actionableChangeRequestsSchema'; + +interface IUseUsersOutput { + users: IUser[]; + roles: IRole[]; + loading: boolean; + refetch: () => void; + error?: Error; +} + +export const useActionableChangeRequests = ( + projectId: string, +): IUseUsersOutput => { + const { data, error, mutate } = useSWR( + formatApiPath( + `api/admin/projects/${projectId}/change-requests/actionable`, + ), + fetcher, + ); + + return useMemo( + () => ({ + users: data?.users ?? [], + roles: data?.rootRoles ?? [], + loading: !error && !data, + refetch: () => mutate(), + error, + }), + [data, error, mutate], + ); +}; + +const fetcher = (path: string) => { + return fetch(path) + .then(handleErrorResponses('Users')) + .then((res) => res.json()); +};