From f7457b5f8d8a24c2a4cf8bc06c88f9999d5722fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Pe=CC=81ault?= Date: Sun, 7 Nov 2021 20:21:20 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Finish=20Cart=20implementation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Use Select component in CartItem to change quantity - Visual update when removing or changing an item quantity - API: Remove Cart item action - API: Handle Cart item adding and updating (quantity) --- src/components/molecules/CartItem.svelte | 56 ++++++++------ src/components/organisms/Cart.svelte | 64 ++++++++++------ src/routes/api/swell.ts | 8 +- src/style/molecules/_cart-item.scss | 80 ++++++++++++++++++-- src/style/organisms/_cart.scss | 81 ++++++++++++++++---- src/style/pages/_shop.scss | 6 +- src/utils/store.ts | 5 +- src/utils/swellFunctions.ts | 95 ++++++++++++++++-------- 8 files changed, 290 insertions(+), 105 deletions(-) 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}