(app-desk) add useUsers react-query hook

Add the hook useUsers, it will be used to
search users by name or email.
This commit is contained in:
Anthony LC
2024-03-14 15:47:56 +01:00
committed by Anthony LC
parent 1ad6ef8f96
commit e9848bd199
2 changed files with 39 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
export * from './useTeamsAccesses';
export * from './useUpdateTeamAccess';
export * from './useUsers';

View File

@@ -0,0 +1,36 @@
import { UseQueryOptions, useQuery } from '@tanstack/react-query';
import { APIError, APIList, errorCauses, fetchAPI } from '@/api';
import { User } from '@/features/auth';
export type UsersParams = {
query: string;
};
type UsersResponse = APIList<User>;
export const getUsers = async ({
query,
}: UsersParams): Promise<UsersResponse> => {
const queryParam = query ? `q=${query}` : '';
const response = await fetchAPI(`users/?${queryParam}`);
if (!response.ok) {
throw new APIError('Failed to get the users', await errorCauses(response));
}
return response.json() as Promise<UsersResponse>;
};
export const KEY_LIST_USER = 'users';
export function useUsers(
param: UsersParams,
queryConfig?: UseQueryOptions<UsersResponse, APIError, UsersResponse>,
) {
return useQuery<UsersResponse, APIError, UsersResponse>({
queryKey: [KEY_LIST_USER, param],
queryFn: () => getUsers(param),
...queryConfig,
});
}