From 03e7ec760cbe6b69e3d9fd1410c49e599ef5a7aa Mon Sep 17 00:00:00 2001 From: mfrances17 <39063664+mfrances17@users.noreply.github.com> Date: Fri, 18 Dec 2020 04:20:11 -0500 Subject: [PATCH] Show/hide Kerberos cache policy related controls (#267) * preliminary usewatch changes * show hide working * fix switches with form access * make auth-pw control edit mode --- .../kerberos/KerberosSettingsCache.tsx | 346 +++++++++--------- .../kerberos/KerberosSettingsRequired.tsx | 107 +++--- 2 files changed, 239 insertions(+), 214 deletions(-) diff --git a/src/user-federation/kerberos/KerberosSettingsCache.tsx b/src/user-federation/kerberos/KerberosSettingsCache.tsx index 48fa886c5c..1bcd083f1b 100644 --- a/src/user-federation/kerberos/KerberosSettingsCache.tsx +++ b/src/user-federation/kerberos/KerberosSettingsCache.tsx @@ -8,12 +8,13 @@ import { import { useTranslation } from "react-i18next"; import { HelpItem } from "../../components/help-enabler/HelpItem"; import React, { useEffect, useState } from "react"; -import { useForm, Controller } from "react-hook-form"; import { convertToFormValues } from "../../util"; +import { useForm, Controller, useWatch } from "react-hook-form"; import ComponentRepresentation from "keycloak-admin/lib/defs/componentRepresentation"; import { FormAccess } from "../../components/form-access/FormAccess"; import { useAdminClient } from "../../context/auth/AdminClient"; import { useParams } from "react-router-dom"; +import _ from "lodash"; export const KerberosSettingsCache = () => { const { t } = useTranslation("user-federation"); @@ -23,37 +24,16 @@ export const KerberosSettingsCache = () => { const { register, control, setValue } = useForm(); const { id } = useParams<{ id: string }>(); - const convertToDays = (num: string) => { - switch (num) { - case "1": - return t("common:Sunday"); - case "2": - return t("common:Monday"); - case "3": - return t("common:Tuesday"); - case "4": - return t("common:Wednesday"); - case "5": - return t("common:Thursday"); - case "6": - return t("common:Friday"); - case "7": - return t("common:Saturday"); - default: - return t("common:selectOne"); - } - }; + const cachePolicyType = useWatch({ + control: control, + name: "config.cachePolicy", + }); const setupForm = (component: ComponentRepresentation) => { Object.entries(component).map((entry) => { + setValue("config.cachePolicy", component.config?.cachePolicy); if (entry[0] === "config") { convertToFormValues(entry[1], "config", setValue); - if (entry[1].evictionDay) { - setValue( - "config.evictionDay", - convertToDays(entry[1].evictionDay[0]) - ); - } } else { setValue(entry[0], entry[1]); } @@ -87,14 +67,14 @@ export const KerberosSettingsCache = () => { , ]; for (let index = 1; index <= 24; index++) { - hourOptions.push(); + hourOptions.push(); } const minuteOptions = [ , ]; for (let index = 1; index <= 60; index++) { - minuteOptions.push(); + minuteOptions.push(); } return ( @@ -136,150 +116,182 @@ export const KerberosSettingsCache = () => { value={t("common:selectOne")} isPlaceholder /> - - - - - + + + + + )} > - - } - fieldId="kc-eviction-day" - > - ( - + setIsEvictionDayDropdownOpen(!isEvictionDayDropdownOpen) + } + isOpen={isEvictionDayDropdownOpen} + onSelect={(_, value) => { + onChange(value as string); + setIsEvictionDayDropdownOpen(false); + }} + selections={value} + variant={SelectVariant.single} + > + + + {t("common:Sunday")} + + + {t("common:Monday")} + + + {t("common:Tuesday")} + + + {t("common:Wednesday")} + + + {t("common:Thursday")} + + + {t("common:Friday")} + + + {t("common:Saturday")} + + + )} + > + + ) : ( + <> + )} + + {_.isEqual(cachePolicyType, ["EVICT_DAILY"]) || + _.isEqual(cachePolicyType, ["EVICT_WEEKLY"]) ? ( + <> + - - - - - - - - - )} - > - - {/* TODO: Investigate whether this should be a number field instead of a dropdown/text field */} - + ( + + )} + > + + + + } + fieldId="kc-eviction-minute" + > + ( + + )} + > + + + ) : ( + <> + )} + {_.isEqual(cachePolicyType, ["MAX_LIFESPAN"]) ? ( + + } + fieldId="kc-max-lifespan" + > + - } - fieldId="kc-eviction-hour" - > - ( - - )} - > - - {/* TODO: Investigate whether this should be a number field instead of a dropdown/text field */} - - } - fieldId="kc-eviction-minute" - > - ( - - )} - > - - - } - fieldId="kc-max-lifespan" - > - - + + ) : ( + <> + )} ); diff --git a/src/user-federation/kerberos/KerberosSettingsRequired.tsx b/src/user-federation/kerberos/KerberosSettingsRequired.tsx index 025b869023..eacc036703 100644 --- a/src/user-federation/kerberos/KerberosSettingsRequired.tsx +++ b/src/user-federation/kerberos/KerberosSettingsRequired.tsx @@ -9,12 +9,13 @@ import { } from "@patternfly/react-core"; import { useTranslation } from "react-i18next"; import { HelpItem } from "../../components/help-enabler/HelpItem"; -import { useForm, Controller } from "react-hook-form"; +import { useForm, Controller, useWatch } from "react-hook-form"; import ComponentRepresentation from "keycloak-admin/lib/defs/componentRepresentation"; import { FormAccess } from "../../components/form-access/FormAccess"; import { useAdminClient } from "../../context/auth/AdminClient"; import { useParams } from "react-router-dom"; import { convertToFormValues } from "../../util"; +import _ from "lodash"; export const KerberosSettingsRequired = () => { const { t } = useTranslation("user-federation"); @@ -24,8 +25,17 @@ export const KerberosSettingsRequired = () => { const { register, control, setValue } = useForm(); const { id } = useParams<{ id: string }>(); + const allowPassAuth = useWatch({ + control: control, + name: "config.allowPasswordAuthentication", + }); + const setupForm = (component: ComponentRepresentation) => { Object.entries(component).map((entry) => { + setValue( + "config.allowPasswordAuthentication", + component.config?.allowPasswordAuthentication + ); if (entry[0] === "config") { convertToFormValues(entry[1], "config", setValue); } @@ -151,7 +161,7 @@ export const KerberosSettingsRequired = () => { onChange([`${value}`])} isChecked={value[0] === "true"} label={t("common:on")} labelOff={t("common:off")} @@ -180,7 +190,7 @@ export const KerberosSettingsRequired = () => { onChange([`${value}`])} isChecked={value[0] === "true"} label={t("common:on")} labelOff={t("common:off")} @@ -189,49 +199,52 @@ export const KerberosSettingsRequired = () => { > - {/* TODO: Field shows only if allowPasswordAuthentication is TRUE */} - - } - fieldId="kc-edit-mode" - > - {" "} - ( - - )} - > - + {_.isEqual(allowPassAuth, ["true"]) ? ( + + } + fieldId="kc-edit-mode" + > + {" "} + ( + + )} + > + + ) : ( + <> + )} { onChange([`${value}`])} isChecked={value[0] === "true"} label={t("common:on")} labelOff={t("common:off")}