import React, { useState } from "react"; import { useTranslation } from "react-i18next"; import { useFormContext, Controller } from "react-hook-form"; import { FormGroup, Button, Checkbox } from "@patternfly/react-core"; import { MinusCircleIcon } from "@patternfly/react-icons"; import { TableComposable, Thead, Tr, Th, Tbody, Td, } from "@patternfly/react-table"; import { Row, ServiceRole } from "../../../components/role-mapping/RoleMapping"; import type { RequiredIdValue } from "./ClientScope"; import { HelpItem } from "../../../components/help-enabler/HelpItem"; import { useAdminClient, useFetch } from "../../../context/auth/AdminClient"; import { AddRoleMappingModal } from "../../../components/role-mapping/AddRoleMappingModal"; export const Role = () => { const { t } = useTranslation("clients"); const { control, getValues, setValue, errors } = useFormContext<{ roles?: RequiredIdValue[]; }>(); const values = getValues("roles"); const [open, setOpen] = useState(false); const [selectedRoles, setSelectedRoles] = useState([]); const adminClient = useAdminClient(); useFetch( async () => { if (values && values.length > 0) { const roles = await Promise.all( values.map((r) => adminClient.roles.findOneById({ id: r.id })) ); return Promise.all( roles .filter((r) => r?.clientRole) .map(async (role) => ({ role: role!, client: await adminClient.clients.findOne({ id: role?.containerId!, }), })) ); } return Promise.resolve([]); }, setSelectedRoles, [] ); return ( } fieldId="roles" helperTextInvalid={t("requiredRoles")} validated={errors.roles ? "error" : "default"} isRequired > value.filter((c) => c.id).length > 0, }} render={({ onChange, value }) => ( <> {open && ( { onChange([ ...value, ...rows.map((row) => ({ id: row.role.id })), ]); setSelectedRoles([...selectedRoles, ...rows]); setOpen(false); }} onClose={() => { setOpen(false); }} isLDAPmapper /> )} )} /> {selectedRoles.length > 0 && ( {t("roles")} {t("required")} {selectedRoles.map((row, index) => ( ( )} />