diff --git a/src/clients/authorization/PermissionDetails.tsx b/src/clients/authorization/PermissionDetails.tsx index 6bdb1c4092..c4b2ce9dbd 100644 --- a/src/clients/authorization/PermissionDetails.tsx +++ b/src/clients/authorization/PermissionDetails.tsx @@ -296,8 +296,8 @@ export default function PermissionDetails() { > ; + permissionId?: string; variant?: SelectVariant; preSelected?: string; isRequired?: boolean; @@ -21,10 +24,31 @@ type Policies = { name?: string; }; +type TypeMapping = { + [key in Type]: { + searchFunction: keyof Pick; + fetchFunction: keyof Pick< + Clients, + "getAssociatedPolicies" | "getAssociatedResources" + >; + }; +}; + +const typeMapping: TypeMapping = { + resources: { + searchFunction: "listResources", + fetchFunction: "getAssociatedResources", + }, + policies: { + searchFunction: "listPolicies", + fetchFunction: "getAssociatedPolicies", + }, +}; + export const ResourcesPolicySelect = ({ name, - searchFunction, clientId, + permissionId, variant = SelectVariant.typeaheadMulti, preSelected, isRequired = false, @@ -40,20 +64,40 @@ export const ResourcesPolicySelect = ({ const [search, setSearch] = useState(""); const [open, setOpen] = useState(false); + const functions = typeMapping[name]; + + const convert = ( + p: PolicyRepresentation | ResourceRepresentation + ): Policies => ({ + id: "_id" in p ? p._id : "id" in p ? p.id : undefined, + name: p.name, + }); + useFetch( async () => ( - await adminClient.clients[searchFunction]( - Object.assign( - { id: clientId, first: 0, max: 10 }, - search === "" ? null : { name: search } - ) - ) - ).map((p) => ({ - id: "_id" in p ? p._id : "id" in p ? p.id : undefined, - name: p.name, - })), - (policies) => setItems(policies), + await Promise.all([ + adminClient.clients[functions.searchFunction]( + Object.assign( + { id: clientId, first: 0, max: 10 }, + search === "" ? null : { name: search } + ) + ), + permissionId + ? adminClient.clients[functions.fetchFunction]({ + id: clientId, + permissionId, + }) + : Promise.resolve([]), + ]) + ) + .flat() + .map(convert) + .filter( + ({ id }, index, self) => + index === self.findIndex(({ id: otherId }) => id === otherId) + ), + setItems, [search] ); diff --git a/src/clients/authorization/policy/Aggregate.tsx b/src/clients/authorization/policy/Aggregate.tsx index 5527516a25..0910a17627 100644 --- a/src/clients/authorization/policy/Aggregate.tsx +++ b/src/clients/authorization/policy/Aggregate.tsx @@ -24,11 +24,7 @@ export const Aggregate = () => { /> } > - +