From 49086653e1a0a63e1e38a47ca8794e8b810853e0 Mon Sep 17 00:00:00 2001 From: Erik Jan de Wit Date: Wed, 27 Apr 2022 10:48:32 +0200 Subject: [PATCH] Changed the way the table is updated (#2505) --- .../role-mapping/AddRoleMappingModal.tsx | 148 +++++++++--------- .../table-toolbar/KeycloakDataTable.tsx | 6 +- 2 files changed, 80 insertions(+), 74 deletions(-) diff --git a/src/components/role-mapping/AddRoleMappingModal.tsx b/src/components/role-mapping/AddRoleMappingModal.tsx index ce896fd215..07e97e6871 100644 --- a/src/components/role-mapping/AddRoleMappingModal.tsx +++ b/src/components/role-mapping/AddRoleMappingModal.tsx @@ -1,6 +1,5 @@ -import React, { useEffect, useState } from "react"; +import React, { useState } from "react"; import { useTranslation } from "react-i18next"; -import { findIndex } from "lodash-es"; import { Badge, Button, @@ -15,11 +14,11 @@ import { SelectVariant, ToolbarItem, } from "@patternfly/react-core"; +import { FilterIcon } from "@patternfly/react-icons"; import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation"; import { KeycloakDataTable } from "../table-toolbar/KeycloakDataTable"; import { useFetch, useAdminClient } from "../../context/auth/AdminClient"; -import { FilterIcon } from "@patternfly/react-icons"; import { castAdminClient, mapping, @@ -27,6 +26,7 @@ import { Row, ServiceRole, } from "./RoleMapping"; +import { KeycloakSpinner } from "../keycloak-spinner/KeycloakSpinner"; type AddRoleMappingModalProps = { id: string; @@ -61,12 +61,11 @@ export const AddRoleMappingModal = ({ const [clients, setClients] = useState([]); const [searchToggle, setSearchToggle] = useState(false); - const [key, setKey] = useState(0); - const refresh = () => setKey(key + 1); - const [selectedClients, setSelectedClients] = useState([]); const [selectedRows, setSelectedRows] = useState([]); + const [data, setData] = useState(); + const mapType = mapping.find((m) => m.resource === type)!; useFetch( @@ -101,71 +100,77 @@ export const AddRoleMappingModal = ({ [] ); - useEffect(refresh, [searchToggle]); + useFetch( + async () => { + const realmRolesSelected = selectedClients.some( + (client) => client.name === "realmRoles" + ); + let selected = selectedClients; + if (realmRolesSelected) { + selected = selectedClients.filter( + (client) => client.name !== "realmRoles" + ); + } + + const availableRoles = await castAdminClient( + adminClient, + mapType.resource + )[mapType.functions.list[1]]({ + id, + }); + + const realmRoles = availableRoles.map((role) => { + return { + id: role.id, + role, + client: undefined, + }; + }); + + const allClients = + selectedClients.length !== 0 + ? selected + : await adminClient.clients.find(); + + const roles = ( + await Promise.all( + allClients.map(async (client) => { + const clientAvailableRoles = await castAdminClient( + adminClient, + mapType.resource === "roles" ? "clients" : mapType.resource + )[mapType.functions.list[0]]({ + id: mapType.resource === "roles" ? client.id : id, + client: client.id, + clientUniqueId: client.id, + }); + + return clientAvailableRoles.map((role) => { + return { + id: role.id, + role, + client, + }; + }); + }) + ) + ).flat(); + + return [ + ...(realmRolesSelected || selected.length === 0 ? realmRoles : []), + ...roles, + ]; + }, + setData, + [selectedClients] + ); const removeClient = (client: ClientRole) => { setSelectedClients(selectedClients.filter((item) => item.id !== client.id)); }; - const loader = async () => { - const realmRolesSelected = findIndex( - selectedClients, - (client) => client.name === "realmRoles" - ); - let selected = selectedClients; - if (realmRolesSelected !== -1) { - selected = selectedClients.filter( - (client) => client.name !== "realmRoles" - ); - } - - const availableRoles = await castAdminClient(adminClient, mapType.resource)[ - mapType.functions.list[1] - ]({ - id, - }); - - const realmRoles = availableRoles.map((role) => { - return { - id: role.id, - role, - client: undefined, - }; - }); - - const allClients = - selectedClients.length !== 0 - ? selected - : await adminClient.clients.find(); - - const roles = ( - await Promise.all( - allClients.map(async (client) => { - const clientAvailableRoles = await castAdminClient( - adminClient, - mapType.resource === "roles" ? "clients" : mapType.resource - )[mapType.functions.list[0]]({ - id: mapType.resource === "roles" ? client.id : id, - client: client.id, - clientUniqueId: client.id, - }); - - return clientAvailableRoles.map((role) => { - return { - id: role.id, - role, - client, - }; - }); - }) - ) - ).flat(); - - return [ - ...(realmRolesSelected !== -1 || selected.length === 0 ? realmRoles : []), - ...roles, - ]; - }; + if (!data) { + return ; + } const createSelectGroup = (clients: ClientRepresentation[]) => [ @@ -215,14 +220,13 @@ export const AddRoleMappingModal = ({ ]} > setSelectedRows([...rows])} searchPlaceholderKey="clients:searchByRoleName" searchTypeComponent={