diff --git a/src/components/molecules/CartItem.svelte b/src/components/molecules/CartItem.svelte index a87dd98..c67f958 100644 --- a/src/components/molecules/CartItem.svelte +++ b/src/components/molecules/CartItem.svelte @@ -1,17 +1,21 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/src/components/organisms/Cart.svelte b/src/components/organisms/Cart.svelte index e880d87..34b4d75 100644 --- a/src/components/organisms/Cart.svelte +++ b/src/components/organisms/Cart.svelte @@ -2,7 +2,7 @@ import { onMount } from 'svelte' import { fade, fly } from 'svelte/transition' import { quartOut } from 'svelte/easing' - import { cartOpen, cartId, cartData, cartAmount } from '$utils/store' + import { cartOpen, cartId, cartData, cartAmount, cartIsUpdating } from '$utils/store' // Components import Button from '$components/atoms/Button.svelte' import CartItem from '$components/molecules/CartItem.svelte' @@ -25,7 +25,7 @@ const cart = await existantCart.json() $cartId = cart.id $cartData = cart - console.log('fetched existant cart:', $cartId, $cartData) + // console.log('Fetched existant cart:', $cartId, $cartData) } } // Cart doesn't exists @@ -41,7 +41,7 @@ const cart = await newCart.json() $cartId = cart.id $cartData = cart - console.log('new cart:', localStorage.getItem('cartId')) + // console.log('Created new cart:', localStorage.getItem('cartId')) } } }) @@ -54,43 +54,53 @@ // Item quantity changed const changedQuantity = async ({ detail: { id, quantity } }) => { - // Update item in cart - // const updatedCart = await swell.cart.updateItem(id, { - // quantity - // }) - // if (updatedCart) { - // $cartData = updatedCart - // } + // Cart is now updating + $cartIsUpdating = true + // Get updated cart const updatedCart = await fetch('/api/swell', { method: 'POST', body: JSON.stringify({ action: 'updateCartItem', cartId: $cartId, productId: id, - quantity: quantity + quantity, }) }) - if (updatedCart.ok) { - // const cart = await updatedCart.json() - // $cartData = cart - // console.log('updated cart:', $cartData.items) + const cart = await updatedCart.json() + $cartData = cart + // Cart is updated + $cartIsUpdating = false } } // Item removed const removedItem = async ({ detail: id }) => { + // Cart is now updating + $cartIsUpdating = true + // Remove item from cart - // const removedItem = await swell.cart.removeItem(id) - // if (removedItem) { - // $cartData = removedItem - // } + const updatedCart = await fetch('/api/swell', { + method: 'POST', + body: JSON.stringify({ + action: 'removeCartItem', + cartId: $cartId, + productId: id, + }) + }) + if (updatedCart.ok) { + const cart = await updatedCart.json() + $cartData = cart + // Cart is updated + $cartIsUpdating = false + } } {#if $cartOpen}