From 12977d929c59dbdb834857cb1aef0c65fe3ec4e2 Mon Sep 17 00:00:00 2001 From: agagancarczyk Date: Mon, 21 Mar 2022 14:46:01 +0000 Subject: [PATCH] Create attribute - required toggle in general settings (#2279) * fixed required in general settings * feedback * indentation fix Co-authored-by: Agnieszka Gancarczyk --- .../attribute/AttributeGeneralSettings.tsx | 252 ++++++++++-------- 1 file changed, 135 insertions(+), 117 deletions(-) diff --git a/src/realm-settings/user-profile/attribute/AttributeGeneralSettings.tsx b/src/realm-settings/user-profile/attribute/AttributeGeneralSettings.tsx index c66fe943d2..fefb77d513 100644 --- a/src/realm-settings/user-profile/attribute/AttributeGeneralSettings.tsx +++ b/src/realm-settings/user-profile/attribute/AttributeGeneralSettings.tsx @@ -13,7 +13,7 @@ import { } from "@patternfly/react-core"; import { useTranslation } from "react-i18next"; import { HelpItem } from "../../../components/help-enabler/HelpItem"; -import { Controller, useFormContext } from "react-hook-form"; +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"; @@ -21,8 +21,10 @@ 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; + { 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"); @@ -37,6 +39,12 @@ export const AttributeGeneralSettings = () => { 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) => { @@ -137,7 +145,7 @@ export const AttributeGeneralSettings = () => { name="enabledWhen" data-testid="enabledWhen" control={form.control} - defaultValue={ENABLED_REQUIRED_WHEN[0]} + defaultValue={ENABLED_REQUIRED_WHEN[0]} render={({ onChange, value }) => ( <> {ENABLED_REQUIRED_WHEN.map((option) => ( @@ -163,7 +171,7 @@ export const AttributeGeneralSettings = () => { { onSelect={(_, selectedValue) => { const option = selectedValue.toString(); let changedValue = [""]; - if (value) { + if (value) { changedValue = value.includes(option) ? value.filter((item) => item !== option) : [...value, option]; } else { changedValue = [option]; - } - + } + onChange(changedValue); }} onClear={(selectedValues) => { @@ -225,126 +233,136 @@ export const AttributeGeneralSettings = () => { > ( onChange([`${value}`])} - isChecked={value[0] === "true"} + onChange={(value) => { + onChange(value); + form.setValue("required", value); + }} + isChecked={value === true} label={t("common:on")} labelOff={t("common:off")} /> )} > - - ( -
- {REQUIRED_FOR.map((option) => ( - onChange(Array.isArray(option.value) ? option.value : [option.value])} - label={option.label} - className="kc-requiredFor-option" - /> - ))} -
- )} - /> -
- - ( - <> - {ENABLED_REQUIRED_WHEN.map((option) => ( - { - onChange(option); - setRequiredWhenSelection(option); + {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[]; + }) => ( + setSelectRequiredForOpen(isOpen)} - selections={value} - onSelect={(_, selectedValue) => { - const option = selectedValue.toString(); - let changedValue = [""]; - if (value) { - changedValue = value.includes(option) - ? value.filter((item) => item !== option) - : [...value, option]; - } else { - changedValue = [option]; - } - onChange(changedValue); - }} - onClear={(selectedValues) => { - selectedValues.stopPropagation(); - onChange([]); - }} - isOpen={selectRequiredForOpen} - isDisabled={requiredWhenSelection === "Always"} - aria-labelledby={"scope"} - > - {clientScopes?.map((option) => ( - - ))} - - )} - /> - + onToggle={(isOpen) => setSelectRequiredForOpen(isOpen)} + selections={value} + onSelect={(_, selectedValue) => { + const option = selectedValue.toString(); + let changedValue = [""]; + if (value) { + changedValue = value.includes(option) + ? value.filter((item) => item !== option) + : [...value, option]; + } else { + changedValue = [option]; + } + onChange(changedValue); + }} + onClear={(selectedValues) => { + selectedValues.stopPropagation(); + onChange([]); + }} + isOpen={selectRequiredForOpen} + isDisabled={requiredWhenSelection === "Always"} + aria-labelledby={"scope"} + > + {clientScopes?.map((option) => ( + + ))} + + )} + /> +
+ + )} ); };