From 80614a1df0d376ca23de2b478577a09b3c0cf461 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Wed, 7 Oct 2020 12:56:51 -0500 Subject: [PATCH] Fix cart removal --- components/cart/CartSidebarView/CartSidebarView.tsx | 2 +- components/core/UserNav/UserNav.tsx | 8 +++++--- lib/bigcommerce/api/cart.ts | 8 +++----- lib/bigcommerce/api/utils/fetch-store-api.ts | 5 ++--- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/components/cart/CartSidebarView/CartSidebarView.tsx b/components/cart/CartSidebarView/CartSidebarView.tsx index 5fa3c04d5..e2cfab68c 100644 --- a/components/cart/CartSidebarView/CartSidebarView.tsx +++ b/components/cart/CartSidebarView/CartSidebarView.tsx @@ -42,7 +42,7 @@ const CartSidebarView: FC = () => { {isEmpty ? ( -
+
) : ( diff --git a/components/core/UserNav/UserNav.tsx b/components/core/UserNav/UserNav.tsx index 0a2241fbd..8e4949ba1 100644 --- a/components/core/UserNav/UserNav.tsx +++ b/components/core/UserNav/UserNav.tsx @@ -25,9 +25,11 @@ const UserNav: FC = ({ className }) => {
  • openSidebar()}> - - {itemsCount} - + {itemsCount > 0 && ( + + {itemsCount} + + )}
  • diff --git a/lib/bigcommerce/api/cart.ts b/lib/bigcommerce/api/cart.ts index 1a563b943..3f917cabc 100644 --- a/lib/bigcommerce/api/cart.ts +++ b/lib/bigcommerce/api/cart.ts @@ -142,11 +142,9 @@ const cartApi: BigcommerceApiHandler = async (req, res, config) => { }) } - const { data } = await config.storeApiFetch( + const result = await config.storeApiFetch<{ data: any } | null>( `/v3/carts/${cartId}/items/${itemId}`, - { - method: 'DELETE', - } + { method: 'DELETE' } ) // Update the cart cookie @@ -155,7 +153,7 @@ const cartApi: BigcommerceApiHandler = async (req, res, config) => { getCartCookie(config.cartCookie, cartId, config.cartCookieMaxAge) ) - return res.status(200).json({ data }) + return res.status(200).json({ data: result?.data }) } } catch (error) { console.error(error) diff --git a/lib/bigcommerce/api/utils/fetch-store-api.ts b/lib/bigcommerce/api/utils/fetch-store-api.ts index 687c53c0e..0f0daedfe 100644 --- a/lib/bigcommerce/api/utils/fetch-store-api.ts +++ b/lib/bigcommerce/api/utils/fetch-store-api.ts @@ -37,9 +37,8 @@ export default async function fetchStoreApi( ) } - const data = await res.json() - - return data + // If something was removed, the response will be empty + return res.status === 204 ? null : await res.json() } async function getErrorText(res: Response) {