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, Tbody, Td, Th, Thead, Tr, } from "@patternfly/react-table"; import type ClientScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientScopeRepresentation"; import { useAdminClient, useFetch } from "../../../context/auth/AdminClient"; import { HelpItem } from "../../../components/help-enabler/HelpItem"; import { AddScopeDialog } from "../../scopes/AddScopeDialog"; import useLocaleSort, { mapByKey } from "../../../utils/useLocaleSort"; export type RequiredIdValue = { id: string; required: boolean; }; export const ClientScope = () => { const { t } = useTranslation("clients"); const { control, getValues, setValue, formState: { errors }, } = useFormContext<{ clientScopes: RequiredIdValue[]; }>(); const [open, setOpen] = useState(false); const [scopes, setScopes] = useState([]); const [selectedScopes, setSelectedScopes] = useState< ClientScopeRepresentation[] >([]); const { adminClient } = useAdminClient(); const localeSort = useLocaleSort(); useFetch( () => adminClient.clientScopes.find(), (scopes) => { setSelectedScopes( getValues("clientScopes").map((s) => scopes.find((c) => c.id === s.id)!) ); setScopes(localeSort(scopes, mapByKey("name"))); }, [] ); return ( } fieldId="clientScopes" helperTextInvalid={t("requiredClientScope")} validated={errors.clientScopes ? "error" : "default"} isRequired > value.filter((c) => c.id).length > 0, }} render={({ onChange, value }) => ( <> {open && ( !value.map((c: RequiredIdValue) => c.id).includes(scope.id!) )} isClientScopesConditionType open={open} toggleDialog={() => setOpen(!open)} onAdd={(scopes) => { setSelectedScopes([ ...selectedScopes, ...scopes.map((s) => s.scope), ]); onChange([ ...value, ...scopes .map((scope) => scope.scope) .map((item) => ({ id: item.id!, required: false })), ]); }} /> )} )} /> {selectedScopes.length > 0 && ( {t("clientScope")} {t("required")} {selectedScopes.map((scope, index) => ( {scope.name} ( )} />