bug fixed

This commit is contained in:
Malini 2025-11-14 17:45:28 +05:30
parent ba77b00761
commit dea409690b
2 changed files with 125 additions and 62 deletions

View File

@ -622,15 +622,17 @@ const EditCompanyProfile = () => {
required
error={fieldErrors.industryCodeBusiness}
/>
<TextField
label="Industry Code (Current Production)"
value={form.industryCodeCurrent || ""}
onChange={(e) => handleFormChange("industryCodeCurrent", e.target.value)}
placeholder="Enter Code"
width="100%"
required
error={fieldErrors.industryCodeCurrent}
/>
<div className="whitespace-nowrap">
<TextField
label="Industry Code (Current Production)"
value={form.industryCodeCurrent || ""}
onChange={(e) => handleFormChange("industryCodeCurrent", e.target.value)}
placeholder="Enter Code"
width="100%"
required
error={fieldErrors.industryCodeCurrent}
/>
</div>
<div className="col-span-full">
<TextField
label="Description of Industry"
@ -947,23 +949,21 @@ const EditCompanyProfile = () => {
};
return (
<div className="min-h-screen bg-[#F8FAFC]">
<HeaderBar />
<div className="max-w-[1280px] mx-auto px-4 py-6">
<div className="bg-white overflow-hidden">
<div className="min-h-screen bg-[#F8FAFC]">
<HeaderBar />
<div className="max-w-[1300px] mx-auto px-4 py-6">
<div className="bg-white overflow-hidden rounded-lg shadow-sm border border-gray-200">
{loading && <Loader />}
{/* Header */}
<div className="flex justify-between items-center border-b border-[#F1F2F4] ">
<div>
<h2 className="text-xl font-bold pb-6 text-gray-900">Edit Company Profile</h2>
</div>
<div className="px-6 pt-6 pb-4 border-b border-gray-200">
<h2 className="text-xl font-bold text-gray-900">Edit Company Profile</h2>
</div>
{/* Body */}
<div className="flex flex-1">
<aside className="w-64 bg-[#F9FAFB] border-r border-[#E5E7EB]">
<ol className="py-6 px-4 space-y-2">
<div className="flex flex-col md:flex-row flex-1">
<aside className="w-full md:w-64 bg-[#F9FAFB] border-b md:border-r border-gray-200">
<ol className="py-4 px-4 space-y-2 overflow-x-auto flex md:block">
{formSteps.map((step, index) => {
const isActive = index === activeStep;
const isCompleted = index < activeStep;
@ -1001,15 +1001,17 @@ const EditCompanyProfile = () => {
</ol>
</aside>
<div className="flex-1">
<div className="px-6 py-6">
{renderStepContent()}
<div className="p-6 w-full">
<div className="max-w-5xl mx-auto w-full">
{renderStepContent()}
</div>
</div>
</div>
</div>
{/* Footer */}
<div className="border-t border-[#F1F2F4] bg-white">
<div className="px-8 py-4 flex items-center justify-between gap-3">
<div className="bg-gray-50 border-t border-gray-200">
<div className="px-6 py-4 flex flex-col sm:flex-row items-center justify-between gap-4">
<div className="min-h-[1rem] flex items-center text-xs text-[#B45309]">
{loading ? (
<span className="inline-flex items-center gap-2">

View File

@ -574,20 +574,39 @@ const IsicHsCodes = () => {
try {
setIsLoading(true);
const productId = selected.id;
const response = await productService.getProductById(productId);
console.log('Fetching product with ID:', productId);
// Get fresh data from the server
const response = await productService.getProductById(productId);
console.log('API Response:', response);
if (response && response.data) {
const product = response.data;
setForm({
code: product.hs_code || '',
product: product.product_name || '',
unit: product.unit_id ? String(product.unit_id) : '',
status: product.is_active ? 'Active' : 'Inactive',
description: product.hs_description || ''
});
const productData = response.data; // The actual product data is in response.data
if (!productData) {
throw new Error('No product data received in response');
}
console.log('Product data for edit:', productData);
// Map the API response fields to form fields
const formData = {
id: productData.id,
code: productData.hs_code || '',
product: productData.product_name || '',
unit: productData.unit_id ? String(productData.unit_id) : '',
status: productData.is_active ? 'Active' : 'Inactive',
description: productData.hs_description || ''
};
console.log('Mapped form data:', formData);
console.log('Setting form data:', formData); // Debug log
setForm(formData);
setEditingRow(index);
setModalMode('edit');
} else {
console.error('Invalid product data received from API');
console.error('Empty response received from API');
showToast('Failed to load product details for editing', 'error');
}
} catch (error) {
@ -805,61 +824,88 @@ const IsicHsCodes = () => {
console.error('Error getting user from session:', error);
}
// Get the existing product to preserve the created_by field
const existingProduct = modalMode === 'edit' && editingRow !== null ? rowsData[editingRow] : null;
// For edit mode, only include the fields we want to update
const productData = {
hs_code: form.code,
product_name: form.product,
unit_id: form.unit ? parseInt(form.unit) : null,
is_active: form.status === 'Active',
hs_description: form.description || '',
created_by: userProfile?.id || null
// Preserve the original created_by value for updates, or use current user for new records
created_by: modalMode === 'edit' ? (existingProduct?.created_by || userProfile?.id) : (userProfile?.id || null)
};
// For new records, include additional required fields
if (modalMode !== 'edit') {
productData.is_active = form.status === 'Active';
productData.hs_description = form.description || '';
}
if (modalMode === 'edit' && editingRow !== null) {
const productId = rowsData[editingRow]?.id;
if (modalMode === 'edit') {
// Try to get the product ID from the form state first
const productId = form.id || (editingRow !== null ? rowsData[editingRow]?.id : null);
if (!productId) {
throw new Error('Product ID not found for editing');
console.error('Product ID not found for editing');
setError('Cannot update product: Product ID not found');
return;
}
try {
// Get the existing product data to preserve fields
const existingProduct = editingRow !== null ? rowsData[editingRow] : null;
console.log("existingProduct",existingProduct)
// Only update the specific fields we want to change
const response = await productService.updateProduct(productId, productData);
const selectedUnit = unitOptions.find(u => u.value === form.unit);
const selectedUnit = unitOptions.find(u => u.value === form.unit);
// Create updated product with all existing fields and update only the necessary ones
const updatedProduct = {
...rowsData[editingRow],
...existingProduct, // This preserves all existing fields including estimatedMapped and createdBy
// Only update these specific fields
code: form.code,
product: form.product,
unit: selectedUnit ? selectedUnit.label : 'N/A',
unit_id: form.unit ? parseInt(form.unit) : null,
unit: selectedUnit ? selectedUnit.label : existingProduct?.unit || 'N/A',
unit_id: form.unit ? parseInt(form.unit) : existingProduct?.unit_id || null,
updated: new Date().toLocaleDateString('en-GB'),
status: form.status,
description: form.description || ''
// Explicitly preserve these fields to ensure they don't get cleared
estimatedMapped: existingProduct?.estimatedMapped || 0,
createdBy: existingProduct?.createdBy || '-',
createdOn: existingProduct?.createdOn || new Date().toLocaleDateString('en-GB')
};
setRowsData(prev => {
const updated = [...prev];
updated[editingRow] = updatedProduct;
return updated;
});
if (editingRow !== null) {
// Update by index if we have the editingRow
setRowsData(prev => {
const updated = [...prev];
updated[editingRow] = updatedProduct;
return updated;
});
}
// Update filteredData with the updated product
setFilteredData(prev => {
const updated = [...prev];
const index = updated.findIndex(item => item.id === updatedProduct.id);
if (index !== -1) {
updated[index] = updatedProduct;
}
return updated;
return prev.map(item =>
item.id === productId ? updatedProduct : item
);
});
// Show success message and reload the page after a short delay
showToast('Product updated successfully!', 'success');
closeModal();
setTimeout(() => {
window.location.reload();
}, 1000);
return;
} catch (updateError) {
console.error('Error updating product:', updateError);
} catch (error) {
console.error('Error updating product:', error);
let errorMessage = 'Failed to update product. Please try again.';
if (updateError.response?.data?.message) {
errorMessage = updateError.response.data.message;
if (error.response?.data?.message) {
errorMessage = error.response.data.message;
}
setError(errorMessage);
return;
}
}
@ -885,6 +931,11 @@ const IsicHsCodes = () => {
setFilteredData(prev => [newProduct, ...prev]);
showToast('Product created successfully!', 'success');
closeModal();
// Reload the page after a short delay to reflect changes
setTimeout(() => {
window.location.reload();
}, 1000);
} catch (createError) {
console.error('Error in product creation:', createError);
let errorMessage = 'Failed to create product. Please try again.';
@ -1114,6 +1165,16 @@ const IsicHsCodes = () => {
headers={headers}
rows={rows}
renderCell={(value, rowIndex, colIndex) => {
// Handle product name column (index 1) with text wrapping
if (colIndex === 1) {
return (
<div className="max-w-[300px] whitespace-normal break-words">
{value}
</div>
);
}
// Handle action buttons column (index 6)
if (colIndex === 6) {
return (
<div className="flex items-center gap-2">