grafana/public/app/core/components/RolePicker/TeamRolePicker.tsx
Alexander Zobnin 867ca5b59e
Access control: Team role picker (#43418)
* Refactor: move fetching from role picker to parent component

* Make built in role props optional

* Initial team role picker

* Add role picker to the teams list

* Optimize fetching roles

* Add pagination for the teams page

* Fix tests

* Hide roles if access control not enabled

* Fix test snapshots

* Refactor: use useAsync() hook

* Refactor: simplify input component

* Move api calls to separate file

* Refactor: use useAsync() hook for user role picker

* Tweak role picker input width

* Fix pagination

* Update test snapshots

* Use loading state from useAsync() hook

* Fix roles label if no roles assigned
2022-01-17 18:04:54 +03:00

43 lines
1.4 KiB
TypeScript

import React, { FC, useState } from 'react';
import { useAsync } from 'react-use';
import { Role } from 'app/types';
import { RolePicker } from './RolePicker';
import { fetchRoleOptions, fetchTeamRoles, updateTeamRoles } from './api';
export interface Props {
teamId: number;
orgId?: number;
getRoleOptions?: () => Promise<Role[]>;
disabled?: boolean;
builtinRolesDisabled?: boolean;
}
export const TeamRolePicker: FC<Props> = ({ teamId, orgId, getRoleOptions, disabled, builtinRolesDisabled }) => {
const [roleOptions, setRoleOptions] = useState<Role[]>([]);
const [appliedRoles, setAppliedRoles] = useState<Role[]>([]);
const { loading } = useAsync(async () => {
try {
let options = await (getRoleOptions ? getRoleOptions() : fetchRoleOptions(orgId));
setRoleOptions(options.filter((option) => !option.name?.startsWith('managed:')));
const teamRoles = await fetchTeamRoles(teamId, orgId);
setAppliedRoles(teamRoles);
} catch (e) {
// TODO handle error
console.error('Error loading options');
}
}, [getRoleOptions, orgId, teamId]);
return (
<RolePicker
onRolesChange={(roles) => updateTeamRoles(roles, teamId, orgId)}
roleOptions={roleOptions}
appliedRoles={appliedRoles}
isLoading={loading}
disabled={disabled}
builtinRolesDisabled={builtinRolesDisabled}
/>
);
};