From 8c708f9bd7821e9dda1d3643e0aeb617bc7f9726 Mon Sep 17 00:00:00 2001 From: Malini Date: Tue, 27 Jan 2026 15:33:28 +0530 Subject: [PATCH] changed in resumitted --- .../EstablishmentInfo/EstablishmentInfo.jsx | 221 ++++++++++-------- .../survey/ProductData/ProductData.jsx | 67 +++++- .../Admin/configuration/CompanyProfile.jsx | 2 +- .../configuration/EditCompanyProfile.jsx | 17 +- 4 files changed, 195 insertions(+), 112 deletions(-) diff --git a/ipi-survey-platform/src/components/survey/EstablishmentInfo/EstablishmentInfo.jsx b/ipi-survey-platform/src/components/survey/EstablishmentInfo/EstablishmentInfo.jsx index 4cee3ec..fde278e 100644 --- a/ipi-survey-platform/src/components/survey/EstablishmentInfo/EstablishmentInfo.jsx +++ b/ipi-survey-platform/src/components/survey/EstablishmentInfo/EstablishmentInfo.jsx @@ -1,5 +1,5 @@ import React, { useEffect, useState, useMemo } from 'react'; - + // Get and encode establishment ID for URL const getSafeEstablishmentId = () => { const establishmentId = localStorage.getItem('establishment_id') || ''; @@ -8,7 +8,7 @@ const getSafeEstablishmentId = () => { import { useNavigate, useLocation } from 'react-router-dom'; import { getEstablishmentProducts } from '../../../services/submissions/submissionService'; import Table from '@/components/common/Table'; - + const caretDownSrc = '/assets/images/CaretDown.svg'; const backVectorSrc = '/assets/images/BackVector.svg'; const mailIconSrc = '/assets/images/material-symbols_mail-outline.svg'; @@ -16,7 +16,7 @@ const phoneIconSrc = '/assets/images/line-md_phone.svg'; const calendarIcon = '/assets/images/Duedate.svg'; const clockIcon = '/assets/images/mingcute_time-line.svg'; const nextIcon = '/assets/images/mdi_page-next-outline.svg'; - + const Field = ({ label, placeholder = '', type = 'text', value = '', onChange = () => {}, disabled = true }) => (
); - + const Select = ({ label, options = [], value = '', placeholder = 'Select option', onChange = () => {}, disabled = true }) => (
@@ -61,7 +61,7 @@ const Select = ({ label, options = [], value = '', placeholder = 'Select option'
); - + const Card = ({ children }) => (
@@ -69,7 +69,7 @@ const Card = ({ children }) => (
); - + const defaultEmployeeInfo = { emiratiMale: '', nonEmiratiMale: '', @@ -78,22 +78,22 @@ const defaultEmployeeInfo = { totalEmployees: '', totalEmirati: '', }; - + const getCurrentQuarterAndYear = () => { const now = new Date(); const currentQuarter = Math.ceil((now.getMonth() + 1) / 3); const currentYear = now.getFullYear(); - + // Calculate end of quarter date const endOfQuarter = new Date(currentYear, currentQuarter * 3, 0); // Last day of the last month in quarter - + return { quarter: `Q${currentQuarter}`, year: currentYear, endDate: endOfQuarter.toISOString() // Return as ISO string for consistency }; }; - + const EstablishmentInfo = ({ data = {}, onChange = () => {}, @@ -111,13 +111,13 @@ const EstablishmentInfo = ({ const location = useLocation(); // Use a ref to persist the survey data across re-renders const surveyDataRef = React.useRef(null); - + // Fetch establishment products React.useEffect(() => { const fetchProducts = async () => { const establishmentId = localStorage.getItem('establishment_id'); if (!establishmentId) return; - + setLoadingProducts(true); try { const response = await getEstablishmentProducts(establishmentId); @@ -133,89 +133,120 @@ const EstablishmentInfo = ({ setLoadingProducts(false); } }; - + fetchProducts(); }, []); - + const [surveyData, setSurveyData] = React.useState(() => { - // Initialize from location state if available, otherwise use defaults + // For resubmission, use the quarter/year from the submission data + if (location.state?.fromResubmit && data?.quarter && data?.year) { + const surveyPeriod = { + quarter: data.quarter, + year: data.year, + endDate: data.end_date || '' + }; + localStorage.setItem('currentSurveyPeriod', JSON.stringify(surveyPeriod)); + return surveyPeriod; + } + + // For new survey, use the quarter/year from location state if available if (location.state?.survey) { const { quarter, year, endDate } = location.state.survey; - - // Save to localStorage const surveyPeriod = { quarter, year, endDate }; localStorage.setItem('currentSurveyPeriod', JSON.stringify(surveyPeriod)); - - return { - quarter: quarter || '', - year: year || '', - endDate: endDate || '' - }; + return surveyPeriod; } - // Or use current quarter/year as fallback + + // Fallback to current quarter/year const current = getCurrentQuarterAndYear(); - - // Save to localStorage const surveyPeriod = { quarter: current.quarter, year: current.year, endDate: current.endDate }; localStorage.setItem('currentSurveyPeriod', JSON.stringify(surveyPeriod)); - - return { - quarter: current.quarter, - year: current.year, - endDate: current.endDate - }; + return surveyPeriod; }); - + // Log when surveyData changes React.useEffect(() => { }, [surveyData]); - + // Update the ref whenever surveyData changes React.useEffect(() => { surveyDataRef.current = surveyData; }, [surveyData]); - + + // Use a ref to track if we've already set the initial survey data + const hasInitialized = React.useRef(false); + React.useEffect(() => { - // Only update if we don't already have survey data - if (!surveyDataRef.current?.quarter && location.state?.survey) { + // Skip if we've already initialized or if we don't have the necessary data yet + if (hasInitialized.current) return; + + // For resubmission, ensure we use the submission's quarter/year + if (location.state?.fromResubmit && data?.quarter && data?.year) { + const newSurveyData = { + quarter: data.quarter, + year: data.year, + endDate: data.end_date || '' + }; + + // Only update if the values have changed + if (JSON.stringify(surveyData) !== JSON.stringify(newSurveyData)) { + setSurveyData(newSurveyData); + + // Update the parent component with the survey data + onChange({ + ...data, + quarter: newSurveyData.quarter, + year: newSurveyData.year, + end_date: newSurveyData.endDate + }); + } + + hasInitialized.current = true; + } + // For new survey, use the quarter/year from location state if available + else if (location.state?.survey && !surveyData.quarter) { const { quarter, year, endDate } = location.state.survey; const newSurveyData = { quarter: quarter || '', year: year || '', endDate: endDate || '' }; - - setSurveyData(newSurveyData); - - // Update the parent component with the survey data - onChange({ - ...data, - quarter: newSurveyData.quarter, - year: newSurveyData.year, - end_date: newSurveyData.endDate - }); + + // Only update if the values have changed + if (JSON.stringify(surveyData) !== JSON.stringify(newSurveyData)) { + setSurveyData(newSurveyData); + + // Update the parent component with the survey data + onChange({ + ...data, + quarter: newSurveyData.quarter, + year: newSurveyData.year, + end_date: newSurveyData.endDate + }); + } + + hasInitialized.current = true; } - // Only run this effect when the component mounts or location.state changes - }, [location.state, data, onChange]); - + }, [location.state, data, onChange, surveyData]); + const handleCloseInfo = () => { setShowInfoCard(false); }; - + // Store establishment data in localStorage during resubmission React.useEffect(() => { // Check if this is a resubmission const isResubmit = location.state?.fromResubmit; - + if (isResubmit && data) { try { // Get existing resubmission data if it exists const existingData = JSON.parse(localStorage.getItem('resubmissionData') || '{}'); - + // Update with establishment data const updatedData = { ...existingData, @@ -234,7 +265,7 @@ const EstablishmentInfo = ({ quarter: data.quarter || surveyData.quarter, year: data.year || surveyData.year }; - + // Save back to localStorage localStorage.setItem('resubmissionData', JSON.stringify(updatedData)); } catch (error) { @@ -242,7 +273,7 @@ const EstablishmentInfo = ({ } } }, [data, location.state?.fromResubmit, surveyData.quarter, surveyData.year]); - + const info = React.useMemo( () => ({ quarter: surveyData.quarter || '', @@ -262,23 +293,23 @@ const EstablishmentInfo = ({ }), [data, surveyData.quarter, surveyData.year] ); - + const handleFieldChange = (field) => (event) => { onChange({ ...info, [field]: event.target.value, }); }; - + // const handleEditProfile = (e) => { // e.preventDefault(); - + // const establishmentId = sessionStorage.getItem('establishment_id'); // if (!establishmentId) { // alert('No establishment ID found in session'); // return; // } - + // localStorage.setItem('returnTo', window.location.pathname); // sessionStorage.setItem('edit_profile_from', 'EstablishmentUser'); // // navigate(`/profile/edit/${establishmentId}`); @@ -306,7 +337,7 @@ const EstablishmentInfo = ({ }, }); }; - + return (
{loading && ( @@ -317,7 +348,7 @@ const EstablishmentInfo = ({

IIP (Index of Industrial Production): {surveyData.quarter} {surveyData.year}

- + {/* Survey Information */} {!showInfoCard && ( - +

You're completing the IIP Quarterly Survey for {surveyData.quarter} {surveyData.year}. This step shows establishment details already registered with us. Step 1 is read-only. To make corrections, update them in Profile → Edit Profile, then return to this survey.

- +
@@ -400,13 +431,13 @@ const EstablishmentInfo = ({
)} - +

Step 1: Review Establishment Information

{isComplete && ( - Completed )} @@ -431,10 +462,10 @@ const EstablishmentInfo = ({ ))} - open
@@ -445,7 +476,7 @@ const EstablishmentInfo = ({ value={surveyData.year} onChange={handleFieldChange('year')} disabled={true} - + className="block w-full h-10 rounded-md border-2 border-[#E6D7A2] focus:border-[#92722A] focus:ring-0 px-4 pr-10 text-sm bg-white text-[#9EA2A9] appearance-none cursor-not-allowed" > @@ -453,17 +484,17 @@ const EstablishmentInfo = ({ ))} - open
- + {/* Establishment Information Form */}

Establishment Information

@@ -474,7 +505,7 @@ const EstablishmentInfo = ({ value={info.establishmentName} onChange={handleFieldChange('establishmentName')} disabled={true} - + /> {/* Email field is hidden but data remains in form state */}
- +

Products

{loadingProducts ? ( @@ -544,7 +575,7 @@ const EstablishmentInfo = ({
No products found for this establishment.
)}
- +
{/* Employee Information */} @@ -600,7 +631,7 @@ const EstablishmentInfo = ({ />
- + {/*

Products

{loadingProducts ? ( @@ -653,8 +684,8 @@ const EstablishmentInfo = ({ )}
*/} {/* Products Table */} - - + + {/* Blue Info Box */}
Edit Profile @@ -699,11 +730,11 @@ const EstablishmentInfo = ({ > Edit Profile - + . Changes saved there will appear here.

- + {/* Footer actions */}
- + {(loading || error) && (
{loading &&

Loading establishment information…

} @@ -735,5 +766,5 @@ const EstablishmentInfo = ({
); }; - -export default EstablishmentInfo; + +export default EstablishmentInfo; \ No newline at end of file diff --git a/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx b/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx index efef5ef..06e3282 100644 --- a/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx +++ b/ipi-survey-platform/src/components/survey/ProductData/ProductData.jsx @@ -333,7 +333,15 @@ const SectionBox = ({ title, children, badgeBg = 'bg-white', badgeText = 'text-g
- + {title}
@@ -400,15 +408,30 @@ const ProductData = ({ useEffect(() => { const fetchQuarterPeriods = async () => { try { - // Try to get survey period from localStorage - const savedSurveyPeriod = localStorage.getItem('currentSurveyPeriod'); let periodYear = year; let periodQuarter = quarter; - - if (savedSurveyPeriod) { - const { quarter: savedQuarter, year: savedYear } = JSON.parse(savedSurveyPeriod); - periodYear = savedYear || year; - periodQuarter = savedQuarter || quarter; + + // For resubmission, use the submission's quarter/year + if (location.state?.fromResubmit && quarter && year) { + periodYear = year; + periodQuarter = quarter; + } + // For new survey, use the quarter/year from location state if available + else if (location.state?.survey) { + const { quarter: surveyQuarter, year: surveyYear } = location.state.survey; + if (surveyQuarter && surveyYear) { + periodQuarter = surveyQuarter; + periodYear = surveyYear; + } + } + // Fallback to localStorage or props + else { + const savedSurveyPeriod = localStorage.getItem('currentSurveyPeriod'); + if (savedSurveyPeriod) { + const { quarter: savedQuarter, year: savedYear } = JSON.parse(savedSurveyPeriod); + periodYear = savedYear || year; + periodQuarter = savedQuarter || quarter; + } } if (!periodQuarter || !periodYear) { @@ -417,7 +440,7 @@ const ProductData = ({ } setIsLoadingPeriods(true); - const quarterNumber = periodQuarter.replace('Q', ''); // Convert 'Q3' to '3' + const quarterNumber = periodQuarter.startsWith('Q') ? periodQuarter.replace('Q', '') : periodQuarter; const response = await getQuarterPeriods(parseInt(periodYear), `Q${quarterNumber}`); setQuarterPeriods(response.data); } catch (error) { @@ -1108,6 +1131,28 @@ const location = useLocation(); displayYear = savedYear || year; } + // For resubmission, use the submission's quarter/year + if (location.state?.fromResubmit && quarter && year) { + return ( +
+ Quarter: {quarter}-{year} +
+ ); + } + + // For new survey, use the quarter/year from location state if available + if (location.state?.survey) { + const { quarter: surveyQuarter, year: surveyYear } = location.state.survey; + if (surveyQuarter && surveyYear) { + return ( +
+ Quarter: {surveyQuarter}-{surveyYear} +
+ ); + } + } + + // Fallback to the values from props or local storage return (displayQuarter || displayYear) ? (
Quarter: {displayQuarter}-{displayYear} @@ -1550,8 +1595,8 @@ const location = useLocation();
diff --git a/ipi-survey-platform/src/pages/Admin/configuration/CompanyProfile.jsx b/ipi-survey-platform/src/pages/Admin/configuration/CompanyProfile.jsx index 729491b..2424f3b 100644 --- a/ipi-survey-platform/src/pages/Admin/configuration/CompanyProfile.jsx +++ b/ipi-survey-platform/src/pages/Admin/configuration/CompanyProfile.jsx @@ -2025,7 +2025,7 @@ const displayedRows = sortedProfiles.map((item) => { emirate: establishmentEmirateName, isicCode: item?.isic_code ?? '', industryCodeBusiness: item?.industry_code, - industryCodeProduction: item?.industry_code_production, + industryCodeProduction: item?.isic_code, industryCodeCurrent: item?.industryCodeCurrent, industryCodeMismatchRemarks: item?.industry_code_mismatch_remarks ?? '', industryDescription: item?.description ?? base.industryDescription, diff --git a/ipi-survey-platform/src/pages/Admin/configuration/EditCompanyProfile.jsx b/ipi-survey-platform/src/pages/Admin/configuration/EditCompanyProfile.jsx index e17b1f6..a0048b2 100644 --- a/ipi-survey-platform/src/pages/Admin/configuration/EditCompanyProfile.jsx +++ b/ipi-survey-platform/src/pages/Admin/configuration/EditCompanyProfile.jsx @@ -42,9 +42,9 @@ const mapApiEstablishmentToProfile = (apiData) => { userProfileEmail: data.users?.[0]?.email || '', permanentFactoryCode: data.permanent_factory_code || '', uniqueLicenseNumber: data.license_number || '', - industryCodeBusiness: data.industry_code_production || '', - industryCodeCurrent: data.industry_code || '', - industryCodeMismatchRemarks: data.industry_code_mismatch_remarks || '', + industryCodeBusiness: data.industry_code || '', + industryCodeCurrent: data.isic_code || '', + industryCodeMismatchRemarks: data.industry_code_mismatch_remarks || '', contactName: data.factory_name || '', contactAddress: data.establishment_address || '', contactCityTown: data.establishment_city?.name || '', @@ -197,7 +197,13 @@ const EditCompanyProfile = () => { if (field === 'userProfileConfirmPassword') { setConfirmError(""); } - }, []); + if (field === 'industryCodeMismatchRemarks') { + setForm(prev => ({ + ...prev, + industryCodeMismatchRemarks: value + })); + } + }, [fieldErrors]); const handleNext = useCallback(() => { if (validateStepFields(activeStep)) { @@ -581,6 +587,7 @@ const EditCompanyProfile = () => { industry_code: form.industryCodeBusiness || "", license_number: form.uniqueLicenseNumber || "", industry_code_production: form.industryCodeCurrent || "", + industry_code_mismatch_remarks: form.industryCodeMismatchRemarks || null, description: form.industryDescription || "", establishment_address: form.contactAddress || "", establishment_city_town_id: selectedCity ? Number(selectedCity.value) : null, @@ -730,7 +737,7 @@ const EditCompanyProfile = () => { rows={3} />
-{form.industryCodeCurrent && form.industryCodeBusiness !== form.industryCodeCurrent && ( +{form.industryCodeBusiness && form.industryCodeBusiness !== form.industryCodeCurrent && (