correciones en las validaciones de crear-editar productos
This commit is contained in:
@@ -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<EditInventory>({
|
||||
const form = useForm<formDataInput>({
|
||||
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?.();
|
||||
|
||||
@@ -61,7 +61,6 @@ export function AccountPlanModal({
|
||||
<CreateForm
|
||||
onSuccess={handleSuccess}
|
||||
onCancel={handleCancel}
|
||||
defaultValues={defaultValues}
|
||||
/>
|
||||
)}
|
||||
</DialogContent>
|
||||
|
||||
@@ -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<EditInventory>({
|
||||
const form = useForm<formDataInput>({
|
||||
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({
|
||||
<FormItem>
|
||||
<FormLabel>Precio</FormLabel>
|
||||
<FormControl>
|
||||
<Input {...field} value={field.value?.toString() ?? ''}/>
|
||||
{/* Simplificado. price es z.string(), field.value ya es string o undefined. */}
|
||||
<Input {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
@@ -132,7 +123,8 @@ export function UpdateForm({
|
||||
<FormItem>
|
||||
<FormLabel>Cantidad/Stock</FormLabel>
|
||||
<FormControl>
|
||||
<Input {...field}/>
|
||||
{/* Añadido type="number" para UX. field.value ya es string debido a formDataInput */}
|
||||
<Input {...field} type="number" />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
@@ -146,7 +138,7 @@ export function UpdateForm({
|
||||
<FormItem>
|
||||
<FormLabel>Imagen</FormLabel>
|
||||
<FormControl>
|
||||
<Input {...field}/>
|
||||
<Input {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
@@ -165,4 +157,4 @@ export function UpdateForm({
|
||||
</form>
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user