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

View File

@ -574,20 +574,39 @@ const IsicHsCodes = () => {
try { try {
setIsLoading(true); setIsLoading(true);
const productId = selected.id; 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) { if (response && response.data) {
const product = response.data; const productData = response.data; // The actual product data is in response.data
setForm({
code: product.hs_code || '', if (!productData) {
product: product.product_name || '', throw new Error('No product data received in response');
unit: product.unit_id ? String(product.unit_id) : '', }
status: product.is_active ? 'Active' : 'Inactive',
description: product.hs_description || '' 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); setEditingRow(index);
setModalMode('edit'); setModalMode('edit');
} else { } 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'); showToast('Failed to load product details for editing', 'error');
} }
} catch (error) { } catch (error) {
@ -805,61 +824,88 @@ const IsicHsCodes = () => {
console.error('Error getting user from session:', error); 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 = { const productData = {
hs_code: form.code, hs_code: form.code,
product_name: form.product, product_name: form.product,
unit_id: form.unit ? parseInt(form.unit) : null, unit_id: form.unit ? parseInt(form.unit) : null,
is_active: form.status === 'Active', // Preserve the original created_by value for updates, or use current user for new records
hs_description: form.description || '', created_by: modalMode === 'edit' ? (existingProduct?.created_by || userProfile?.id) : (userProfile?.id || null)
created_by: 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) { if (modalMode === 'edit') {
const productId = rowsData[editingRow]?.id; // Try to get the product ID from the form state first
const productId = form.id || (editingRow !== null ? rowsData[editingRow]?.id : null);
if (!productId) { 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 { 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 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 = { const updatedProduct = {
...rowsData[editingRow], ...existingProduct, // This preserves all existing fields including estimatedMapped and createdBy
// Only update these specific fields
code: form.code, code: form.code,
product: form.product, product: form.product,
unit: selectedUnit ? selectedUnit.label : 'N/A', unit: selectedUnit ? selectedUnit.label : existingProduct?.unit || 'N/A',
unit_id: form.unit ? parseInt(form.unit) : null, unit_id: form.unit ? parseInt(form.unit) : existingProduct?.unit_id || null,
updated: new Date().toLocaleDateString('en-GB'), updated: new Date().toLocaleDateString('en-GB'),
status: form.status, // Explicitly preserve these fields to ensure they don't get cleared
description: form.description || '' estimatedMapped: existingProduct?.estimatedMapped || 0,
createdBy: existingProduct?.createdBy || '-',
createdOn: existingProduct?.createdOn || new Date().toLocaleDateString('en-GB')
}; };
setRowsData(prev => { if (editingRow !== null) {
const updated = [...prev]; // Update by index if we have the editingRow
updated[editingRow] = updatedProduct; setRowsData(prev => {
return updated; const updated = [...prev];
}); updated[editingRow] = updatedProduct;
return updated;
});
}
// Update filteredData with the updated product
setFilteredData(prev => { setFilteredData(prev => {
const updated = [...prev]; return prev.map(item =>
const index = updated.findIndex(item => item.id === updatedProduct.id); item.id === productId ? updatedProduct : item
if (index !== -1) { );
updated[index] = updatedProduct;
}
return updated;
}); });
// Show success message and reload the page after a short delay
showToast('Product updated successfully!', 'success'); showToast('Product updated successfully!', 'success');
closeModal(); closeModal();
setTimeout(() => {
window.location.reload();
}, 1000);
return; return;
} catch (updateError) { } catch (error) {
console.error('Error updating product:', updateError); console.error('Error updating product:', error);
let errorMessage = 'Failed to update product. Please try again.'; let errorMessage = 'Failed to update product. Please try again.';
if (updateError.response?.data?.message) { if (error.response?.data?.message) {
errorMessage = updateError.response.data.message; errorMessage = error.response.data.message;
} }
setError(errorMessage); setError(errorMessage);
return;
} }
} }
@ -885,6 +931,11 @@ const IsicHsCodes = () => {
setFilteredData(prev => [newProduct, ...prev]); setFilteredData(prev => [newProduct, ...prev]);
showToast('Product created successfully!', 'success'); showToast('Product created successfully!', 'success');
closeModal(); closeModal();
// Reload the page after a short delay to reflect changes
setTimeout(() => {
window.location.reload();
}, 1000);
} catch (createError) { } catch (createError) {
console.error('Error in product creation:', createError); console.error('Error in product creation:', createError);
let errorMessage = 'Failed to create product. Please try again.'; let errorMessage = 'Failed to create product. Please try again.';
@ -1114,6 +1165,16 @@ const IsicHsCodes = () => {
headers={headers} headers={headers}
rows={rows} rows={rows}
renderCell={(value, rowIndex, colIndex) => { 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) { if (colIndex === 6) {
return ( return (
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">