From 3da00919b717ca2bb44591771aed3cf6e1cc3898 Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Thu, 17 Nov 2022 13:39:29 +0100 Subject: [PATCH] Use `react-form-hook` v7 for flow form (#3785) --- .../src/authentication/DuplicateFlowModal.tsx | 46 +++++++++--------- .../src/authentication/EditFlowModal.tsx | 48 ++++++++----------- .../src/authentication/form/CreateFlow.tsx | 38 +++++++-------- .../src/authentication/form/FlowType.tsx | 22 +++++---- .../authentication/form/NameDescription.tsx | 29 +++++------ 5 files changed, 86 insertions(+), 97 deletions(-) diff --git a/apps/admin-ui/src/authentication/DuplicateFlowModal.tsx b/apps/admin-ui/src/authentication/DuplicateFlowModal.tsx index fc014df5b1..797a186164 100644 --- a/apps/admin-ui/src/authentication/DuplicateFlowModal.tsx +++ b/apps/admin-ui/src/authentication/DuplicateFlowModal.tsx @@ -1,7 +1,4 @@ -import { useEffect } from "react"; -import { useTranslation } from "react-i18next"; -import { FormProvider, useForm } from "react-hook-form"; -import { useNavigate } from "react-router-dom-v5-compat"; +import AuthenticationFlowRepresentation from "@keycloak/keycloak-admin-client/lib/defs/authenticationFlowRepresentation"; import { AlertVariant, Button, @@ -10,12 +7,16 @@ import { Modal, ModalVariant, } from "@patternfly/react-core"; +import { useEffect } from "react"; +import { FormProvider, useForm } from "react-hook-form-v7"; +import { useTranslation } from "react-i18next"; +import { useNavigate } from "react-router-dom-v5-compat"; -import { useAdminClient } from "../context/auth/AdminClient"; import { useAlerts } from "../components/alert/Alerts"; +import { useAdminClient } from "../context/auth/AdminClient"; +import { useRealm } from "../context/realm-context/RealmContext"; import { NameDescription } from "./form/NameDescription"; import { toFlow } from "./routes/Flow"; -import { useRealm } from "../context/realm-context/RealmContext"; type DuplicateFlowModalProps = { name: string; @@ -31,27 +32,25 @@ export const DuplicateFlowModal = ({ onComplete, }: DuplicateFlowModalProps) => { const { t } = useTranslation("authentication"); - const form = useForm({ - shouldUnregister: false, - }); - const { setValue, trigger, getValues } = form; + const form = useForm({ mode: "onChange" }); + const { setValue, trigger, getValues, handleSubmit } = form; const { adminClient } = useAdminClient(); const { addAlert, addError } = useAlerts(); const navigate = useNavigate(); const { realm } = useRealm(); useEffect(() => { - setValue("description", description); setValue("alias", t("copyOf", { name })); - }, [name, description, setValue]); + setValue("description", description); + }, [name, description]); - const save = async () => { + const onSubmit = async () => { if (!(await trigger())) return; const form = getValues(); try { await adminClient.authenticationManagement.copyFlow({ flow: name, - newName: form.alias, + newName: form.alias!, }); const newFlow = ( await adminClient.authenticationManagement.getFlows() @@ -82,33 +81,34 @@ export const DuplicateFlowModal = ({ return ( {t("duplicate")} , , ]} + isOpen > -
+
diff --git a/apps/admin-ui/src/authentication/EditFlowModal.tsx b/apps/admin-ui/src/authentication/EditFlowModal.tsx index 32d4b19b2c..dff61ff59c 100644 --- a/apps/admin-ui/src/authentication/EditFlowModal.tsx +++ b/apps/admin-ui/src/authentication/EditFlowModal.tsx @@ -1,6 +1,4 @@ -import { useEffect } from "react"; -import { useTranslation } from "react-i18next"; -import { FormProvider, useForm } from "react-hook-form"; +import type AuthenticationFlowRepresentation from "@keycloak/keycloak-admin-client/lib/defs/authenticationFlowRepresentation"; import { AlertVariant, Button, @@ -9,10 +7,12 @@ import { Modal, ModalVariant, } from "@patternfly/react-core"; +import { useEffect } from "react"; +import { FormProvider, useForm } from "react-hook-form-v7"; +import { useTranslation } from "react-i18next"; -import type AuthenticationFlowRepresentation from "@keycloak/keycloak-admin-client/lib/defs/authenticationFlowRepresentation"; -import { useAdminClient } from "../context/auth/AdminClient"; import { useAlerts } from "../components/alert/Alerts"; +import { useAdminClient } from "../context/auth/AdminClient"; import { NameDescription } from "./form/NameDescription"; type EditFlowModalProps = { @@ -20,29 +20,20 @@ type EditFlowModalProps = { toggleDialog: () => void; }; -type EditForm = { - alias: string; - description: string; -}; - export const EditFlowModal = ({ flow, toggleDialog }: EditFlowModalProps) => { const { t } = useTranslation("authentication"); - const form = useForm({ - shouldUnregister: false, - }); - const { reset, handleSubmit } = form; const { adminClient } = useAdminClient(); const { addAlert, addError } = useAlerts(); + const form = useForm({ mode: "onChange" }); + const { reset, handleSubmit } = form; - useEffect(() => { - reset(flow); - }, [flow, reset]); + useEffect(() => reset(flow), [flow]); - const save = async (values: EditForm) => { + const onSubmit = async (formValues: AuthenticationFlowRepresentation) => { try { await adminClient.authenticationManagement.updateFlow( { flowId: flow.id! }, - { ...flow, ...values } + { ...flow, ...formValues } ); addAlert(t("updateFlowSuccess"), AlertVariant.success); } catch (error) { @@ -54,33 +45,34 @@ export const EditFlowModal = ({ flow, toggleDialog }: EditFlowModalProps) => { return ( {t("edit")} , , ]} + isOpen > -
+
diff --git a/apps/admin-ui/src/authentication/form/CreateFlow.tsx b/apps/admin-ui/src/authentication/form/CreateFlow.tsx index 896b89887e..b8265c0fd2 100644 --- a/apps/admin-ui/src/authentication/form/CreateFlow.tsx +++ b/apps/admin-ui/src/authentication/form/CreateFlow.tsx @@ -1,38 +1,36 @@ -import { Link } from "react-router-dom-v5-compat"; -import { useNavigate } from "react-router-dom-v5-compat"; -import { useTranslation } from "react-i18next"; -import { FormProvider, useForm } from "react-hook-form"; +import type AuthenticationFlowRepresentation from "@keycloak/keycloak-admin-client/lib/defs/authenticationFlowRepresentation"; import { ActionGroup, AlertVariant, Button, PageSection, } from "@patternfly/react-core"; +import { FormProvider, useForm } from "react-hook-form-v7"; +import { useTranslation } from "react-i18next"; +import { Link, useNavigate } from "react-router-dom-v5-compat"; -import type AuthenticationFlowRepresentation from "@keycloak/keycloak-admin-client/lib/defs/authenticationFlowRepresentation"; -import { ViewHeader } from "../../components/view-header/ViewHeader"; -import { FormAccess } from "../../components/form-access/FormAccess"; -import { useRealm } from "../../context/realm-context/RealmContext"; -import { useAdminClient } from "../../context/auth/AdminClient"; import { useAlerts } from "../../components/alert/Alerts"; -import { NameDescription } from "./NameDescription"; -import { FlowType } from "./FlowType"; -import { toFlow } from "../routes/Flow"; +import { FormAccess } from "../../components/form-access/FormAccess"; +import { ViewHeader } from "../../components/view-header/ViewHeader"; +import { useAdminClient } from "../../context/auth/AdminClient"; +import { useRealm } from "../../context/realm-context/RealmContext"; import { toAuthentication } from "../routes/Authentication"; +import { toFlow } from "../routes/Flow"; +import { FlowType } from "./FlowType"; +import { NameDescription } from "./NameDescription"; export default function CreateFlow() { const { t } = useTranslation("authentication"); const navigate = useNavigate(); const { realm } = useRealm(); - const form = useForm({ - defaultValues: { builtIn: false, topLevel: true }, - }); - const { handleSubmit, register } = form; - const { adminClient } = useAdminClient(); const { addAlert } = useAlerts(); + const form = useForm(); + const { handleSubmit } = form; + + const onSubmit = async (formValues: AuthenticationFlowRepresentation) => { + const flow = { ...formValues, builtIn: false, topLevel: true }; - const save = async (flow: AuthenticationFlowRepresentation) => { try { const { id } = await adminClient.authenticationManagement.createFlow( flow @@ -66,10 +64,8 @@ export default function CreateFlow() { - - diff --git a/apps/admin-ui/src/authentication/form/FlowType.tsx b/apps/admin-ui/src/authentication/form/FlowType.tsx index f405df63fc..09cf1e83c1 100644 --- a/apps/admin-ui/src/authentication/form/FlowType.tsx +++ b/apps/admin-ui/src/authentication/form/FlowType.tsx @@ -1,12 +1,13 @@ -import { useState } from "react"; -import { useTranslation } from "react-i18next"; -import { Controller, useFormContext } from "react-hook-form"; +import AuthenticationFlowRepresentation from "@keycloak/keycloak-admin-client/lib/defs/authenticationFlowRepresentation"; import { FormGroup, Select, SelectOption, SelectVariant, } from "@patternfly/react-core"; +import { useState } from "react"; +import { Controller, useFormContext } from "react-hook-form-v7"; +import { useTranslation } from "react-i18next"; import { HelpItem } from "../../components/help-enabler/HelpItem"; @@ -14,8 +15,7 @@ const TYPES = ["basic-flow", "client-flow"] as const; export const FlowType = () => { const { t } = useTranslation("authentication"); - const { control } = useFormContext(); - + const { control } = useFormContext(); const [open, setOpen] = useState(false); return ( @@ -33,21 +33,25 @@ export const FlowType = () => { name="providerId" defaultValue={TYPES[0]} control={control} - render={({ onChange, value }) => ( + render={({ field }) => (