bug fixed
This commit is contained in:
parent
01df11d1fe
commit
f4c59bf3da
@ -499,10 +499,9 @@ const ProductData = ({
|
|||||||
const selectedProduct = options?.find(opt => opt.value === value);
|
const selectedProduct = options?.find(opt => opt.value === value);
|
||||||
const updatedProduct = {
|
const updatedProduct = {
|
||||||
...product,
|
...product,
|
||||||
product: value,
|
product: selectedProduct?.value || value,
|
||||||
productName: selectedProduct?.label || '',
|
productName: selectedProduct?.label || '',
|
||||||
productId: selectedProduct?.value || '',
|
productId: selectedProduct?.value || ''
|
||||||
[field]: value
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Fetch previous forecast data when product is selected
|
// Fetch previous forecast data when product is selected
|
||||||
@ -807,24 +806,77 @@ const ProductData = ({
|
|||||||
<Select
|
<Select
|
||||||
placeholder={isLoadingProducts ? 'Loading products...' : 'Select product HS code'}
|
placeholder={isLoadingProducts ? 'Loading products...' : 'Select product HS code'}
|
||||||
options={productOptions}
|
options={productOptions}
|
||||||
value={p.product || ''}
|
value={p.productId || p.product || ''}
|
||||||
onChange={(e) => {
|
onChange={async (e) => {
|
||||||
|
const selectedValue = e.target.value;
|
||||||
// Clear error when user selects a product
|
// Clear error when user selects a product
|
||||||
if (formErrors[`product_${idx}`]) {
|
if (formErrors[`product_${idx}`]) {
|
||||||
const newErrors = { ...formErrors };
|
const newErrors = { ...formErrors };
|
||||||
delete newErrors[`product_${idx}`];
|
delete newErrors[`product_${idx}`];
|
||||||
setFormErrors(newErrors);
|
setFormErrors(newErrors);
|
||||||
}
|
}
|
||||||
updateProductField(p.id, 'product', e.target.value, productOptions);
|
// Find the selected product from options
|
||||||
|
const selectedProduct = productOptions.find(opt => opt.value === selectedValue);
|
||||||
|
// Update the product with all necessary fields
|
||||||
|
const updatedProduct = {
|
||||||
|
...p,
|
||||||
|
product: selectedProduct?.value || selectedValue,
|
||||||
|
productName: selectedProduct?.label || '',
|
||||||
|
productId: selectedProduct?.value || selectedValue
|
||||||
|
};
|
||||||
|
// Update the products array with the updated product
|
||||||
|
const updatedProducts = products.map(prod =>
|
||||||
|
prod.id === p.id ? updatedProduct : prod
|
||||||
|
);
|
||||||
|
onProductsChange(updatedProducts);
|
||||||
|
|
||||||
|
// After updating the product, fetch forecast data if we have a valid product and establishment ID
|
||||||
|
if (selectedProduct?.value) {
|
||||||
|
try {
|
||||||
|
// Get establishment ID from sessionStorage or localStorage
|
||||||
|
const establishmentId = sessionStorage.getItem('establishment_id') ||
|
||||||
|
localStorage.getItem('establishmentId') ||
|
||||||
|
localStorage.getItem('establishment_id');
|
||||||
|
|
||||||
|
if (establishmentId && quarter && year) {
|
||||||
|
const selectedProductId = selectedProduct.originalData?.id || selectedProduct.value;
|
||||||
|
const forecastData = await fetchPreviousForecastData(selectedProductId, establishmentId, p.id);
|
||||||
|
|
||||||
|
if (forecastData) {
|
||||||
|
const data = forecastData.data || forecastData;
|
||||||
|
const updatedWithForecast = {
|
||||||
|
...updatedProduct,
|
||||||
|
octQuantity: data.previous_quantity_period_one || '',
|
||||||
|
novQuantity: data.previous_quantity_period_two || '',
|
||||||
|
decQuantity: data.previous_quantity_period_three || '',
|
||||||
|
octCost: data.previous_cost_period_one || '',
|
||||||
|
novCost: data.previous_cost_period_two || '',
|
||||||
|
decCost: data.previous_cost_period_three || '',
|
||||||
|
capacity: data.annual_installed_capacity || ''
|
||||||
|
};
|
||||||
|
|
||||||
|
const finalUpdatedProducts = updatedProducts.map(prod =>
|
||||||
|
prod.id === p.id ? updatedWithForecast : prod
|
||||||
|
);
|
||||||
|
|
||||||
|
onProductsChange(finalUpdatedProducts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching forecast data:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
loading={isLoadingProducts}
|
loading={isLoadingProducts}
|
||||||
disabled={isLoadingProducts}
|
disabled={isLoadingProducts}
|
||||||
error={!!formErrors[`product_${idx}`]}
|
error={!!formErrors[`product_${idx}`]}
|
||||||
/>
|
/>
|
||||||
|
<div className="h-5">
|
||||||
{formErrors[`product_${idx}`] && (
|
{formErrors[`product_${idx}`] && (
|
||||||
<p className="mt-1 text-sm text-red-600">{formErrors[`product_${idx}`]}</p>
|
<p className="text-sm text-red-600">{formErrors[`product_${idx}`]}</p>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
{productsError && productOptions.length === 0 && (
|
{productsError && productOptions.length === 0 && (
|
||||||
<p className="mt-1 text-xs text-red-600">{productsError}</p>
|
<p className="mt-1 text-xs text-red-600">{productsError}</p>
|
||||||
)}
|
)}
|
||||||
@ -838,13 +890,12 @@ const ProductData = ({
|
|||||||
onChange={(e) => updateProductField(p.id, 'unit', e.target.value, unitOptions)}
|
onChange={(e) => updateProductField(p.id, 'unit', e.target.value, unitOptions)}
|
||||||
error={!!formErrors[`unit_${idx}`]}
|
error={!!formErrors[`unit_${idx}`]}
|
||||||
/>
|
/>
|
||||||
|
<div className="h-5">
|
||||||
{formErrors[`unit_${idx}`] && (
|
{formErrors[`unit_${idx}`] && (
|
||||||
<p className="mt-1 text-xs text-red-600">{formErrors[`unit_${idx}`]}</p>
|
<p className="text-xs text-red-600">{formErrors[`unit_${idx}`]}</p>
|
||||||
)}
|
|
||||||
{!formErrors[`unit_${idx}`] && unitsError && unitOptions.length === 0 && (
|
|
||||||
<p className="mt-1 text-xs text-red-600">{unitsError}</p>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label className="block text-sm font-medium text-gray-700 mb-1">Annual installed capacity (in unit) <span className="text-red-500">*</span></label>
|
<label className="block text-sm font-medium text-gray-700 mb-1">Annual installed capacity (in unit) <span className="text-red-500">*</span></label>
|
||||||
<Input
|
<Input
|
||||||
@ -855,11 +906,13 @@ const ProductData = ({
|
|||||||
required
|
required
|
||||||
error={!!formErrors[`capacity_${idx}`]}
|
error={!!formErrors[`capacity_${idx}`]}
|
||||||
/>
|
/>
|
||||||
|
<div className="h-5">
|
||||||
{formErrors[`capacity_${idx}`] && (
|
{formErrors[`capacity_${idx}`] && (
|
||||||
<p className="mt-1 text-xs text-red-600">{formErrors[`capacity_${idx}`]}</p>
|
<p className="text-xs text-red-600">{formErrors[`capacity_${idx}`]}</p>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
|
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
|
||||||
<SectionBox
|
<SectionBox
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user