2023-03-14 09:38:21 -05:00
|
|
|
import React, { useEffect } from 'react';
|
2022-02-09 07:42:46 -06:00
|
|
|
import { useAsyncFn } from 'react-use';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2022-08-12 02:16:16 -05:00
|
|
|
import { contextSrv } from 'app/core/core';
|
|
|
|
import { Role, AccessControlAction } from 'app/types';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2022-01-17 09:04:54 -06:00
|
|
|
import { RolePicker } from './RolePicker';
|
2022-02-04 07:54:42 -06:00
|
|
|
import { fetchTeamRoles, updateTeamRoles } from './api';
|
2022-01-17 09:04:54 -06:00
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
teamId: number;
|
|
|
|
orgId?: number;
|
2022-02-04 07:54:42 -06:00
|
|
|
roleOptions: Role[];
|
2022-01-17 09:04:54 -06:00
|
|
|
disabled?: boolean;
|
2023-11-01 05:57:02 -05:00
|
|
|
roles?: Role[];
|
2022-08-18 06:21:06 -05:00
|
|
|
onApplyRoles?: (newRoles: Role[]) => void;
|
|
|
|
pendingRoles?: Role[];
|
|
|
|
/**
|
|
|
|
* Set whether the component should send a request with the new roles to the
|
|
|
|
* backend in TeamRolePicker.onRolesChange (apply=false), or call {@link onApplyRoles}
|
|
|
|
* with the updated list of roles (apply=true).
|
|
|
|
*
|
|
|
|
* Besides it sets the RolePickerMenu's Button title to
|
|
|
|
* * `Update` in case apply equals false
|
|
|
|
* * `Apply` in case apply equals true
|
|
|
|
*
|
|
|
|
* @default false
|
|
|
|
*/
|
|
|
|
apply?: boolean;
|
2022-08-25 06:30:11 -05:00
|
|
|
maxWidth?: string | number;
|
2023-10-25 06:03:12 -05:00
|
|
|
width?: string | number;
|
2023-11-01 05:57:02 -05:00
|
|
|
isLoading?: boolean;
|
2022-01-17 09:04:54 -06:00
|
|
|
}
|
|
|
|
|
2023-03-14 09:38:21 -05:00
|
|
|
export const TeamRolePicker = ({
|
2022-08-18 06:21:06 -05:00
|
|
|
teamId,
|
|
|
|
roleOptions,
|
|
|
|
disabled,
|
2023-11-01 05:57:02 -05:00
|
|
|
roles,
|
2022-08-18 06:21:06 -05:00
|
|
|
onApplyRoles,
|
|
|
|
pendingRoles,
|
|
|
|
apply = false,
|
2022-08-25 06:30:11 -05:00
|
|
|
maxWidth,
|
2023-10-25 06:03:12 -05:00
|
|
|
width,
|
2023-11-01 05:57:02 -05:00
|
|
|
isLoading,
|
2023-03-14 09:38:21 -05:00
|
|
|
}: Props) => {
|
2023-11-01 05:57:02 -05:00
|
|
|
const [{ loading, value: appliedRoles = roles || [] }, getTeamRoles] = useAsyncFn(async () => {
|
2022-01-17 09:04:54 -06:00
|
|
|
try {
|
2023-11-01 05:57:02 -05:00
|
|
|
if (roles) {
|
|
|
|
return roles;
|
|
|
|
}
|
2022-08-18 06:21:06 -05:00
|
|
|
if (apply && Boolean(pendingRoles?.length)) {
|
|
|
|
return pendingRoles;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (contextSrv.hasPermission(AccessControlAction.ActionTeamsRolesList)) {
|
|
|
|
return await fetchTeamRoles(teamId);
|
|
|
|
}
|
2022-01-17 09:04:54 -06:00
|
|
|
} catch (e) {
|
2022-08-18 06:21:06 -05:00
|
|
|
console.error('Error loading options', e);
|
2022-01-17 09:04:54 -06:00
|
|
|
}
|
2022-02-09 07:42:46 -06:00
|
|
|
return [];
|
2023-11-01 05:57:02 -05:00
|
|
|
}, [teamId, pendingRoles, roles]);
|
2022-01-17 09:04:54 -06:00
|
|
|
|
2022-02-09 07:42:46 -06:00
|
|
|
useEffect(() => {
|
|
|
|
getTeamRoles();
|
2023-11-01 05:57:02 -05:00
|
|
|
}, [getTeamRoles]);
|
2022-02-09 07:42:46 -06:00
|
|
|
|
2022-05-23 09:13:55 -05:00
|
|
|
const onRolesChange = async (roles: Role[]) => {
|
2022-08-18 06:21:06 -05:00
|
|
|
if (!apply) {
|
|
|
|
await updateTeamRoles(roles, teamId);
|
|
|
|
await getTeamRoles();
|
|
|
|
} else if (onApplyRoles) {
|
|
|
|
onApplyRoles(roles);
|
|
|
|
}
|
2022-02-09 07:42:46 -06:00
|
|
|
};
|
|
|
|
|
2022-08-12 02:16:16 -05:00
|
|
|
const canUpdateRoles =
|
|
|
|
contextSrv.hasPermission(AccessControlAction.ActionTeamsRolesAdd) &&
|
|
|
|
contextSrv.hasPermission(AccessControlAction.ActionTeamsRolesRemove);
|
|
|
|
|
2022-01-17 09:04:54 -06:00
|
|
|
return (
|
|
|
|
<RolePicker
|
2022-08-18 06:21:06 -05:00
|
|
|
apply={apply}
|
2022-02-09 07:42:46 -06:00
|
|
|
onRolesChange={onRolesChange}
|
2022-01-17 09:04:54 -06:00
|
|
|
roleOptions={roleOptions}
|
|
|
|
appliedRoles={appliedRoles}
|
2023-11-01 05:57:02 -05:00
|
|
|
isLoading={loading || isLoading}
|
2022-01-17 09:04:54 -06:00
|
|
|
disabled={disabled}
|
2022-08-18 06:21:06 -05:00
|
|
|
basicRoleDisabled={true}
|
2022-08-12 02:16:16 -05:00
|
|
|
canUpdateRoles={canUpdateRoles}
|
2022-08-25 06:30:11 -05:00
|
|
|
maxWidth={maxWidth}
|
2023-10-25 06:03:12 -05:00
|
|
|
width={width}
|
2022-01-17 09:04:54 -06:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|