bug fixed
This commit is contained in:
parent
9b363d88bb
commit
4f083b1f68
@ -88,6 +88,72 @@ const ProductDetails = ({ product, onBack, showToast: shouldShowToast, onToastSh
|
||||
|
||||
const navigate = useNavigate();
|
||||
|
||||
// Function to save resubmission data to localStorage
|
||||
const saveResubmissionData = (productData) => {
|
||||
try {
|
||||
console.log('Saving resubmission data:', productData); // Debug log
|
||||
const resubmissionData = {
|
||||
submissionId: productData.submission_id,
|
||||
product: {
|
||||
id: productData.product?.id,
|
||||
product_id: productData.product_id,
|
||||
product_name: productData.product?.product_name,
|
||||
hs_code: productData.product?.hs_code,
|
||||
unit: productData.unit ? {
|
||||
id: productData.unit.id,
|
||||
uom: productData.unit.uom
|
||||
} : null,
|
||||
// Add all relevant product data that needs to be pre-filled
|
||||
previous_quantity_period_one: productData.previous_quantity_period_one,
|
||||
previous_quantity_period_two: productData.previous_quantity_period_two,
|
||||
previous_quantity_period_three: productData.previous_quantity_period_three,
|
||||
current_quantity_period_one: productData.current_quantity_period_one,
|
||||
current_quantity_period_two: productData.current_quantity_period_two,
|
||||
current_quantity_period_three: productData.current_quantity_period_three,
|
||||
forecast_quantity_period_one: productData.forecast_quantity_period_one,
|
||||
forecast_quantity_period_two: productData.forecast_quantity_period_two,
|
||||
forecast_quantity_period_three: productData.forecast_quantity_period_three,
|
||||
previous_cost_period_one: productData.previous_cost_period_one,
|
||||
previous_cost_period_two: productData.previous_cost_period_two,
|
||||
previous_cost_period_three: productData.previous_cost_period_three,
|
||||
current_cost_period_one: productData.current_cost_period_one,
|
||||
current_cost_period_two: productData.current_cost_period_two,
|
||||
current_cost_period_three: productData.current_cost_period_three,
|
||||
forecast_cost_period_one: productData.forecast_cost_period_one,
|
||||
forecast_cost_period_two: productData.forecast_cost_period_two,
|
||||
forecast_cost_period_three: productData.forecast_cost_period_three,
|
||||
variation_reason: productData.variation_reason,
|
||||
zero_target_reason: productData.zero_target_reason,
|
||||
other_variation_reason: productData.other_variation_reason,
|
||||
other_zero_target_reason: productData.other_zero_target_reason,
|
||||
remarks: productData.remarks,
|
||||
annual_installed_capacity: productData.annual_installed_capacity,
|
||||
// Add quarter and year directly in product data
|
||||
quarter: productData.quarter || productData.product?.quarter,
|
||||
year: productData.year || productData.product?.year
|
||||
},
|
||||
establishment: {
|
||||
id: productData.establishment_id,
|
||||
name: productData.establishment_name,
|
||||
permanent_factory_code: productData.permanent_factory_code,
|
||||
industry_code: productData.industry_code,
|
||||
license_number: productData.license_number,
|
||||
isic_code: productData.isic_code,
|
||||
emirate: productData.emirate
|
||||
},
|
||||
// Add quarter and year at the root level as well
|
||||
quarter: productData.quarter || productData.product?.quarter,
|
||||
year: productData.year || productData.product?.year,
|
||||
timestamp: new Date().toISOString()
|
||||
};
|
||||
|
||||
localStorage.setItem('resubmissionData', JSON.stringify(resubmissionData));
|
||||
console.log('Resubmission data saved to localStorage');
|
||||
} catch (error) {
|
||||
console.error('Error saving resubmission data to localStorage:', error);
|
||||
}
|
||||
};
|
||||
|
||||
if (!product) return null;
|
||||
|
||||
// Format the product data to match the component's expected structure
|
||||
@ -182,25 +248,74 @@ const ProductDetails = ({ product, onBack, showToast: shouldShowToast, onToastSh
|
||||
<span className="text-sm font-medium">{toast.message}</span>
|
||||
{toast.type === 'rejected' && (
|
||||
<button
|
||||
onClick={() => {
|
||||
navigate('/survey', {
|
||||
state: {
|
||||
submission: {
|
||||
id: product.submission_id,
|
||||
quarter: product.quarter,
|
||||
year: product.year,
|
||||
establishment_name: product.establishment_name,
|
||||
permanent_factory_code: product.permanent_factory_code,
|
||||
industry_code: product.industry_code,
|
||||
license_number: product.license_number,
|
||||
isic_code: product.isic_code,
|
||||
emirate: product.emirate,
|
||||
status: product.status // Include status for reference
|
||||
},
|
||||
isResubmit: true,
|
||||
submissionId: product.submission_id // Add submissionId for resubmission
|
||||
onClick={async () => {
|
||||
try {
|
||||
// Make the API call to get the submission details
|
||||
const response = await fetch(`/api/submissions/${product.submission_id}`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch submission data');
|
||||
}
|
||||
});
|
||||
const submissionData = await response.json();
|
||||
|
||||
// Get quarter and year from the product or submission data
|
||||
const quarter = submissionData.quarter || product.quarter || 'Q1'; // Default to Q1 if not found
|
||||
const year = submissionData.year || product.year || new Date().getFullYear().toString(); // Default to current year if not found
|
||||
|
||||
// Prepare the data to be saved
|
||||
const dataToStore = {
|
||||
...submissionData,
|
||||
quarter: quarter,
|
||||
year: year,
|
||||
product: {
|
||||
...submissionData.product,
|
||||
quarter: quarter,
|
||||
year: year
|
||||
},
|
||||
timestamp: new Date().toISOString()
|
||||
};
|
||||
|
||||
// Save to localStorage
|
||||
localStorage.setItem('resubmissionData', JSON.stringify(dataToStore));
|
||||
|
||||
console.log('API response saved to localStorage:', dataToStore);
|
||||
|
||||
// Navigate to the survey page with the submission data
|
||||
navigate('/survey', {
|
||||
state: {
|
||||
submission: {
|
||||
...submissionData,
|
||||
id: product.submission_id,
|
||||
quarter: quarter,
|
||||
year: year,
|
||||
establishment_name: product.establishment_name
|
||||
},
|
||||
isResubmit: true,
|
||||
submissionId: product.submission_id,
|
||||
fromResubmit: true,
|
||||
// Add quarter and year to the root of location state as well
|
||||
quarter: quarter,
|
||||
year: year
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error during resubmission:', error);
|
||||
// Fallback to the previous method if API call fails
|
||||
saveResubmissionData(product);
|
||||
|
||||
navigate('/survey', {
|
||||
state: {
|
||||
submission: {
|
||||
id: product.submission_id,
|
||||
quarter: product.quarter,
|
||||
year: product.year,
|
||||
establishment_name: product.establishment_name
|
||||
},
|
||||
isResubmit: true,
|
||||
submissionId: product.submission_id,
|
||||
fromResubmit: true
|
||||
}
|
||||
});
|
||||
}
|
||||
}}
|
||||
className="ml-auto px-3 py-1 bg-[#FEF2F2] text-[#7C2320] text-sm font-medium rounded hover:bg-[#FEF2F2] transition-colors"
|
||||
>
|
||||
|
||||
@ -151,10 +151,45 @@ const EstablishmentInfo = ({
|
||||
setShowInfoCard(false);
|
||||
};
|
||||
|
||||
// Debug log to see what data is being received
|
||||
// Store establishment data in localStorage during resubmission
|
||||
React.useEffect(() => {
|
||||
console.log('Establishment data:', data);
|
||||
}, [data]);
|
||||
|
||||
// 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,
|
||||
establishment: {
|
||||
id: data.establishmentId,
|
||||
name: data.establishmentName,
|
||||
permanent_factory_code: data.permanentFactoryCode,
|
||||
industry_code: data.industryCode,
|
||||
license_number: data.licenseNumber,
|
||||
isic_code: data.isicCode,
|
||||
emirate: data.emirate,
|
||||
establishment_contact_email: data.establishment_contact_email,
|
||||
employeeInfo: data.employeeInfo || {}
|
||||
},
|
||||
// Ensure quarter and year are included
|
||||
quarter: data.quarter || surveyData.quarter,
|
||||
year: data.year || surveyData.year
|
||||
};
|
||||
|
||||
// Save back to localStorage
|
||||
localStorage.setItem('resubmissionData', JSON.stringify(updatedData));
|
||||
console.log('Updated resubmission data with establishment info:', updatedData);
|
||||
} catch (error) {
|
||||
console.error('Error updating resubmission data with establishment info:', error);
|
||||
}
|
||||
}
|
||||
}, [data, location.state?.fromResubmit, surveyData.quarter, surveyData.year]);
|
||||
|
||||
const info = React.useMemo(
|
||||
() => ({
|
||||
|
||||
@ -515,33 +515,23 @@ const ProductData = ({
|
||||
}
|
||||
}, [products]);
|
||||
|
||||
const [surveyData, setSurveyData] = React.useState({
|
||||
quarter: '',
|
||||
year: '',
|
||||
endDate: ''
|
||||
});
|
||||
const [surveyData, setSurveyData] = React.useState(() => ({
|
||||
quarter: quarter || '',
|
||||
year: year || '',
|
||||
endDate: ''
|
||||
}));
|
||||
|
||||
const location = useLocation();
|
||||
|
||||
// Update surveyData when props or location changes
|
||||
React.useEffect(() => {
|
||||
// Get survey data from navigation state if available
|
||||
if (location.state?.survey) {
|
||||
const { quarter, year, endDate } = location.state.survey;
|
||||
setSurveyData({
|
||||
quarter: quarter || '',
|
||||
year: year || '',
|
||||
endDate: endDate || ''
|
||||
});
|
||||
} else {
|
||||
// Fall back to props if no location state
|
||||
setSurveyData(prev => ({
|
||||
...prev,
|
||||
quarter: quarter || prev.quarter,
|
||||
year: year || prev.year
|
||||
}));
|
||||
}
|
||||
}, [location.state, quarter, year]);
|
||||
// Always use the props directly for quarter and year
|
||||
setSurveyData(prev => ({
|
||||
...prev,
|
||||
quarter: quarter || '',
|
||||
year: year || ''
|
||||
}));
|
||||
}, [quarter, year]);
|
||||
|
||||
const requiredMessage = 'Required';
|
||||
const validateForm = () => {
|
||||
@ -1430,7 +1420,7 @@ const handleProductSelect = async (productId, establishmentId, id) => {
|
||||
value={p.otherVariationReason || ''}
|
||||
onChange={(e) => updateProductField(p.id, 'otherVariationReason', e.target.value)}
|
||||
/>
|
||||
<p className="mt-1 text-xs text-gray-500">Please provide details about the variation reason</p>
|
||||
{/* <p className="mt-1 text-xs text-gray-500">Please provide details about the variation reason</p> */}
|
||||
</div>
|
||||
)}
|
||||
{reasonsError && variationReasons.length === 0 && (
|
||||
@ -1577,7 +1567,7 @@ const handleProductSelect = async (productId, establishmentId, id) => {
|
||||
value={p.otherZeroTargetReason || ''}
|
||||
onChange={(e) => updateProductField(p.id, 'otherZeroTargetReason', e.target.value)}
|
||||
/>
|
||||
<p className="mt-1 text-xs text-gray-500">Please provide details about the zero target reason</p>
|
||||
{/* <p className="mt-1 text-xs text-gray-500">Please provide details about the zero target reason</p> */}
|
||||
</div>
|
||||
)}
|
||||
{zeroReasonsError && zeroTargetReasons.length === 0 && (
|
||||
|
||||
@ -532,12 +532,11 @@ const ValidationReview = () => {
|
||||
colSpan="3"
|
||||
className="px-4 py-3 text-center bg-[#E9F6EC] text-[#1E7C34] font-medium rounded-md"
|
||||
>
|
||||
{product?.variation_reason?.reason || 'No reason provided'}
|
||||
{product.other_variation_reason && (
|
||||
<div className="text-xs mt-1">
|
||||
Note: {product.other_variation_reason}
|
||||
</div>
|
||||
)}
|
||||
{product.other_variation_reason ||
|
||||
(product?.variation_reason?.reason ?
|
||||
(product.variation_reason.reason.startsWith('Other (specify)') ? '' : product.variation_reason.reason)
|
||||
: 'No reason provided')
|
||||
}
|
||||
</td>
|
||||
<td colSpan="3" className="text-center text-[#6B7280]">
|
||||
--
|
||||
@ -555,33 +554,32 @@ const ValidationReview = () => {
|
||||
<td colSpan="3" className="text-center text-[#6B7280]">
|
||||
--
|
||||
</td>
|
||||
<td
|
||||
colSpan="3"
|
||||
className="px-4 py-3 text-center bg-[#FFF2E0] text-[#D97706] font-medium rounded-md"
|
||||
>
|
||||
{product?.zero_target_reason?.reason || 'No reason provided'}
|
||||
{product.other_zero_target_reason && (
|
||||
<div className="text-xs mt-1">
|
||||
Note: {product.other_zero_target_reason}
|
||||
</div>
|
||||
)}
|
||||
</td>
|
||||
<td
|
||||
colSpan="3"
|
||||
className="px-4 py-3 text-center bg-[#FFF2E0] text-[#D97706] font-medium rounded-md"
|
||||
>
|
||||
{product.other_zero_target_reason ||
|
||||
(product?.zero_target_reason?.reason ?
|
||||
(product.zero_target_reason.reason.startsWith('Other (specify)') ? '' : product.zero_target_reason.reason)
|
||||
: 'No reason provided')
|
||||
}
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<div className="mt-3 text-sm text-[#4B5563]">
|
||||
<div className="mt-3 ml-4 text-sm text-[#4B5563]">
|
||||
<strong className="font-semibold text-[#232528]">Remarks:</strong>{' '}
|
||||
{product.remarks || 'NA'}
|
||||
</div>
|
||||
|
||||
<div className="mt-3">
|
||||
<div className="mt-3 ml-4">
|
||||
<label className="block text-sm font-semibold mb-1 text-[#232528]">
|
||||
Admin Remarks
|
||||
</label>
|
||||
<textarea
|
||||
rows="2"
|
||||
className="w-full rounded-[10px] border border-[#D1D5DB] bg-[#FFFCF2] px-4 py-2 text-sm text-[#232528] focus:ring-2 focus:ring-[#92722A] focus:outline-none"
|
||||
className="w-[1100px] rounded-[10px] border border-[#D1D5DB] bg-[#FFFCF2] px-4 py-2 mb-4 text-sm text-[#232528] focus:ring-2 focus:ring-[#92722A] focus:outline-none"
|
||||
placeholder="Please enter remarks here"
|
||||
></textarea>
|
||||
</div>
|
||||
@ -698,7 +696,7 @@ const ValidationReview = () => {
|
||||
type="button"
|
||||
className="inline-flex h-11 items-center justify-center rounded-[10px] border border-[#B52520] px-6 text-sm font-semibold text-[#B52520] transition-colors hover:bg-[#FEE2E2] disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
onClick={() => setIsRejectOpen(true)}
|
||||
disabled={actionLoading || submissionData?.status === 'Rejected'}
|
||||
disabled={actionLoading || submissionData?.status === 'Rejected' || submissionData?.status === 'Approved'}
|
||||
>
|
||||
{submissionData?.status === 'Rejected' ? 'Already Rejected' : 'Reject'}
|
||||
</button>
|
||||
@ -706,7 +704,7 @@ const ValidationReview = () => {
|
||||
type="button"
|
||||
className="inline-flex h-11 items-center justify-center rounded-[10px] bg-[#92722A] px-6 text-sm font-semibold text-white transition-colors hover:bg-[#B68A35] disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
onClick={() => setIsApproveOpen(true)}
|
||||
disabled={actionLoading || submissionData?.status === 'Approved'}
|
||||
disabled={actionLoading || submissionData?.status === 'Approved' || submissionData?.status === 'Rejected'}
|
||||
>
|
||||
{actionLoading ? 'Processing...' : submissionData?.status === 'Approved' ? 'Already Approved' : 'Approve'}
|
||||
</button>
|
||||
|
||||
@ -466,9 +466,22 @@ const Survey = () => {
|
||||
const buildSubmissionPayload = React.useCallback((isResubmitFlow = false) => {
|
||||
const info = establishmentData?.employeeInfo ?? {};
|
||||
|
||||
// Get quarter and year from surveyData if available, otherwise from establishmentData
|
||||
const quarter = surveyData?.quarter || '';
|
||||
const year = surveyData?.year || '';
|
||||
// Check for resubmission data in localStorage
|
||||
let quarter = surveyData?.quarter || '';
|
||||
let year = surveyData?.year || '';
|
||||
|
||||
if (isResubmitFlow) {
|
||||
const resubmitData = localStorage.getItem('resubmissionData');
|
||||
if (resubmitData) {
|
||||
try {
|
||||
const parsedData = JSON.parse(resubmitData);
|
||||
quarter = parsedData.quarter || quarter;
|
||||
year = parsedData.year || year;
|
||||
} catch (e) {
|
||||
console.error('Error parsing resubmission data:', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
quarter: quarter,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user