From 965145d98aadbc352d3556344227135c46fae342 Mon Sep 17 00:00:00 2001 From: Patrick Ear Date: Thu, 18 Apr 2024 15:15:23 +0200 Subject: [PATCH] save --- .../ui-elements/SelectAccountIAMRole.tsx | 64 ++++++++++++++----- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/src/react/ui-elements/SelectAccountIAMRole.tsx b/src/react/ui-elements/SelectAccountIAMRole.tsx index f5667f326..89912cd7e 100644 --- a/src/react/ui-elements/SelectAccountIAMRole.tsx +++ b/src/react/ui-elements/SelectAccountIAMRole.tsx @@ -1,3 +1,4 @@ +import { Stack } from '@scality/core-ui'; import { Select } from '@scality/core-ui/dist/next'; import { IAM } from 'aws-sdk'; import { useState } from 'react'; @@ -6,20 +7,42 @@ import { Account } from '../../types/iam'; import { useIAMClient } from '../IAMProvider'; import { getListRolesQuery } from '../queries'; import { useAccounts } from '../utils/hooks'; -import { Stack } from '@scality/core-ui'; -// { accountName: JBw, roleName: Dataconsumer } +const filterRoles = ( + accountName: string, + roles: IAM.Role[], + hideAccountRoles: { accountName: string; roleName: string }[], +) => { + return roles.filter( + (role) => + !hideAccountRoles.find( + (hideRole) => + hideRole.accountName === accountName && + hideRole.roleName === role.RoleName, + ), + ); +}; + const defaultOnChange = () => ({}); export const SelectAccountIAMRole = (props: { onChange: (account: Account, role: IAM.Role) => void; - // hideRoles - // defaultValue + defaultValue?: { accountName: string; roleName: string }; + hideAccountRoles?: { accountName: string; roleName: string }[]; }) => { - const { onChange = defaultOnChange } = props; + const { + onChange = defaultOnChange, + hideAccountRoles = [], + defaultValue, + } = props; const { accounts } = useAccounts(); const IAMClient = useIAMClient(); - const [account, setAccount] = useState(null); + const defaultAccount = + accounts.find((account) => account.Name === defaultValue?.accountName) ?? + null; + const [account, setAccount] = useState(defaultAccount); + const [role, setRole] = useState(null); + const accountName = account ? account.Name : ''; const rolesQuery = getListRolesQuery(accountName, IAMClient); const listRolesQuery = { @@ -27,43 +50,54 @@ export const SelectAccountIAMRole = (props: { enabled: !!IAMClient && !!IAMClient.client && accountName !== '', }; const roleQueryData = useQuery(listRolesQuery); - const roles = roleQueryData?.data?.Roles ?? null; + const roles = filterRoles( + accountName, + roleQueryData?.data?.Roles ?? [], + hideAccountRoles, + ); - console.log('account', account); + const isDefaultAccountSelected = account?.Name === defaultValue?.accountName; + const defaultRole = isDefaultAccountSelected ? defaultValue?.roleName : null; return ( - {roles ? ( + {roles.length > 0 ? (