From f5962efb8b2abdef23261c4e81a90cca17d6dc49 Mon Sep 17 00:00:00 2001 From: Sergio Ramirez Date: Fri, 27 Jun 2025 14:38:08 -0400 Subject: [PATCH] correciones en las validaciones de crear-editar productos --- .../inventory/create-product-form.tsx | 32 ++++-------- .../components/inventory/inventory-modal.tsx | 1 - .../inventory/update-product-form.tsx | 50 ++++++++----------- .../feactures/inventory/schemas/inventory.ts | 7 ++- 4 files changed, 35 insertions(+), 55 deletions(-) diff --git a/apps/web/feactures/inventory/components/inventory/create-product-form.tsx b/apps/web/feactures/inventory/components/inventory/create-product-form.tsx index 297721e..b241d9c 100644 --- a/apps/web/feactures/inventory/components/inventory/create-product-form.tsx +++ b/apps/web/feactures/inventory/components/inventory/create-product-form.tsx @@ -10,18 +10,9 @@ import { FormMessage, } from '@repo/shadcn/form'; import { Input } from '@repo/shadcn/input'; -// import { -// Select, -// SelectContent, -// SelectItem, -// SelectTrigger, -// SelectValue, -// } from '@repo/shadcn/select'; import { useForm } from 'react-hook-form'; import { useCreateUser } from "@/feactures/inventory/hooks/use-mutation"; -import { EditInventory, editInventory } from '@/feactures/inventory/schemas/inventory'; -import { parse } from 'path'; - +import { EditInventory, editInventory, formDataInput } from '@/feactures/inventory/schemas/inventory'; interface CreateFormProps { onSuccess?: () => void; @@ -31,8 +22,7 @@ interface CreateFormProps { export function CreateForm({ onSuccess, - onCancel, - defaultValues, + onCancel }: CreateFormProps) { const { mutate: saveAccountingAccounts, @@ -40,17 +30,15 @@ export function CreateForm({ isError, } = useCreateUser(); - // const { data: AccoutingAccounts } = useSurveyMutation(); - const defaultformValues = { - title: defaultValues?.title || '', - description: defaultValues?.description || '', - price: defaultValues?.price || '', - stock: defaultValues?.stock || 0, - urlImg: defaultValues?.urlImg || '' + title: '', + description: '', + price: '', + stock: '', + urlImg: '' } - const form = useForm({ + const form = useForm({ resolver: zodResolver(editInventory), defaultValues: defaultformValues, mode: 'onChange', // Enable real-time validation @@ -58,9 +46,7 @@ export function CreateForm({ const onSubmit = async (data: EditInventory) => { - const formData = data - - saveAccountingAccounts(formData, { + saveAccountingAccounts(data, { onSuccess: () => { form.reset(); onSuccess?.(); diff --git a/apps/web/feactures/inventory/components/inventory/inventory-modal.tsx b/apps/web/feactures/inventory/components/inventory/inventory-modal.tsx index 28a4348..41a36c6 100644 --- a/apps/web/feactures/inventory/components/inventory/inventory-modal.tsx +++ b/apps/web/feactures/inventory/components/inventory/inventory-modal.tsx @@ -61,7 +61,6 @@ export function AccountPlanModal({ )} diff --git a/apps/web/feactures/inventory/components/inventory/update-product-form.tsx b/apps/web/feactures/inventory/components/inventory/update-product-form.tsx index c4ee45d..a84962f 100644 --- a/apps/web/feactures/inventory/components/inventory/update-product-form.tsx +++ b/apps/web/feactures/inventory/components/inventory/update-product-form.tsx @@ -10,17 +10,9 @@ import { FormMessage, } from '@repo/shadcn/form'; import { Input } from '@repo/shadcn/input'; -// import { -// Select, -// SelectContent, -// SelectItem, -// SelectTrigger, -// SelectValue, -// } from '@repo/shadcn/select'; import { useForm } from 'react-hook-form'; import { useUpdateUser } from "@/feactures/inventory/hooks/use-mutation"; -import { EditInventory, editInventory } from '@/feactures/inventory/schemas/inventory'; - +import { editInventory, formDataInput, EditInventory } from '@/feactures/inventory/schemas/inventory'; // Renombrado EditInventory para claridad interface UpdateFormProps { onSuccess?: () => void; @@ -36,39 +28,37 @@ export function UpdateForm({ const { mutate: saveAccountingAccounts, isPending: isSaving, - isError, + isError, // isError no se usa en el template, podrías usarlo para mostrar un mensaje global } = useUpdateUser(); - const defaultformValues = { - id: defaultValues?.id, + const defaultformValues: formDataInput = { + id: defaultValues?.id, title: defaultValues?.title || '', description: defaultValues?.description || '', - price: defaultValues?.price || '', - stock: defaultValues?.stock || 0, + price: defaultValues?.price || '', + stock: (defaultValues?.stock ?? '').toString(), urlImg: defaultValues?.urlImg || '', - } + userId: defaultValues?.userId + }; - // console.log(defaultValues); - - const form = useForm({ + const form = useForm({ resolver: zodResolver(editInventory), defaultValues: defaultformValues, mode: 'onChange', // Enable real-time validation }); - const onSubmit = async (data: EditInventory) => { - - const formData = data - - saveAccountingAccounts(formData, { + const onSubmit = async (data: formDataInput) => { + + saveAccountingAccounts(data, { onSuccess: () => { form.reset(); onSuccess?.(); }, - onError: () => { + onError: (error) => { // Captura el error para mostrar un mensaje más específico si es posible + console.error("Error al guardar el producto:", error); form.setError('root', { type: 'manual', - message: 'Error al guardar la cuenta contable', + message: error.message || 'Error al guardar el producto', // Mejor mensaje de error }); }, }); @@ -118,7 +108,8 @@ export function UpdateForm({ Precio - + {/* Simplificado. price es z.string(), field.value ya es string o undefined. */} + @@ -132,7 +123,8 @@ export function UpdateForm({ Cantidad/Stock - + {/* Añadido type="number" para UX. field.value ya es string debido a formDataInput */} + @@ -146,7 +138,7 @@ export function UpdateForm({ Imagen - + @@ -165,4 +157,4 @@ export function UpdateForm({ ); -} +} \ No newline at end of file diff --git a/apps/web/feactures/inventory/schemas/inventory.ts b/apps/web/feactures/inventory/schemas/inventory.ts index a20ee35..e37cd34 100644 --- a/apps/web/feactures/inventory/schemas/inventory.ts +++ b/apps/web/feactures/inventory/schemas/inventory.ts @@ -1,7 +1,8 @@ import { z } from 'zod'; export type InventoryTable = z.infer; -export type EditInventory = z.infer; +export type EditInventory = z.infer; //output +export type formDataInput = z.input; export const product = z.object({ id: z.number().optional(), @@ -18,7 +19,9 @@ export const editInventory = z.object({ id: z.number().optional(), title: z.string().min(5, { message: "Debe de tener 5 o más caracteres" }), description: z.string().min(10, { message: "Debe de tener 10 o más caracteres" }), - stock: z.string().transform(val => Number(val)), + stock: z.string().transform(val => Number(val)).pipe(z.number( + { invalid_type_error: 'El stock debe ser un número' }).min(0, { message: "El stock debe ser mayor a 0" }) +), price: z.string(), urlImg: z.string(), userId: z.number().optional(),