fix(main-data): implement automatic class description updates based on class_id changes
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
import TariffFractionDialog from './tariff-fraction-dialog.svelte';
|
||||
import ClassDialog from './class-dialog.svelte';
|
||||
import type { LineItem, LineQuantities, LineFinancials, LineCustoms } from '$lib/api/dashboard/a76/items';
|
||||
import { companyStore } from '$lib/stores/company.svelte';
|
||||
|
||||
let {
|
||||
lineItem = $bindable(),
|
||||
@@ -26,6 +27,9 @@
|
||||
let showCountryDialog = $state(false);
|
||||
let showFractionDialog = $state(false);
|
||||
|
||||
// Track previous class_id to detect changes
|
||||
let previousClassId = $state<number | undefined>(undefined);
|
||||
|
||||
// Initialize from existing data
|
||||
$effect(() => {
|
||||
if (lineItem.class_code) {
|
||||
@@ -33,12 +37,66 @@
|
||||
}
|
||||
});
|
||||
|
||||
// Watch for class_id changes and update descriptions automatically
|
||||
$effect(() => {
|
||||
const currentClassId = lineItem.class_id;
|
||||
const activeCompanyId = companyStore?.activeCompany?.id;
|
||||
|
||||
// Only fetch if class_id changed, is valid, and we have a company
|
||||
if (currentClassId && currentClassId !== previousClassId && activeCompanyId) {
|
||||
previousClassId = currentClassId;
|
||||
|
||||
// Fetch all classes and find the one with matching ID
|
||||
fetch(`/api-sveltekit/classes?company_id=${activeCompanyId}&limit=100`)
|
||||
.then(response => {
|
||||
if (response.ok) {
|
||||
return response.json();
|
||||
}
|
||||
throw new Error('Failed to fetch classes');
|
||||
})
|
||||
.then(data => {
|
||||
const classes = data.items || [];
|
||||
const classItem = classes.find((c: any) => c.id === currentClassId);
|
||||
|
||||
if (classItem) {
|
||||
// Store the code and description in the lineItem for display
|
||||
(lineItem as any).class_code = classItem.class_code;
|
||||
(lineItem as any).class_unit_of_measure = classItem.unit_of_measure;
|
||||
(lineItem as any).class_description = classItem.description_es || classItem.description_en;
|
||||
|
||||
// Update description fields if description object exists
|
||||
if ((lineItem as any).description) {
|
||||
if (classItem.description_es) {
|
||||
(lineItem as any).description.description_spanish = classItem.description_es;
|
||||
}
|
||||
if (classItem.description_en) {
|
||||
(lineItem as any).description.description_english = classItem.description_en;
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error fetching class data:', error);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
function handleClassSelect(classItem: any) {
|
||||
lineItem.class_id = classItem.id;
|
||||
// Store the unit of measure and description for display
|
||||
(lineItem as any).class_unit_of_measure = classItem.unit_of_measure;
|
||||
(lineItem as any).class_code = classItem.class_code;
|
||||
(lineItem as any).class_description = classItem.description_es || classItem.description_en;
|
||||
|
||||
// Update description fields if description object exists
|
||||
if ((lineItem as any).description) {
|
||||
if (classItem.description_es) {
|
||||
(lineItem as any).description.description_spanish = classItem.description_es;
|
||||
}
|
||||
if (classItem.description_en) {
|
||||
(lineItem as any).description.description_english = classItem.description_en;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function handleUnitSelect(unit: any) {
|
||||
|
||||
Reference in New Issue
Block a user