fix
This commit is contained in:
parent
df35fdcc50
commit
5fb960abbd
@ -527,18 +527,32 @@ if (!establishment) {
|
|||||||
|
|
||||||
// Prepare products data in the required format
|
// Prepare products data in the required format
|
||||||
const formattedProducts = products.map(product => {
|
const formattedProducts = products.map(product => {
|
||||||
// Get the selected product option to access product_id
|
// Find the existing product in the current submission's products
|
||||||
const selectedProduct = productOptions.find(p => p.value === product.product);
|
const selectedProduct = productOptions.find(p => p.value === product.product);
|
||||||
|
console.log('Product matching debug:', {
|
||||||
|
product,
|
||||||
|
selectedProduct,
|
||||||
|
productOptions,
|
||||||
|
currentSubmissionProducts: currentSubmission.products
|
||||||
|
});
|
||||||
const existingProduct = currentSubmission.products?.find(p =>
|
const existingProduct = currentSubmission.products?.find(p =>
|
||||||
p.product_id === (selectedProduct?.product_id || product.product_id) ||
|
p.product_id == product.product_id || // Loose equality to handle string/number mismatch
|
||||||
p.product?.id === (selectedProduct?.product_id || product.product_id)
|
p.product?.id == product.product_id // Also check nested product.id
|
||||||
);
|
);
|
||||||
|
console.log('Product matching debug:', {
|
||||||
|
productId: product.product_id,
|
||||||
|
productIdType: typeof product.product_id,
|
||||||
|
existingProduct,
|
||||||
|
currentSubmissionProducts: currentSubmission.products
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
// Use the existing product ID if found, otherwise use 0 for new products
|
||||||
id: existingProduct?.id || 0,
|
id: existingProduct?.id || 0,
|
||||||
product_id: selectedProduct?.product_id || product.product_id || null,
|
product_id: product.product_id || null,
|
||||||
unit_id: product.unit?.toString() || '', // Ensure unit_id is a string
|
unit_id: product.unit?.toString() || '',
|
||||||
annual_installed_capacity: product.capacity || '0',
|
annual_installed_capacity: product.capacity || '0',
|
||||||
|
// ... rest of the product fields remain the same
|
||||||
previous_quantity_period_one: product.octQuantity || '0',
|
previous_quantity_period_one: product.octQuantity || '0',
|
||||||
previous_quantity_period_two: product.novQuantity || '0',
|
previous_quantity_period_two: product.novQuantity || '0',
|
||||||
previous_quantity_period_three: product.decQuantity || '0',
|
previous_quantity_period_three: product.decQuantity || '0',
|
||||||
@ -557,21 +571,17 @@ const existingProduct = currentSubmission.products?.find(p =>
|
|||||||
forecast_cost_period_one: product.aprCost || '0',
|
forecast_cost_period_one: product.aprCost || '0',
|
||||||
forecast_cost_period_two: product.mayCost || '0',
|
forecast_cost_period_two: product.mayCost || '0',
|
||||||
forecast_cost_period_three: product.junCost || '0',
|
forecast_cost_period_three: product.junCost || '0',
|
||||||
// Previous quarter (Q4) - October
|
|
||||||
previous_quantity: product.octQuantity || '0',
|
previous_quantity: product.octQuantity || '0',
|
||||||
previous_cost: product.octCost || '0',
|
previous_cost: product.octCost || '0',
|
||||||
// Current quarter (Q1) - Using November as current (as per previous implementation)
|
|
||||||
// If you want to use a different field for current, replace novQuantity/novCost with the appropriate field
|
|
||||||
current_quantity: product.novQuantity || '0',
|
current_quantity: product.novQuantity || '0',
|
||||||
current_cost: product.novCost || '0',
|
current_cost: product.novCost || '0',
|
||||||
// Next quarter forecast (Q2) - December
|
|
||||||
forecast_quantity: product.decQuantity || '0',
|
forecast_quantity: product.decQuantity || '0',
|
||||||
forecast_cost: product.decCost || '0',
|
forecast_cost: product.decCost || '0',
|
||||||
variation_reason_master_id: product.variationReason || '',
|
variation_reason_master_id: product.variationReason || '',
|
||||||
other_variation_reason: product.otherVariationReason || '',
|
other_variation_reason: product.otherVariationReason || '',
|
||||||
zero_target_reason_master_id: product.zeroTargetReason || '',
|
zero_target_reason_master_id: product.zeroTargetReason || '',
|
||||||
other_zero_target_reason: product.otherZeroTargetReason || '',
|
other_zero_target_reason: product.otherZeroTargetReason || '',
|
||||||
remarks: product.remarks || remarks || '' // Use product.remarks if available, otherwise use the component's remarks state
|
remarks: product.remarks || remarks || ''
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -598,9 +608,13 @@ const existingProduct = currentSubmission.products?.find(p =>
|
|||||||
response = await resubmitSurvey(submissionId, payload);
|
response = await resubmitSurvey(submissionId, payload);
|
||||||
setToast({
|
setToast({
|
||||||
show: true,
|
show: true,
|
||||||
message: 'Draft updated successfully',
|
message: 'Draft Updated Successfully',
|
||||||
type: 'success'
|
type: 'success'
|
||||||
});
|
});
|
||||||
|
setTimeout(() => {
|
||||||
|
window.location.href = '/dashboard'; // Update this path if your dashboard route is different
|
||||||
|
}, 1000);
|
||||||
|
// setShowDraftSaved(true);
|
||||||
} else {
|
} else {
|
||||||
// Create new submission
|
// Create new submission
|
||||||
response = await submitSurvey(payload);
|
response = await submitSurvey(payload);
|
||||||
@ -609,38 +623,20 @@ const existingProduct = currentSubmission.products?.find(p =>
|
|||||||
if (response && response.submission_data) {
|
if (response && response.submission_data) {
|
||||||
localStorage.setItem('currentSubmission', JSON.stringify(response.submission_data));
|
localStorage.setItem('currentSubmission', JSON.stringify(response.submission_data));
|
||||||
}
|
}
|
||||||
|
|
||||||
setToast({
|
setToast({
|
||||||
show: true,
|
show: true,
|
||||||
message: 'Draft saved successfully',
|
message: 'Draft Saved Successfully',
|
||||||
type: 'success'
|
type: 'success'
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
// Redirect to dashboard after a short delay to show the success message
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.location.href = '/dashboard';
|
window.location.href = '/dashboard'; // Update this path if your dashboard route is different
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
// setShowDraftSaved(true);
|
||||||
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error details:', {
|
console.error('Error saving draft:', error);
|
||||||
message: error.message,
|
alert(error.message || 'Failed to save draft');
|
||||||
response: error.response?.data,
|
|
||||||
status: error.response?.status,
|
|
||||||
config: {
|
|
||||||
url: error.config?.url,
|
|
||||||
method: error.config?.method,
|
|
||||||
data: error.config?.data
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
setToast({
|
|
||||||
show: true,
|
|
||||||
message: error.message || 'Failed to save draft',
|
|
||||||
type: 'error'
|
|
||||||
});
|
|
||||||
} finally {
|
} finally {
|
||||||
setIsSavingDraft(false);
|
setIsSavingDraft(false);
|
||||||
}
|
}
|
||||||
@ -681,14 +677,14 @@ const existingProduct = currentSubmission.products?.find(p =>
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-5">
|
<div className="space-y-5">
|
||||||
{toast.show && (
|
{toast.show && toast.message && (
|
||||||
<CustomToast
|
<CustomToast
|
||||||
message={toast.message}
|
message={toast.message}
|
||||||
type={toast.type}
|
type={toast.type}
|
||||||
onClose={() => setToast(prev => ({ ...prev, show: false }))}
|
onClose={() => setToast(prev => ({ ...prev, show: false }))}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{toast && (
|
{toast.show && toast.message && (
|
||||||
<div
|
<div
|
||||||
className={`flex items-start justify-between gap-3 rounded-md border px-4 py-3 text-sm ${
|
className={`flex items-start justify-between gap-3 rounded-md border px-4 py-3 text-sm ${
|
||||||
toast.type === 'success'
|
toast.type === 'success'
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user