correciones en las validaciones de crear-editar productos
This commit is contained in:
@@ -10,18 +10,9 @@ import {
|
|||||||
FormMessage,
|
FormMessage,
|
||||||
} from '@repo/shadcn/form';
|
} from '@repo/shadcn/form';
|
||||||
import { Input } from '@repo/shadcn/input';
|
import { Input } from '@repo/shadcn/input';
|
||||||
// import {
|
|
||||||
// Select,
|
|
||||||
// SelectContent,
|
|
||||||
// SelectItem,
|
|
||||||
// SelectTrigger,
|
|
||||||
// SelectValue,
|
|
||||||
// } from '@repo/shadcn/select';
|
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { useCreateUser } from "@/feactures/inventory/hooks/use-mutation";
|
import { useCreateUser } from "@/feactures/inventory/hooks/use-mutation";
|
||||||
import { EditInventory, editInventory } from '@/feactures/inventory/schemas/inventory';
|
import { EditInventory, editInventory, formDataInput } from '@/feactures/inventory/schemas/inventory';
|
||||||
import { parse } from 'path';
|
|
||||||
|
|
||||||
|
|
||||||
interface CreateFormProps {
|
interface CreateFormProps {
|
||||||
onSuccess?: () => void;
|
onSuccess?: () => void;
|
||||||
@@ -31,8 +22,7 @@ interface CreateFormProps {
|
|||||||
|
|
||||||
export function CreateForm({
|
export function CreateForm({
|
||||||
onSuccess,
|
onSuccess,
|
||||||
onCancel,
|
onCancel
|
||||||
defaultValues,
|
|
||||||
}: CreateFormProps) {
|
}: CreateFormProps) {
|
||||||
const {
|
const {
|
||||||
mutate: saveAccountingAccounts,
|
mutate: saveAccountingAccounts,
|
||||||
@@ -40,17 +30,15 @@ export function CreateForm({
|
|||||||
isError,
|
isError,
|
||||||
} = useCreateUser();
|
} = useCreateUser();
|
||||||
|
|
||||||
// const { data: AccoutingAccounts } = useSurveyMutation();
|
|
||||||
|
|
||||||
const defaultformValues = {
|
const defaultformValues = {
|
||||||
title: defaultValues?.title || '',
|
title: '',
|
||||||
description: defaultValues?.description || '',
|
description: '',
|
||||||
price: defaultValues?.price || '',
|
price: '',
|
||||||
stock: defaultValues?.stock || 0,
|
stock: '',
|
||||||
urlImg: defaultValues?.urlImg || ''
|
urlImg: ''
|
||||||
}
|
}
|
||||||
|
|
||||||
const form = useForm<EditInventory>({
|
const form = useForm<formDataInput>({
|
||||||
resolver: zodResolver(editInventory),
|
resolver: zodResolver(editInventory),
|
||||||
defaultValues: defaultformValues,
|
defaultValues: defaultformValues,
|
||||||
mode: 'onChange', // Enable real-time validation
|
mode: 'onChange', // Enable real-time validation
|
||||||
@@ -58,9 +46,7 @@ export function CreateForm({
|
|||||||
|
|
||||||
const onSubmit = async (data: EditInventory) => {
|
const onSubmit = async (data: EditInventory) => {
|
||||||
|
|
||||||
const formData = data
|
saveAccountingAccounts(data, {
|
||||||
|
|
||||||
saveAccountingAccounts(formData, {
|
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
form.reset();
|
form.reset();
|
||||||
onSuccess?.();
|
onSuccess?.();
|
||||||
|
|||||||
@@ -61,7 +61,6 @@ export function AccountPlanModal({
|
|||||||
<CreateForm
|
<CreateForm
|
||||||
onSuccess={handleSuccess}
|
onSuccess={handleSuccess}
|
||||||
onCancel={handleCancel}
|
onCancel={handleCancel}
|
||||||
defaultValues={defaultValues}
|
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
|
|||||||
@@ -10,17 +10,9 @@ import {
|
|||||||
FormMessage,
|
FormMessage,
|
||||||
} from '@repo/shadcn/form';
|
} from '@repo/shadcn/form';
|
||||||
import { Input } from '@repo/shadcn/input';
|
import { Input } from '@repo/shadcn/input';
|
||||||
// import {
|
|
||||||
// Select,
|
|
||||||
// SelectContent,
|
|
||||||
// SelectItem,
|
|
||||||
// SelectTrigger,
|
|
||||||
// SelectValue,
|
|
||||||
// } from '@repo/shadcn/select';
|
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { useUpdateUser } from "@/feactures/inventory/hooks/use-mutation";
|
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 {
|
interface UpdateFormProps {
|
||||||
onSuccess?: () => void;
|
onSuccess?: () => void;
|
||||||
@@ -36,39 +28,37 @@ export function UpdateForm({
|
|||||||
const {
|
const {
|
||||||
mutate: saveAccountingAccounts,
|
mutate: saveAccountingAccounts,
|
||||||
isPending: isSaving,
|
isPending: isSaving,
|
||||||
isError,
|
isError, // isError no se usa en el template, podrías usarlo para mostrar un mensaje global
|
||||||
} = useUpdateUser();
|
} = useUpdateUser();
|
||||||
|
|
||||||
const defaultformValues = {
|
const defaultformValues: formDataInput = {
|
||||||
id: defaultValues?.id,
|
id: defaultValues?.id,
|
||||||
title: defaultValues?.title || '',
|
title: defaultValues?.title || '',
|
||||||
description: defaultValues?.description || '',
|
description: defaultValues?.description || '',
|
||||||
price: defaultValues?.price || '',
|
price: defaultValues?.price || '',
|
||||||
stock: defaultValues?.stock || 0,
|
stock: (defaultValues?.stock ?? '').toString(),
|
||||||
urlImg: defaultValues?.urlImg || '',
|
urlImg: defaultValues?.urlImg || '',
|
||||||
}
|
userId: defaultValues?.userId
|
||||||
|
};
|
||||||
|
|
||||||
// console.log(defaultValues);
|
const form = useForm<formDataInput>({
|
||||||
|
|
||||||
const form = useForm<EditInventory>({
|
|
||||||
resolver: zodResolver(editInventory),
|
resolver: zodResolver(editInventory),
|
||||||
defaultValues: defaultformValues,
|
defaultValues: defaultformValues,
|
||||||
mode: 'onChange', // Enable real-time validation
|
mode: 'onChange', // Enable real-time validation
|
||||||
});
|
});
|
||||||
|
|
||||||
const onSubmit = async (data: EditInventory) => {
|
const onSubmit = async (data: formDataInput) => {
|
||||||
|
|
||||||
const formData = data
|
saveAccountingAccounts(data, {
|
||||||
|
|
||||||
saveAccountingAccounts(formData, {
|
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
form.reset();
|
form.reset();
|
||||||
onSuccess?.();
|
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', {
|
form.setError('root', {
|
||||||
type: 'manual',
|
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({
|
|||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>Precio</FormLabel>
|
<FormLabel>Precio</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input {...field} value={field.value?.toString() ?? ''}/>
|
{/* Simplificado. price es z.string(), field.value ya es string o undefined. */}
|
||||||
|
<Input {...field} />
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
@@ -132,7 +123,8 @@ export function UpdateForm({
|
|||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>Cantidad/Stock</FormLabel>
|
<FormLabel>Cantidad/Stock</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input {...field}/>
|
{/* Añadido type="number" para UX. field.value ya es string debido a formDataInput */}
|
||||||
|
<Input {...field} type="number" />
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
@@ -146,7 +138,7 @@ export function UpdateForm({
|
|||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>Imagen</FormLabel>
|
<FormLabel>Imagen</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input {...field}/>
|
<Input {...field} />
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
export type InventoryTable = z.infer<typeof product>;
|
export type InventoryTable = z.infer<typeof product>;
|
||||||
export type EditInventory = z.infer<typeof editInventory>;
|
export type EditInventory = z.infer<typeof editInventory>; //output
|
||||||
|
export type formDataInput = z.input<typeof editInventory>;
|
||||||
|
|
||||||
export const product = z.object({
|
export const product = z.object({
|
||||||
id: z.number().optional(),
|
id: z.number().optional(),
|
||||||
@@ -18,7 +19,9 @@ export const editInventory = z.object({
|
|||||||
id: z.number().optional(),
|
id: z.number().optional(),
|
||||||
title: z.string().min(5, { message: "Debe de tener 5 o más caracteres" }),
|
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" }),
|
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(),
|
price: z.string(),
|
||||||
urlImg: z.string(),
|
urlImg: z.string(),
|
||||||
userId: z.number().optional(),
|
userId: z.number().optional(),
|
||||||
|
|||||||
Reference in New Issue
Block a user