From fa77455c3eafe01146288d7f7d70859c36bec707 Mon Sep 17 00:00:00 2001 From: Pablo Lara Date: Tue, 24 Sep 2024 08:09:02 +0200 Subject: [PATCH] feat: edit provider has client validation now --- actions/providers.ts | 6 ++-- components/providers/forms/EditForm.tsx | 45 +++++++++++++++---------- components/ui/custom/CustomInputNew.tsx | 44 ++++++++++++++++++++++++ components/ui/custom/index.ts | 1 + types/{auth.ts => authFormSchema.ts} | 0 types/formSchemas.ts | 15 +++++++++ types/index.ts | 3 +- 7 files changed, 92 insertions(+), 22 deletions(-) create mode 100644 components/ui/custom/CustomInputNew.tsx rename types/{auth.ts => authFormSchema.ts} (100%) create mode 100644 types/formSchemas.ts diff --git a/actions/providers.ts b/actions/providers.ts index 178ece1df1..d4b0dc4d29 100644 --- a/actions/providers.ts +++ b/actions/providers.ts @@ -76,8 +76,8 @@ export const updateProvider = async (formData: FormData) => { const keyServer = process.env.API_BASE_URL; const tenantId = session?.user.tenantId; - const providerId = formData.get("id"); - const alias = formData.get("alias"); + const providerId = formData.get("providerId"); + const providerAlias = formData.get("alias"); const url = new URL(`${keyServer}/providers/${providerId}`); @@ -93,7 +93,7 @@ export const updateProvider = async (formData: FormData) => { type: "Provider", id: providerId, attributes: { - alias: alias, + alias: providerAlias, }, }, }), diff --git a/components/providers/forms/EditForm.tsx b/components/providers/forms/EditForm.tsx index 55774a1b57..1404382296 100644 --- a/components/providers/forms/EditForm.tsx +++ b/components/providers/forms/EditForm.tsx @@ -8,13 +8,9 @@ import * as z from "zod"; import { updateProvider } from "@/actions"; import { useToast } from "@/components/ui"; -import { CustomInput } from "@/components/ui/custom"; +import { CustomInputNew } from "@/components/ui/custom"; import { Form } from "@/components/ui/form"; - -const formSchema = z.object({ - alias: z.string(), - providerId: z.string(), -}); +import { editProviderFormSchema } from "@/types"; export const EditForm = ({ providerId, @@ -25,18 +21,26 @@ export const EditForm = ({ providerAlias?: string; setIsOpen: Dispatch>; }) => { + const formSchema = editProviderFormSchema(providerAlias ?? ""); + const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { - alias: "", + providerId: providerId, + alias: providerAlias, }, }); const { toast } = useToast(); const isLoading = form.formState.isSubmitting; - async function onSubmitClient(formData: FormData) { - // client-side validation + const onSubmitClient = async (values: z.infer) => { + const formData = new FormData(); + + Object.entries(values).forEach( + ([key, value]) => value !== undefined && formData.append(key, value), + ); + const data = await updateProvider(formData); if (data?.errors && data.errors.length > 0) { @@ -54,18 +58,27 @@ export const EditForm = ({ description: "The provider was updated successfully.", }); } - } + }; return (
- +
Current alias: {providerAlias}
- */} + + setIsOpen(false)} > {isLoading ? ( - <> - - Saving... - + ) : ( Save )} diff --git a/components/ui/custom/CustomInputNew.tsx b/components/ui/custom/CustomInputNew.tsx new file mode 100644 index 0000000000..a5f16436be --- /dev/null +++ b/components/ui/custom/CustomInputNew.tsx @@ -0,0 +1,44 @@ +import { Input } from "@nextui-org/react"; +import { Control, FieldPath, FieldValues } from "react-hook-form"; + +import { FormControl, FormField, FormMessage } from "@/components/ui/form"; + +interface CustomInputNewProps { + control: Control; + name: FieldPath; + label: string; + type: string; + placeholder?: string; + isRequired?: boolean; +} + +export const CustomInputNew = ({ + control, + name, + label, + type, + placeholder, + isRequired = false, +}: CustomInputNewProps) => { + return ( + ( + <> + + + + + + )} + /> + ); +}; diff --git a/components/ui/custom/index.ts b/components/ui/custom/index.ts index 0520404750..d36dafac6e 100644 --- a/components/ui/custom/index.ts +++ b/components/ui/custom/index.ts @@ -2,4 +2,5 @@ export * from "./CustomAlertModal"; export * from "./CustomBox"; export * from "./CustomButtonClientAction"; export * from "./CustomInput"; +export * from "./CustomInputNew"; export * from "./CustomLoader"; diff --git a/types/auth.ts b/types/authFormSchema.ts similarity index 100% rename from types/auth.ts rename to types/authFormSchema.ts diff --git a/types/formSchemas.ts b/types/formSchemas.ts new file mode 100644 index 0000000000..ee174f95d0 --- /dev/null +++ b/types/formSchemas.ts @@ -0,0 +1,15 @@ +import { z } from "zod"; + +export const editProviderFormSchema = (currentAlias: string) => + z.object({ + alias: z + .string() + .refine((val) => val === "" || val.length >= 3, { + message: "The alias must be empty or have at least 3 characters.", + }) + .refine((val) => val !== currentAlias, { + message: "The new alias must be different from the current one.", + }) + .optional(), + providerId: z.string(), + }); diff --git a/types/index.ts b/types/index.ts index 89e43941c9..955dd0d6f8 100644 --- a/types/index.ts +++ b/types/index.ts @@ -1,2 +1,3 @@ -export * from "./auth"; +export * from "./authFormSchema"; export * from "./components"; +export * from "./formSchemas";