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
|
// Mutations
|
||||||
const saveItemMutation = useMutation({
|
const saveItemMutation = useMutation({
|
||||||
mutationFn: (item: Item) => saveItemToDb(url, item),
|
mutationFn: (item: Item) => saveItemToDb(url, item),
|
||||||
// REMOVED: onSuccess invalidation that was causing the issue
|
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
console.error('Failed to save item:', 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) => {
|
const handleWikidataSelect = useCallback(async (suggestion: WikidataSuggestion, itemId: string) => {
|
||||||
console.log('Wikidata selection for item:', itemId, suggestion);
|
console.log('Wikidata selection for item:', itemId, suggestion);
|
||||||
|
|
||||||
// Update item immediately with suggestion data
|
// Find the current item and its index
|
||||||
setItems(prev => prev.map(item =>
|
const itemIndex = items.findIndex(item => item.id === itemId);
|
||||||
item.id === itemId
|
const currentItem = items[itemIndex];
|
||||||
? {
|
|
||||||
...item,
|
|
||||||
name: suggestion.label || '',
|
|
||||||
description: suggestion.description || '',
|
|
||||||
wikidataId: suggestion.id
|
|
||||||
}
|
|
||||||
: item
|
|
||||||
));
|
|
||||||
|
|
||||||
// Save the updated item
|
if (!currentItem || itemIndex === -1) {
|
||||||
const updatedItem = items.find(item => item.id === itemId);
|
console.error('Item not found:', itemId);
|
||||||
if (updatedItem) {
|
return;
|
||||||
const itemToSave = {
|
}
|
||||||
...updatedItem,
|
|
||||||
|
// Create the complete updated item object
|
||||||
|
const updatedItem: Item = {
|
||||||
|
...currentItem,
|
||||||
name: suggestion.label || '',
|
name: suggestion.label || '',
|
||||||
description: suggestion.description || '',
|
description: suggestion.description || '',
|
||||||
wikidataId: suggestion.id
|
wikidataId: suggestion.id
|
||||||
};
|
};
|
||||||
saveItemMutation.mutate(itemToSave);
|
|
||||||
}
|
// Update item immediately with suggestion data
|
||||||
|
setItems(prev => prev.map(item =>
|
||||||
|
item.id === itemId ? updatedItem : item
|
||||||
|
));
|
||||||
|
|
||||||
|
// Save the updated item (using the item we just created)
|
||||||
|
saveItemMutation.mutate(updatedItem);
|
||||||
|
|
||||||
// Fetch properties for this Wikidata item
|
// Fetch properties for this Wikidata item
|
||||||
if (suggestion.id) {
|
if (suggestion.id) {
|
||||||
|
|
|
@ -256,19 +256,17 @@ export function TypeaheadInput({
|
||||||
const newValue = suggestion.label || '';
|
const newValue = suggestion.label || '';
|
||||||
setLocalValue(newValue);
|
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);
|
onSelect(suggestion);
|
||||||
|
|
||||||
// Then update the input value
|
|
||||||
onInput(newValue);
|
|
||||||
|
|
||||||
// Hide suggestions
|
// Hide suggestions
|
||||||
setShowSuggestions(false);
|
setShowSuggestions(false);
|
||||||
setSelectedIndex(-1);
|
setSelectedIndex(-1);
|
||||||
|
|
||||||
// Keep focus on input
|
// Keep focus on input
|
||||||
inputRef.current?.focus();
|
inputRef.current?.focus();
|
||||||
}, [onInput, onSelect]);
|
}, [onSelect]);
|
||||||
|
|
||||||
// Handle focus
|
// Handle focus
|
||||||
const handleFocus = useCallback(() => {
|
const handleFocus = useCallback(() => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue