unit test fixes
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 48m56s
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 48m56s
This commit is contained in:
@@ -51,63 +51,90 @@ const useShoppingListsHook = () => {
|
||||
|
||||
const createList = useCallback(async (name: string) => {
|
||||
if (!user) return;
|
||||
const newList = await createListApi(name);
|
||||
if (newList) {
|
||||
setShoppingLists(prev => [...prev, newList]);
|
||||
try {
|
||||
const newList = await createListApi(name);
|
||||
if (newList) {
|
||||
setShoppingLists(prev => [...prev, newList]);
|
||||
}
|
||||
} catch (e) {
|
||||
// The useApi hook handles setting the error state.
|
||||
// We catch the error here to prevent unhandled promise rejections and add logging.
|
||||
console.error('useShoppingLists: Failed to create list.', e);
|
||||
}
|
||||
}, [user, setShoppingLists, createListApi]);
|
||||
|
||||
const deleteList = useCallback(async (listId: number) => {
|
||||
if (!user) return;
|
||||
const result = await deleteListApi(listId);
|
||||
// A successful DELETE will have a null result from useApi (for 204 No Content)
|
||||
if (result === null) {
|
||||
const newLists = shoppingLists.filter(l => l.shopping_list_id !== listId);
|
||||
setShoppingLists(newLists);
|
||||
if (activeListId === listId) {
|
||||
setActiveListId(newLists.length > 0 ? newLists[0].shopping_list_id : null);
|
||||
try {
|
||||
const result = await deleteListApi(listId);
|
||||
// A successful DELETE will have a null result from useApi (for 204 No Content)
|
||||
if (result === null) {
|
||||
const newLists = shoppingLists.filter(l => l.shopping_list_id !== listId);
|
||||
setShoppingLists(newLists);
|
||||
if (activeListId === listId) {
|
||||
setActiveListId(newLists.length > 0 ? newLists[0].shopping_list_id : null);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('useShoppingLists: Failed to delete list.', e);
|
||||
}
|
||||
}, [user, shoppingLists, activeListId, setShoppingLists, deleteListApi]);
|
||||
|
||||
const addItemToList = useCallback(async (listId: number, item: { masterItemId?: number, customItemName?: string }) => {
|
||||
if (!user) return;
|
||||
const newItem = await addItemApi(listId, item);
|
||||
if (newItem) {
|
||||
setShoppingLists(prevLists => prevLists.map(list => {
|
||||
if (list.shopping_list_id === listId) {
|
||||
const itemExists = list.items.some(i => i.shopping_list_item_id === newItem.shopping_list_item_id);
|
||||
if (itemExists) return list;
|
||||
return { ...list, items: [...list.items, newItem] };
|
||||
}
|
||||
return list;
|
||||
}));
|
||||
try {
|
||||
const newItem = await addItemApi(listId, item);
|
||||
if (newItem) {
|
||||
setShoppingLists(prevLists => prevLists.map(list => {
|
||||
if (list.shopping_list_id === listId) {
|
||||
// Prevent adding a duplicate item if it's a master item and already exists in the list.
|
||||
// We don't prevent duplicates for custom items as they don't have a unique ID.
|
||||
const itemExists = newItem.master_item_id
|
||||
? list.items.some(i => i.master_item_id === newItem.master_item_id)
|
||||
: false;
|
||||
if (itemExists) return list;
|
||||
|
||||
return { ...list, items: [...list.items, newItem] };
|
||||
}
|
||||
return list;
|
||||
}));
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('useShoppingLists: Failed to add item.', e);
|
||||
}
|
||||
}, [user, setShoppingLists, addItemApi]);
|
||||
|
||||
const updateItemInList = useCallback(async (itemId: number, updates: Partial<ShoppingListItem>) => {
|
||||
if (!user || !activeListId) return;
|
||||
const updatedItem = await updateItemApi(itemId, updates);
|
||||
if (updatedItem) {
|
||||
setShoppingLists(prevLists => prevLists.map(list => {
|
||||
if (list.shopping_list_id === activeListId) {
|
||||
return { ...list, items: list.items.map(i => i.shopping_list_item_id === itemId ? updatedItem : i) };
|
||||
}
|
||||
return list;
|
||||
}));
|
||||
try {
|
||||
const updatedItem = await updateItemApi(itemId, updates);
|
||||
if (updatedItem) {
|
||||
setShoppingLists(prevLists => prevLists.map(list => {
|
||||
if (list.shopping_list_id === activeListId) {
|
||||
return { ...list, items: list.items.map(i => i.shopping_list_item_id === itemId ? updatedItem : i) };
|
||||
}
|
||||
return list;
|
||||
}));
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('useShoppingLists: Failed to update item.', e);
|
||||
}
|
||||
}, [user, activeListId, setShoppingLists, updateItemApi]);
|
||||
|
||||
const removeItemFromList = useCallback(async (itemId: number) => {
|
||||
if (!user || !activeListId) return;
|
||||
const result = await removeItemApi(itemId);
|
||||
if (result === null) {
|
||||
setShoppingLists(prevLists => prevLists.map(list => {
|
||||
if (list.shopping_list_id === activeListId) {
|
||||
return { ...list, items: list.items.filter(i => i.shopping_list_item_id !== itemId) };
|
||||
}
|
||||
return list;
|
||||
}));
|
||||
try {
|
||||
const result = await removeItemApi(itemId);
|
||||
if (result === null) {
|
||||
setShoppingLists(prevLists => prevLists.map(list => {
|
||||
if (list.shopping_list_id === activeListId) {
|
||||
return { ...list, items: list.items.filter(i => i.shopping_list_item_id !== itemId) };
|
||||
}
|
||||
return list;
|
||||
}));
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('useShoppingLists: Failed to remove item.', e);
|
||||
}
|
||||
}, [user, activeListId, setShoppingLists, removeItemApi]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user