Fix templates loading on page load
This commit is contained in:
@@ -58,6 +58,7 @@ const SearchableTemplateSelect: React.FC<SearchableTemplateSelectProps> = ({
|
|||||||
console.debug('SearchableTemplateSelect brand update:', {
|
console.debug('SearchableTemplateSelect brand update:', {
|
||||||
selectedBrand,
|
selectedBrand,
|
||||||
defaultBrand,
|
defaultBrand,
|
||||||
|
templatesCount: templates?.length || 0,
|
||||||
templatesForBrand: templates?.filter(t => t.company === selectedBrand)?.length || 0
|
templatesForBrand: templates?.filter(t => t.company === selectedBrand)?.length || 0
|
||||||
});
|
});
|
||||||
}, [selectedBrand, defaultBrand, templates]);
|
}, [selectedBrand, defaultBrand, templates]);
|
||||||
@@ -69,6 +70,14 @@ const SearchableTemplateSelect: React.FC<SearchableTemplateSelectProps> = ({
|
|||||||
}
|
}
|
||||||
}, [defaultBrand]);
|
}, [defaultBrand]);
|
||||||
|
|
||||||
|
// Force a re-render when templates change from empty to non-empty
|
||||||
|
useEffect(() => {
|
||||||
|
if (templates && templates.length > 0) {
|
||||||
|
// Force a re-render by updating state
|
||||||
|
setSearchTerm("");
|
||||||
|
}
|
||||||
|
}, [templates]);
|
||||||
|
|
||||||
// Handle wheel events for scrolling
|
// Handle wheel events for scrolling
|
||||||
const handleWheel = (e: React.WheelEvent) => {
|
const handleWheel = (e: React.WheelEvent) => {
|
||||||
const scrollArea = e.currentTarget;
|
const scrollArea = e.currentTarget;
|
||||||
|
|||||||
@@ -53,7 +53,8 @@ const ValidationContainer = <T extends string>({
|
|||||||
updateFilters,
|
updateFilters,
|
||||||
loadTemplates,
|
loadTemplates,
|
||||||
setData,
|
setData,
|
||||||
fields
|
fields,
|
||||||
|
isLoadingTemplates
|
||||||
} = validationState
|
} = validationState
|
||||||
|
|
||||||
// Add state for tracking product lines and sublines per row
|
// Add state for tracking product lines and sublines per row
|
||||||
@@ -599,11 +600,14 @@ const ValidationContainer = <T extends string>({
|
|||||||
// Create a new array with the item numbers merged in
|
// Create a new array with the item numbers merged in
|
||||||
return props.data.map((row: any, index: number) => {
|
return props.data.map((row: any, index: number) => {
|
||||||
if (itemNumbers[index]) {
|
if (itemNumbers[index]) {
|
||||||
return { ...row, item_number: itemNumbers[index] };
|
return {
|
||||||
|
...row,
|
||||||
|
item_number: itemNumbers[index]
|
||||||
|
};
|
||||||
}
|
}
|
||||||
return row;
|
return row;
|
||||||
});
|
});
|
||||||
}, [props.data]);
|
}, [props.data, itemNumbers]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ValidationTable
|
<ValidationTable
|
||||||
@@ -611,9 +615,10 @@ const ValidationContainer = <T extends string>({
|
|||||||
data={enhancedData}
|
data={enhancedData}
|
||||||
validatingCells={validatingCells}
|
validatingCells={validatingCells}
|
||||||
itemNumbers={itemNumbersMap}
|
itemNumbers={itemNumbersMap}
|
||||||
|
isLoadingTemplates={isLoadingTemplates}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}, [itemNumbers, validatingUpcRows]);
|
}, [validatingUpcRows, itemNumbers, isLoadingTemplates]);
|
||||||
|
|
||||||
// Memoize the ValidationTable to prevent unnecessary re-renders
|
// Memoize the ValidationTable to prevent unnecessary re-renders
|
||||||
const renderValidationTable = useMemo(() => {
|
const renderValidationTable = useMemo(() => {
|
||||||
@@ -752,7 +757,12 @@ const ValidationContainer = <T extends string>({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center">
|
<div className="flex items-center">
|
||||||
{templates && templates.length > 0 ? (
|
{isLoadingTemplates ? (
|
||||||
|
<Button variant="outline" className="w-[220px] justify-between" disabled>
|
||||||
|
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
|
||||||
|
Loading templates...
|
||||||
|
</Button>
|
||||||
|
) : templates && templates.length > 0 ? (
|
||||||
<SearchableTemplateSelect
|
<SearchableTemplateSelect
|
||||||
templates={templates}
|
templates={templates}
|
||||||
value={selectedTemplateId || ""}
|
value={selectedTemplateId || ""}
|
||||||
@@ -767,7 +777,7 @@ const ValidationContainer = <T extends string>({
|
|||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<Button variant="outline" className="w-full justify-between" disabled>
|
<Button variant="outline" className="w-full justify-between" disabled>
|
||||||
Loading templates...
|
No templates available
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import SearchableTemplateSelect from './SearchableTemplateSelect'
|
|||||||
import { Table, TableHeader, TableBody, TableHead, TableRow, TableCell } from '@/components/ui/table'
|
import { Table, TableHeader, TableBody, TableHead, TableRow, TableCell } from '@/components/ui/table'
|
||||||
import { Checkbox } from '@/components/ui/checkbox'
|
import { Checkbox } from '@/components/ui/checkbox'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
|
import { Button } from '@/components/ui/button'
|
||||||
|
import { Loader2 } from 'lucide-react'
|
||||||
|
|
||||||
// Define a simple Error type locally to avoid import issues
|
// Define a simple Error type locally to avoid import issues
|
||||||
type ErrorType = {
|
type ErrorType = {
|
||||||
@@ -42,6 +44,7 @@ interface ValidationTableProps<T extends string> {
|
|||||||
upcValidationResults: Map<number, { itemNumber: string }>
|
upcValidationResults: Map<number, { itemNumber: string }>
|
||||||
validatingCells: Set<string>
|
validatingCells: Set<string>
|
||||||
itemNumbers: Map<number, string>
|
itemNumbers: Map<number, string>
|
||||||
|
isLoadingTemplates?: boolean
|
||||||
[key: string]: any
|
[key: string]: any
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,7 +250,8 @@ const ValidationTable = <T extends string>({
|
|||||||
applyTemplate,
|
applyTemplate,
|
||||||
getTemplateDisplayText,
|
getTemplateDisplayText,
|
||||||
validatingCells,
|
validatingCells,
|
||||||
itemNumbers
|
itemNumbers,
|
||||||
|
isLoadingTemplates = false
|
||||||
}: ValidationTableProps<T>) => {
|
}: ValidationTableProps<T>) => {
|
||||||
const { translations } = useRsi<T>();
|
const { translations } = useRsi<T>();
|
||||||
|
|
||||||
@@ -348,19 +352,26 @@ const ValidationTable = <T extends string>({
|
|||||||
const defaultBrand = row.original.company || undefined;
|
const defaultBrand = row.original.company || undefined;
|
||||||
return (
|
return (
|
||||||
<TableCell className="p-1" style={{ width: '200px', minWidth: '200px' }}>
|
<TableCell className="p-1" style={{ width: '200px', minWidth: '200px' }}>
|
||||||
<SearchableTemplateSelect
|
{isLoadingTemplates ? (
|
||||||
templates={templates}
|
<Button variant="outline" className="w-full justify-between" disabled>
|
||||||
value={templateValue || ''}
|
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
|
||||||
onValueChange={(value) => {
|
Loading...
|
||||||
applyTemplate(value, [row.index]);
|
</Button>
|
||||||
}}
|
) : (
|
||||||
getTemplateDisplayText={getTemplateDisplayText}
|
<SearchableTemplateSelect
|
||||||
defaultBrand={defaultBrand}
|
templates={templates}
|
||||||
/>
|
value={templateValue || ''}
|
||||||
|
onValueChange={(value) => {
|
||||||
|
applyTemplate(value, [row.index]);
|
||||||
|
}}
|
||||||
|
getTemplateDisplayText={getTemplateDisplayText}
|
||||||
|
defaultBrand={defaultBrand}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}), [templates, applyTemplate, getTemplateDisplayText]);
|
}), [templates, applyTemplate, getTemplateDisplayText, isLoadingTemplates]);
|
||||||
|
|
||||||
// Memoize field columns
|
// Memoize field columns
|
||||||
const fieldColumns = useMemo(() => fields.map((field): ColumnDef<RowData<T>, any> | null => {
|
const fieldColumns = useMemo(() => fields.map((field): ColumnDef<RowData<T>, any> | null => {
|
||||||
|
|||||||
@@ -165,6 +165,7 @@ export const useValidationState = <T extends string>({
|
|||||||
|
|
||||||
// Template state
|
// Template state
|
||||||
const [templates, setTemplates] = useState<Template[]>([])
|
const [templates, setTemplates] = useState<Template[]>([])
|
||||||
|
const [isLoadingTemplates, setIsLoadingTemplates] = useState(true)
|
||||||
const [templateState, setTemplateState] = useState({
|
const [templateState, setTemplateState] = useState({
|
||||||
selectedTemplateId: null as string | null,
|
selectedTemplateId: null as string | null,
|
||||||
showSaveTemplateDialog: false,
|
showSaveTemplateDialog: false,
|
||||||
@@ -896,6 +897,7 @@ export const useValidationState = <T extends string>({
|
|||||||
// Load templates
|
// Load templates
|
||||||
const loadTemplates = useCallback(async () => {
|
const loadTemplates = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
|
setIsLoadingTemplates(true);
|
||||||
console.log('Fetching templates from:', `${getApiUrl()}/templates`);
|
console.log('Fetching templates from:', `${getApiUrl()}/templates`);
|
||||||
// Fetch templates from the API
|
// Fetch templates from the API
|
||||||
const response = await fetch(`${getApiUrl()}/templates`)
|
const response = await fetch(`${getApiUrl()}/templates`)
|
||||||
@@ -935,6 +937,8 @@ export const useValidationState = <T extends string>({
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching templates:', error)
|
console.error('Error fetching templates:', error)
|
||||||
toast.error('Failed to load templates')
|
toast.error('Failed to load templates')
|
||||||
|
} finally {
|
||||||
|
setIsLoadingTemplates(false);
|
||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
@@ -1525,6 +1529,7 @@ export const useValidationState = <T extends string>({
|
|||||||
|
|
||||||
// Templates
|
// Templates
|
||||||
templates,
|
templates,
|
||||||
|
isLoadingTemplates,
|
||||||
selectedTemplateId: templateState.selectedTemplateId,
|
selectedTemplateId: templateState.selectedTemplateId,
|
||||||
showSaveTemplateDialog: templateState.showSaveTemplateDialog,
|
showSaveTemplateDialog: templateState.showSaveTemplateDialog,
|
||||||
newTemplateName: templateState.newTemplateName,
|
newTemplateName: templateState.newTemplateName,
|
||||||
|
|||||||
Reference in New Issue
Block a user