From 946416e6e6a17c48aa4d2bf43cceffb7823dfeaf Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 10 Jul 2025 17:50:56 +0300 Subject: [PATCH] feat(ItemsList, TypeaheadInput): streamline item update logic and improve suggestion handling --- src/components/ItemsList.tsx | 43 +++++++++++++++---------------- src/components/TypeaheadInput.tsx | 16 +++++------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/components/ItemsList.tsx b/src/components/ItemsList.tsx index a1a3566..a1a1299 100644 --- a/src/components/ItemsList.tsx +++ b/src/components/ItemsList.tsx @@ -305,10 +305,8 @@ export function ItemsList({ url }: ItemsListProps) { // Mutations const saveItemMutation = useMutation({ mutationFn: (item: Item) => saveItemToDb(url, item), - // REMOVED: onSuccess invalidation that was causing the issue onError: (error) => { console.error('Failed to save item:', error); - // You could add a toast notification here } }); @@ -542,29 +540,30 @@ export function ItemsList({ url }: ItemsListProps) { const handleWikidataSelect = useCallback(async (suggestion: WikidataSuggestion, itemId: string) => { console.log('Wikidata selection for item:', itemId, suggestion); + // Find the current item and its index + const itemIndex = items.findIndex(item => item.id === itemId); + const currentItem = items[itemIndex]; + + if (!currentItem || itemIndex === -1) { + console.error('Item not found:', itemId); + return; + } + + // Create the complete updated item object + const updatedItem: Item = { + ...currentItem, + name: suggestion.label || '', + description: suggestion.description || '', + wikidataId: suggestion.id + }; + // Update item immediately with suggestion data setItems(prev => prev.map(item => - item.id === itemId - ? { - ...item, - name: suggestion.label || '', - description: suggestion.description || '', - wikidataId: suggestion.id - } - : item + item.id === itemId ? updatedItem : item )); - - // Save the updated item - const updatedItem = items.find(item => item.id === itemId); - if (updatedItem) { - const itemToSave = { - ...updatedItem, - name: suggestion.label || '', - description: suggestion.description || '', - wikidataId: suggestion.id - }; - saveItemMutation.mutate(itemToSave); - } + + // Save the updated item (using the item we just created) + saveItemMutation.mutate(updatedItem); // Fetch properties for this Wikidata item if (suggestion.id) { diff --git a/src/components/TypeaheadInput.tsx b/src/components/TypeaheadInput.tsx index 21f854c..6a856ef 100644 --- a/src/components/TypeaheadInput.tsx +++ b/src/components/TypeaheadInput.tsx @@ -251,24 +251,22 @@ export function TypeaheadInput({ // Handle suggestion selection const handleSelect = useCallback((suggestion: WikidataSuggestion) => { console.log('Suggestion selected:', suggestion); - + // Update local value with the selected suggestion const newValue = suggestion.label || ''; setLocalValue(newValue); - - // Call the selection handler first (this will update the parent) + + // Call the selection handler (this will update the parent with all Wikidata info) + // This handles name, description, and wikidataId updates onSelect(suggestion); - - // Then update the input value - onInput(newValue); - + // Hide suggestions setShowSuggestions(false); setSelectedIndex(-1); - + // Keep focus on input inputRef.current?.focus(); - }, [onInput, onSelect]); + }, [onSelect]); // Handle focus const handleFocus = useCallback(() => {