diff --git a/src/components/ItemsList.tsx b/src/components/ItemsList.tsx index a1a1299..e93b636 100644 --- a/src/components/ItemsList.tsx +++ b/src/components/ItemsList.tsx @@ -481,10 +481,14 @@ export function ItemsList({ url }: ItemsListProps) { }; })); - // FOURTH: Save to database (but don't invalidate queries immediately) + // FOURTH: Save to database and refresh data try { await addPropertyToDb(url, normalizedProperty); console.log('Property successfully saved to database:', normalizedProperty); + + // Invalidate queries to refresh data from database + queryClient.invalidateQueries({ queryKey: ['items', url] }); + } catch (error) { console.error('Error saving property to database:', error); // Revert optimistic updates on error @@ -516,6 +520,8 @@ export function ItemsList({ url }: ItemsListProps) { // Update the item with the fetched property value if it exists if (properties[normalizedProperty]) { console.log(`Updating ${item.wikidataId} with ${normalizedProperty}:`, properties[normalizedProperty]); + + // Update local state setItems(prevItems => prevItems.map(prevItem => prevItem.wikidataId === item.wikidataId ? { @@ -527,6 +533,21 @@ export function ItemsList({ url }: ItemsListProps) { } : prevItem )); + + // Save the updated item to database to persist the auto-populated value + const updatedItem = items.find(i => i.wikidataId === item.wikidataId); + if (updatedItem) { + const itemToSave = { + ...updatedItem, + customProperties: { + ...updatedItem.customProperties, + [normalizedProperty]: properties[normalizedProperty] + } + }; + saveItemToDb(url, itemToSave).catch(error => { + console.error('Error saving auto-populated value:', error); + }); + } } } catch (error) { console.error(`Error fetching Wikidata properties for ${item.wikidataId}:`, error);