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:', {
|
||||
selectedBrand,
|
||||
defaultBrand,
|
||||
templatesCount: templates?.length || 0,
|
||||
templatesForBrand: templates?.filter(t => t.company === selectedBrand)?.length || 0
|
||||
});
|
||||
}, [selectedBrand, defaultBrand, templates]);
|
||||
@@ -69,6 +70,14 @@ const SearchableTemplateSelect: React.FC<SearchableTemplateSelectProps> = ({
|
||||
}
|
||||
}, [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
|
||||
const handleWheel = (e: React.WheelEvent) => {
|
||||
const scrollArea = e.currentTarget;
|
||||
|
||||
@@ -53,7 +53,8 @@ const ValidationContainer = <T extends string>({
|
||||
updateFilters,
|
||||
loadTemplates,
|
||||
setData,
|
||||
fields
|
||||
fields,
|
||||
isLoadingTemplates
|
||||
} = validationState
|
||||
|
||||
// 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
|
||||
return props.data.map((row: any, index: number) => {
|
||||
if (itemNumbers[index]) {
|
||||
return { ...row, item_number: itemNumbers[index] };
|
||||
return {
|
||||
...row,
|
||||
item_number: itemNumbers[index]
|
||||
};
|
||||
}
|
||||
return row;
|
||||
});
|
||||
}, [props.data]);
|
||||
}, [props.data, itemNumbers]);
|
||||
|
||||
return (
|
||||
<ValidationTable
|
||||
@@ -611,9 +615,10 @@ const ValidationContainer = <T extends string>({
|
||||
data={enhancedData}
|
||||
validatingCells={validatingCells}
|
||||
itemNumbers={itemNumbersMap}
|
||||
isLoadingTemplates={isLoadingTemplates}
|
||||
/>
|
||||
);
|
||||
}, [itemNumbers, validatingUpcRows]);
|
||||
}, [validatingUpcRows, itemNumbers, isLoadingTemplates]);
|
||||
|
||||
// Memoize the ValidationTable to prevent unnecessary re-renders
|
||||
const renderValidationTable = useMemo(() => {
|
||||
@@ -752,7 +757,12 @@ const ValidationContainer = <T extends string>({
|
||||
</div>
|
||||
|
||||
<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
|
||||
templates={templates}
|
||||
value={selectedTemplateId || ""}
|
||||
@@ -767,7 +777,7 @@ const ValidationContainer = <T extends string>({
|
||||
/>
|
||||
) : (
|
||||
<Button variant="outline" className="w-full justify-between" disabled>
|
||||
Loading templates...
|
||||
No templates available
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -14,6 +14,8 @@ import SearchableTemplateSelect from './SearchableTemplateSelect'
|
||||
import { Table, TableHeader, TableBody, TableHead, TableRow, TableCell } from '@/components/ui/table'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
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
|
||||
type ErrorType = {
|
||||
@@ -42,6 +44,7 @@ interface ValidationTableProps<T extends string> {
|
||||
upcValidationResults: Map<number, { itemNumber: string }>
|
||||
validatingCells: Set<string>
|
||||
itemNumbers: Map<number, string>
|
||||
isLoadingTemplates?: boolean
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
@@ -247,7 +250,8 @@ const ValidationTable = <T extends string>({
|
||||
applyTemplate,
|
||||
getTemplateDisplayText,
|
||||
validatingCells,
|
||||
itemNumbers
|
||||
itemNumbers,
|
||||
isLoadingTemplates = false
|
||||
}: ValidationTableProps<T>) => {
|
||||
const { translations } = useRsi<T>();
|
||||
|
||||
@@ -348,6 +352,12 @@ const ValidationTable = <T extends string>({
|
||||
const defaultBrand = row.original.company || undefined;
|
||||
return (
|
||||
<TableCell className="p-1" style={{ width: '200px', minWidth: '200px' }}>
|
||||
{isLoadingTemplates ? (
|
||||
<Button variant="outline" className="w-full justify-between" disabled>
|
||||
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
|
||||
Loading...
|
||||
</Button>
|
||||
) : (
|
||||
<SearchableTemplateSelect
|
||||
templates={templates}
|
||||
value={templateValue || ''}
|
||||
@@ -357,10 +367,11 @@ const ValidationTable = <T extends string>({
|
||||
getTemplateDisplayText={getTemplateDisplayText}
|
||||
defaultBrand={defaultBrand}
|
||||
/>
|
||||
)}
|
||||
</TableCell>
|
||||
);
|
||||
}
|
||||
}), [templates, applyTemplate, getTemplateDisplayText]);
|
||||
}), [templates, applyTemplate, getTemplateDisplayText, isLoadingTemplates]);
|
||||
|
||||
// Memoize field columns
|
||||
const fieldColumns = useMemo(() => fields.map((field): ColumnDef<RowData<T>, any> | null => {
|
||||
|
||||
@@ -165,6 +165,7 @@ export const useValidationState = <T extends string>({
|
||||
|
||||
// Template state
|
||||
const [templates, setTemplates] = useState<Template[]>([])
|
||||
const [isLoadingTemplates, setIsLoadingTemplates] = useState(true)
|
||||
const [templateState, setTemplateState] = useState({
|
||||
selectedTemplateId: null as string | null,
|
||||
showSaveTemplateDialog: false,
|
||||
@@ -896,6 +897,7 @@ export const useValidationState = <T extends string>({
|
||||
// Load templates
|
||||
const loadTemplates = useCallback(async () => {
|
||||
try {
|
||||
setIsLoadingTemplates(true);
|
||||
console.log('Fetching templates from:', `${getApiUrl()}/templates`);
|
||||
// Fetch templates from the API
|
||||
const response = await fetch(`${getApiUrl()}/templates`)
|
||||
@@ -935,6 +937,8 @@ export const useValidationState = <T extends string>({
|
||||
} catch (error) {
|
||||
console.error('Error fetching templates:', error)
|
||||
toast.error('Failed to load templates')
|
||||
} finally {
|
||||
setIsLoadingTemplates(false);
|
||||
}
|
||||
}, [])
|
||||
|
||||
@@ -1525,6 +1529,7 @@ export const useValidationState = <T extends string>({
|
||||
|
||||
// Templates
|
||||
templates,
|
||||
isLoadingTemplates,
|
||||
selectedTemplateId: templateState.selectedTemplateId,
|
||||
showSaveTemplateDialog: templateState.showSaveTemplateDialog,
|
||||
newTemplateName: templateState.newTemplateName,
|
||||
|
||||
Reference in New Issue
Block a user