/* eslint-disable @typescript-eslint/no-unnecessary-condition */ /* eslint-disable prettier/prettier */ import React, { useState } from "react"; import { Divider, FormGroup, Radio, Select, SelectOption, SelectVariant, Switch, TextInput, } from "@patternfly/react-core"; import { useTranslation } from "react-i18next"; import { HelpItem } from "../../../components/help-enabler/HelpItem"; import { Controller, useFormContext, useWatch } from "react-hook-form"; import { FormAccess } from "../../../components/form-access/FormAccess"; import { useAdminClient, useFetch } from "../../../context/auth/AdminClient"; import type ClientScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientScopeRepresentation"; import "../../realm-settings-section.css"; const ENABLED_REQUIRED_WHEN = ["Always", "Scopes are requested"] as const; const REQUIRED_FOR = [ { label: "Both users and admins", value: ["admin", "user"] }, { label: "Only users", value: ["user"] }, { label: "Only admins", value: ["admin"] }, ] as const; export const AttributeGeneralSettings = () => { const { t } = useTranslation("realm-settings"); const adminClient = useAdminClient(); const form = useFormContext(); const [clientScopes, setClientScopes] = useState(); const [selectEnabledWhenOpen, setSelectEnabledWhenOpen] = useState(false); const [selectRequiredForOpen, setSelectRequiredForOpen] = useState(false); const [isAttributeGroupDropdownOpen, setIsAttributeGroupDropdownOpen] = useState(false); const [enabledWhenSelection, setEnabledWhenSelection] = useState("Always"); const [requiredWhenSelection, setRequiredWhenSelection] = useState("Always"); const requiredToggle = useWatch({ control: form.control, name: "required", defaultValue: false, }); useFetch( () => adminClient.clientScopes.find(), (clientScopes) => { setClientScopes(clientScopes); }, [] ); return ( } fieldId="kc-attribute-name" isRequired validated={form.errors.name ? "error" : "default"} helperTextInvalid={form.errors.name?.message} > } fieldId="kc-attribute-display-name" > } fieldId="kc-attribute-group" > ( )} > ( <> {ENABLED_REQUIRED_WHEN.map((option) => ( { onChange(option); setEnabledWhenSelection(option); }} label={option} className="pf-u-mb-md" /> ))} )} /> void; value: string[]; }) => ( )} /> } fieldId="kc-required" hasNoPaddingTop > ( { onChange(value); form.setValue("required", value); }} isChecked={value === true} label={t("common:on")} labelOff={t("common:off")} /> )} > {requiredToggle && ( <> (
{REQUIRED_FOR.map((option) => ( onChange(option.value)} label={option.label} className="kc-requiredFor-option" /> ))}
)} />
( <> {ENABLED_REQUIRED_WHEN.map((option) => ( { onChange(option); setRequiredWhenSelection(option); }} label={option} className="pf-u-mb-md" /> ))} )} /> void; value: string[]; }) => ( )} /> )}
); };