From 5080fadd60f37cfb33e6d9f558367a2bcebda5bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Pe=CC=81ault?= Date: Mon, 15 Nov 2021 17:02:44 +0100 Subject: [PATCH] Create a new cart if current cart is not active/has been paid --- src/components/organisms/Cart.svelte | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/components/organisms/Cart.svelte b/src/components/organisms/Cart.svelte index 88076c6..90ac3d2 100644 --- a/src/components/organisms/Cart.svelte +++ b/src/components/organisms/Cart.svelte @@ -23,9 +23,31 @@ }) if (existantCart.ok) { const cart = await existantCart.json() - $cartId = cart.id - $cartData = cart // console.log('Fetched existant cart:', $cartId, $cartData) + + // Cart is active / has not been ordered + if (cart.active || !cart.order_id) { + // Keep current cart + $cartId = cart.id + $cartData = cart + } + + // Cart is inactive / has been ordered + else { + // Fetch a new cart + const newCart = await fetch('/api/swell', { + method: 'POST', + body: JSON.stringify({ + action: 'createCart' + }) + }) + if (newCart.ok) { + const cart = await newCart.json() + // Save new cart + $cartId = cart.id + $cartData = cart + } + } } } // Cart doesn't exists