diff --git a/frontend/src/app/products/page.tsx b/frontend/src/app/products/page.tsx index 36c06df..3a658b7 100644 --- a/frontend/src/app/products/page.tsx +++ b/frontend/src/app/products/page.tsx @@ -40,6 +40,10 @@ export default function ProductsPage() { name: '', description: '', }); + const [errors, setErrors] = useState({ + name: '', + description: '', + }); useEffect(() => { loadProducts(); @@ -54,8 +58,29 @@ export default function ProductsPage() { } }; + const validateForm = () => { + const newErrors = { + name: '', + description: '', + }; + + // Product name cannot be empty + if (!formData.name.trim()) { + newErrors.name = 'Product name is required'; + } + + // Description can be empty, no validation needed + setErrors(newErrors); + return !newErrors.name; + }; + const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); + + if (!validateForm()) { + return; + } + try { if (editingProduct) { await productApi.update(editingProduct.id, formData); @@ -65,6 +90,7 @@ export default function ProductsPage() { setIsDialogOpen(false); setEditingProduct(null); setFormData({ name: '', description: '' }); + setErrors({ name: '', description: '' }); loadProducts(); } catch (error) { console.error('Error saving product:', error); @@ -77,6 +103,7 @@ export default function ProductsPage() { name: product.name, description: product.description, }); + setErrors({ name: '', description: '' }); setIsDialogOpen(true); }; @@ -91,6 +118,14 @@ export default function ProductsPage() { } }; + const handleInputChange = (field: 'name' | 'description', value: string) => { + setFormData({ ...formData, [field]: value }); + // Clear error when user starts typing + if (errors[field]) { + setErrors({ ...errors, [field]: '' }); + } + }; + return (