From 2eed889ab7224e5910155c386e2816e64652157c Mon Sep 17 00:00:00 2001 From: Eric Leijonmarck Date: Wed, 12 Apr 2023 11:07:06 +0100 Subject: [PATCH] Service accounts: Refactor to make roleOptions act as Users for service accounts (#66107) add fetchAC for licenseenabled --- .../components/ServiceAccountProfile.tsx | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/public/app/features/serviceaccounts/components/ServiceAccountProfile.tsx b/public/app/features/serviceaccounts/components/ServiceAccountProfile.tsx index a6d83cc5b38..324f101b0ba 100644 --- a/public/app/features/serviceaccounts/components/ServiceAccountProfile.tsx +++ b/public/app/features/serviceaccounts/components/ServiceAccountProfile.tsx @@ -19,7 +19,7 @@ interface Props { export function ServiceAccountProfile({ serviceAccount, timeZone, onChange }: Props): JSX.Element { const styles = useStyles2(getStyles); const ableToWrite = contextSrv.hasPermission(AccessControlAction.ServiceAccountsWrite); - const [roles, setRoles] = React.useState([]); + const [roles, setRoleOptions] = React.useState([]); const onRoleChange = (role: OrgRole) => { onChange({ ...serviceAccount, role: role }); @@ -28,21 +28,21 @@ export function ServiceAccountProfile({ serviceAccount, timeZone, onChange }: Pr const onNameChange = (newValue: string) => { onChange({ ...serviceAccount, name: newValue }); }; - // TODO: this is a temporary solution to fetch roles for service accounts - // until we make use of the state from the serviceaccountspage - // and pass it down to the serviceaccountprofile + React.useEffect(() => { - if (contextSrv.licensedAccessControlEnabled()) { - if (contextSrv.hasPermission(AccessControlAction.ActionRolesList)) { - fetchRoleOptions(serviceAccount.orgId) - .then((roles) => { - setRoles(roles); - }) - .catch((err) => { - console.log('fetchRoleOptions error: ', err); - }); + async function fetchOptions() { + try { + if (contextSrv.hasPermission(AccessControlAction.ActionRolesList)) { + let options = await fetchRoleOptions(serviceAccount.orgId); + setRoleOptions(options); + } + } catch (e) { + console.error('Error loading options for service account'); } } + if (contextSrv.licensedAccessControlEnabled()) { + fetchOptions(); + } }, [serviceAccount.orgId]); return (