2022-05-31 08:59:09 +02:00
|
|
|
import useSWR from 'swr';
|
|
|
|
import { useMemo } from 'react';
|
2022-03-25 12:34:20 +01:00
|
|
|
import { formatApiPath } from 'utils/formatPath';
|
2021-10-15 09:21:38 +02:00
|
|
|
import handleErrorResponses from '../httpErrorResponseHandler';
|
2021-04-23 10:59:11 +02:00
|
|
|
|
2022-05-31 08:59:09 +02:00
|
|
|
export const useUsers = () => {
|
|
|
|
const { data, error, mutate } = useSWR(
|
|
|
|
formatApiPath(`api/admin/user-admin`),
|
|
|
|
fetcher
|
|
|
|
);
|
2021-04-23 10:59:11 +02:00
|
|
|
|
2022-05-31 08:59:09 +02:00
|
|
|
return useMemo(
|
|
|
|
() => ({
|
|
|
|
users: data?.users ?? [],
|
|
|
|
roles: data?.rootRoles ?? [],
|
|
|
|
loading: !error && !data,
|
|
|
|
refetch: () => mutate(),
|
|
|
|
error,
|
|
|
|
}),
|
|
|
|
[data, error, mutate]
|
|
|
|
);
|
2021-04-23 10:59:11 +02:00
|
|
|
};
|
|
|
|
|
2022-05-31 08:59:09 +02:00
|
|
|
const fetcher = (path: string) => {
|
|
|
|
return fetch(path)
|
|
|
|
.then(handleErrorResponses('Users'))
|
|
|
|
.then(res => res.json());
|
|
|
|
};
|