From f1dcf436c23cd5bf240f7996f40ab6685e80bd79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Loipf=C3=BChrer?= Date: Sun, 8 Sep 2024 18:07:20 +0200 Subject: [PATCH] refactor(web): switch group create modal to react-hook-form --- .../apps/web/src/components/FormTextField.tsx | 2 +- .../components/groups/GroupCreateModal.tsx | 130 +++++++----------- 2 files changed, 50 insertions(+), 82 deletions(-) diff --git a/frontend/apps/web/src/components/FormTextField.tsx b/frontend/apps/web/src/components/FormTextField.tsx index d21c86b2..3270813d 100644 --- a/frontend/apps/web/src/components/FormTextField.tsx +++ b/frontend/apps/web/src/components/FormTextField.tsx @@ -4,7 +4,7 @@ import { TextField, TextFieldProps } from "@mui/material"; export type FormTextFieldProps = Omit & { name: string; - control: Control; + control: Control; }; export const FormTextField = ({ name, control, ...props }: FormTextFieldProps) => { diff --git a/frontend/apps/web/src/components/groups/GroupCreateModal.tsx b/frontend/apps/web/src/components/groups/GroupCreateModal.tsx index a00d7da7..99c03b9f 100644 --- a/frontend/apps/web/src/components/groups/GroupCreateModal.tsx +++ b/frontend/apps/web/src/components/groups/GroupCreateModal.tsx @@ -1,22 +1,13 @@ import { createGroup } from "@abrechnung/redux"; -import { - Button, - Checkbox, - Dialog, - DialogActions, - DialogContent, - DialogTitle, - FormControlLabel, - LinearProgress, - TextField, -} from "@mui/material"; -import { Form, Formik, FormikHelpers, FormikProps } from "formik"; -import React, { ReactNode } from "react"; +import { Button, Checkbox, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel } from "@mui/material"; +import * as React from "react"; import { toast } from "react-toastify"; import { z } from "zod"; import { api } from "@/core/api"; import { useAppDispatch } from "@/store"; -import { toFormikValidationSchema } from "@abrechnung/utils"; +import { Controller, useForm } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { FormTextField } from "../FormTextField"; const validationSchema = z.object({ name: z.string({ required_error: "Name is required" }), @@ -39,8 +30,12 @@ interface Props { export const GroupCreateModal: React.FC = ({ show, onClose }) => { const dispatch = useAppDispatch(); + const { control, handleSubmit } = useForm({ + resolver: zodResolver(validationSchema), + defaultValues: initialValues, + }); - const handleSubmit = (values: FormValues, { setSubmitting }: FormikHelpers) => { + const onSubmit = (values: FormValues) => { dispatch( createGroup({ api, @@ -55,12 +50,10 @@ export const GroupCreateModal: React.FC = ({ show, onClose }) => { ) .unwrap() .then(() => { - setSubmitting(false); onClose("completed"); }) .catch((err) => { toast.error(err); - setSubmitting(false); }); }; @@ -68,71 +61,46 @@ export const GroupCreateModal: React.FC = ({ show, onClose }) => { onClose(reason)}> Create Group - - {({ - values, - touched, - errors, - handleBlur, - handleChange, - isSubmitting, - setFieldValue, - }: FormikProps) => ( -
- + + + } /> - - setFieldValue("addUserAccountOnJoin", e.target.checked)} - checked={values.addUserAccountOnJoin} - /> - } - label="Automatically add accounts for newly joined group members" - /> - {isSubmitting && } - - - - - - )} -
+ } + /> + + + + +
);