diff --git a/src/web/src/features/products/components/ProductForm.tsx b/src/web/src/features/products/components/ProductForm.tsx index df48dab..1c6679e 100644 --- a/src/web/src/features/products/components/ProductForm.tsx +++ b/src/web/src/features/products/components/ProductForm.tsx @@ -12,6 +12,7 @@ import { FormLabel, FormMessage, } from '@/components/ui/form' +import type { ProductTypeListItem } from '@/features/product-types/types' // ─── Schema ─────────────────────────────────────────────────────────────────── @@ -76,6 +77,7 @@ export interface ProductFormDefaultValues { } interface ProductFormProps { + productTypes: ProductTypeListItem[] defaultValues?: ProductFormDefaultValues onSubmit: (values: ProductFormOutput) => void onCancel: () => void @@ -86,6 +88,7 @@ interface ProductFormProps { // ─── Component ──────────────────────────────────────────────────────────────── export function ProductForm({ + productTypes, defaultValues, onSubmit, onCancel, @@ -117,8 +120,21 @@ export function ProductForm({ // eslint-disable-next-line react-hooks/exhaustive-deps }, [defaultValues?.nombre, defaultValues?.medioId, defaultValues?.productTypeId]) + // Derive selected ProductType flags + const productTypeIdStr = form.watch('productTypeId') + const selectedProductTypeId = productTypeIdStr ? Number(productTypeIdStr) : null + const selectedProductType = productTypes.find((pt) => pt.id === selectedProductTypeId) ?? null + const requiresCategory = selectedProductType?.requiresCategory ?? false + const hasDuration = selectedProductType?.hasDuration ?? false + function handleSubmit(data: ProductFormOutput) { - onSubmit(data) + // Normalize conditional fields to null when not applicable + const normalized: ProductFormOutput = { + ...data, + rubroId: requiresCategory ? data.rubroId : null, + priceDurationDays: hasDuration ? data.priceDurationDays : null, + } + onSubmit(normalized) } return ( @@ -194,27 +210,29 @@ export function ProductForm({ )} /> - {/* Rubro ID (optional) */} - ( - - ID de Rubro (opcional) - - - - - - )} - /> + {/* Rubro ID — only shown when requiresCategory=true */} + {requiresCategory && ( + ( + + ID de Rubro + + + + + + )} + /> + )} {/* Base Price */} - {/* Price Duration Days (optional) */} - ( - - Días de duración del precio (opcional) - - - - - - )} - /> + {/* Price Duration Days — only shown when hasDuration=true */} + {hasDuration && ( + ( + + Días de duración del precio + + + + + + )} + /> + )}