feat(ItemsList, TypeaheadInput): streamline item update logic and improve suggestion handling
This commit is contained in:
parent
99a0d221ba
commit
946416e6e6
2 changed files with 28 additions and 31 deletions
|
@ -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) {
|
||||
|
|
|
@ -256,19 +256,17 @@ export function TypeaheadInput({
|
|||
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(() => {
|
||||
|
|
Loading…
Add table
Reference in a new issue