1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-09 00:18:00 +01:00
unleash.unleash/frontend/src/hooks/api/getters/useUsers/useUsers.ts

29 lines
726 B
TypeScript
Raw Normal View History

import useSWR from 'swr';
import { useMemo } from 'react';
import { formatApiPath } from 'utils/formatPath';
import handleErrorResponses from '../httpErrorResponseHandler';
export const useUsers = () => {
const { data, error, mutate } = useSWR(
formatApiPath(`api/admin/user-admin`),
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());
};