diff --git a/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx b/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx index 7d62043..3389238 100644 --- a/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx +++ b/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx @@ -603,6 +603,19 @@ useEffect(() => { const handleSaveDraft = async () => { try { + // Validate draft form before saving + if (!validateDraftForm()) { + // Scroll to first error + const firstErrorKey = Object.keys(formErrors)[0]; + if (firstErrorKey) { + const element = document.querySelector(`[data-field="${firstErrorKey}"]`); + if (element) { + element.scrollIntoView({ behavior: 'smooth', block: 'center' }); + } + } + return; + } + setIsSavingDraft(true); // Get current submission from localStorage @@ -1180,6 +1193,55 @@ useEffect(() => { return isValid; }; + // Validation function for Save as Draft - requires at least one field to be filled + const validateDraftForm = () => { + const errors = {}; + let hasAnyData = false; + + products.forEach((product, index) => { + // Check if product has any data + const hasProduct = product.product || product.productId || product.product_id; + const hasUnit = product.unit || product.unit_id; + const hasCapacity = product.capacity; + const hasQuantity = product.octQuantity || product.novQuantity || product.decQuantity || + product.janQuantity || product.febQuantity || product.marQuantity || + product.aprQuantity || product.mayQuantity || product.junQuantity; + const hasCost = product.octCost || product.novCost || product.decCost || + product.janCost || product.febCost || product.marCost || + product.aprCost || product.mayCost || product.junCost; + const hasVariationReason = product.variationReason || product.otherVariationReason; + const hasZeroReason = product.zeroTargetReason || product.otherZeroTargetReason; + const hasRemarks = product.remarks; + + // If any field has data, mark hasAnyData as true + if (hasProduct || hasUnit || hasCapacity || hasQuantity || hasCost || + hasVariationReason || hasZeroReason || hasRemarks) { + hasAnyData = true; + } + + // If product has some data, validate mandatory fields + if (hasProduct || hasUnit || hasCapacity || hasQuantity || hasCost) { + // Product is mandatory if any other field is filled + if (!hasProduct) { + errors[`product_${index}`] = 'Please enter product field'; + } + + // Unit is mandatory if product is selected or any quantity/cost is entered + if (!hasUnit && (hasProduct || hasQuantity || hasCost)) { + errors[`unit_${index}`] = 'Unit is required when product or quantities are entered'; + } + } + }); + + // Check if at least one product has any data + if (!hasAnyData) { + errors['draft_general'] = 'Please enter at least one field before saving as draft'; + } + + setFormErrors(errors); + return Object.keys(errors).length === 0; + }; + const handleNext = () => { if (validateForm()) { onNext(); @@ -1782,7 +1844,7 @@ useEffect(() => {
Monthly entries: Enter Qty and Cost for each month in the Current and Forecast quarters. Use 0 where there is no output or cost.
Number only: Use a decimal point where needed. No negative values.
Units: Use one measurement unit per product across all months.
-Delete: Removing a product here affects this survey only; it does not change your profile.
+ {/*Delete: Removing a product here affects this survey only; it does not change your profile.
*/} )} @@ -2453,18 +2515,19 @@ useEffect(() => { - {(hasError || isInitialLoad) && ( + {(hasError || isInitialLoad || Object.keys(formErrors).length > 0) && (Loading product data...
)} - {hasError && ( + {(hasError || Object.keys(formErrors).length > 0) && ({error}
} {reasonsError &&{reasonsError}
} {zeroReasonsError &&{zeroReasonsError}
} {productsError &&{productsError}
} {unitsError &&{unitsError}
} + {formErrors['draft_general'] &&{formErrors['draft_general']}
}A simple quarterly cycle
@@ -65,7 +65,7 @@ const Cards = () => { ))}+
Accounts are issued by FCSC. There is no self-registration.
- Support hours: Sun–Thu, 8:00–16:00 GST + Support hours: Monday–Friday, 8:00–16:00 GST
diff --git a/ipi-survey-platform/src/pages/LandingPage/component/Industrial.jsx b/ipi-survey-platform/src/pages/LandingPage/component/Industrial.jsx index 0047e9d..5c6418a 100644 --- a/ipi-survey-platform/src/pages/LandingPage/component/Industrial.jsx +++ b/ipi-survey-platform/src/pages/LandingPage/component/Industrial.jsx @@ -30,10 +30,10 @@ const Industrial = () => { Reference area: United Arab Emirates