1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-06-04 01:18:20 +02:00
unleash.unleash/frontend/src/hooks/api/getters/useServiceAccounts/useServiceAccounts.ts
Tymoteusz Czech 30a753b93f
UI/bulk select (#3267)
Select multiple toggles on project overview.
2023-03-14 08:56:03 +00:00

36 lines
1.1 KiB
TypeScript

import IRole from 'interfaces/role';
import { IServiceAccount } from 'interfaces/service-account';
import { useMemo } from 'react';
import { formatApiPath } from 'utils/formatPath';
import handleErrorResponses from '../httpErrorResponseHandler';
import { useConditionalSWR } from '../useConditionalSWR/useConditionalSWR';
import useUiConfig from '../useUiConfig/useUiConfig';
export const useServiceAccounts = () => {
const { isEnterprise } = useUiConfig();
const { data, error, mutate } = useConditionalSWR(
isEnterprise(),
{ serviceAccounts: [], rootRoles: [] },
formatApiPath(`api/admin/service-account`),
fetcher
);
return useMemo(
() => ({
serviceAccounts: (data?.serviceAccounts ?? []) as IServiceAccount[],
roles: (data?.rootRoles ?? []) as IRole[],
loading: !error && !data,
refetch: () => mutate(),
error,
}),
[data, error, mutate]
);
};
const fetcher = (path: string) => {
return fetch(path)
.then(handleErrorResponses('Service Accounts'))
.then(res => res.json());
};