Skip to content

Commit

Permalink
feat: add placeholder getter hook
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheartman committed Oct 31, 2024
1 parent 3cd9e48 commit d087f8a
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<ActionableChangeRequestsSchema>(
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());
};

0 comments on commit d087f8a

Please sign in to comment.