diff --git a/src/components/ShoppingCart.svelte b/src/components/ShoppingCart.svelte index 6536485..42452d7 100644 --- a/src/components/ShoppingCart.svelte +++ b/src/components/ShoppingCart.svelte @@ -9,10 +9,11 @@ import { authService, cartService } from '$lib/services/api'; let isOpen = false; - let isUpdating = false; + let updatingItemId: string | null = null; let updateTimeout: NodeJS.Timeout; let cartItems: CartItem[] = []; let isLoading = true; + let showClearConfirmation = false; onMount(async () => { await loadCart(); @@ -41,7 +42,7 @@ // Debounced quantity update const handleQuantityChange = async (cartItemId: string, quantity: number) => { clearTimeout(updateTimeout); - isUpdating = true; + updatingItemId = cartItemId; updateTimeout = setTimeout(async () => { try { @@ -50,10 +51,21 @@ } catch (error) { console.error('Failed to update quantity:', error); } - isUpdating = false; + updatingItemId = null; }, 500); }; + // Handle increment/decrement buttons + const updateQuantity = async (cartItemId: string, delta: number) => { + const item = cartItems.find((item) => item.id === cartItemId); + if (item) { + const newQuantity = item.quantity + delta; + if (newQuantity >= 1) { + await handleQuantityChange(cartItemId, newQuantity); + } + } + }; + const handleClickOutside = () => { isOpen = false; }; @@ -79,6 +91,7 @@ // Clear entire cart const clearEntireCart = async () => { + showClearConfirmation = false; try { const currentUser = authService.getCurrentUser(); if (currentUser) { @@ -95,11 +108,22 @@ isOpen = !isOpen; goto('/main'); }; + + // Keyboard event handling + onMount(() => { + window.addEventListener('keydown', handleKeyDown); + }); + + const handleKeyDown = (event: KeyboardEvent) => { + if (event.key === 'Escape' && isOpen) { + isOpen = false; + } + };
- +
+ Empty Cart +

Your cart is empty

+ +
{:else} -