import React, { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { Button, ButtonVariant, Dropdown, DropdownToggle, Modal, ModalVariant, DropdownDirection, } from "@patternfly/react-core"; import { CaretUpIcon } from "@patternfly/react-icons"; import { Table, TableBody, TableHeader, TableVariant, } from "@patternfly/react-table"; import ClientScopeRepresentation from "keycloak-admin/lib/defs/clientScopeRepresentation"; import { ClientScopeType, clientScopeTypesDropdown, } from "../../components/client-scope/ClientScopeTypes"; export type AddScopeDialogProps = { clientScopes: ClientScopeRepresentation[]; open: boolean; toggleDialog: () => void; onAdd: ( scopes: { scope: ClientScopeRepresentation; type: ClientScopeType }[] ) => void; }; type Row = { selected: boolean; scope: ClientScopeRepresentation; cells: (string | undefined)[]; }; export const AddScopeDialog = ({ clientScopes, open, toggleDialog, onAdd, }: AddScopeDialogProps) => { const { t } = useTranslation("clients"); const [addToggle, setAddToggle] = useState(false); const [rows, setRows] = useState([]); useEffect(() => { setRows( clientScopes.map((scope) => { return { selected: false, scope, cells: [scope.name, scope.description], }; }) ); }, [clientScopes]); const action = (scope: ClientScopeType) => { const scopes = rows .filter((row) => row.selected) .map((row) => { return { scope: row.scope, type: scope }; }); onAdd(scopes); setAddToggle(false); toggleDialog(); }; return ( setAddToggle(!addToggle)} isPrimary toggleIndicator={CaretUpIcon} id="add-scope-toggle" > {t("common:add")} } dropdownItems={clientScopeTypesDropdown(t, action)} />, , ]} > { if (rowIndex === -1) { setRows( rows.map((row) => { row.selected = isSelected; return row; }) ); } else { rows[rowIndex].selected = isSelected; setRows([...rows]); } }} rows={rows} aria-label={t("chooseAMapperType")} >
); };