From 2f3535894228a966dc7e8264cc489c6b92f61fba Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Mon, 5 Oct 2020 01:05:04 -0500 Subject: [PATCH] See if cart is empty --- components/cart/CartSidebarView/CartSidebarView.tsx | 5 +++++ lib/bigcommerce/api/cart.ts | 6 ++++++ lib/bigcommerce/cart/index.tsx | 10 ++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/components/cart/CartSidebarView/CartSidebarView.tsx b/components/cart/CartSidebarView/CartSidebarView.tsx index 96fd650ae..f18144fa9 100644 --- a/components/cart/CartSidebarView/CartSidebarView.tsx +++ b/components/cart/CartSidebarView/CartSidebarView.tsx @@ -3,9 +3,14 @@ import { UserNav } from '@components/core' import { Button } from '@components/ui' import { Trash, Cross } from '@components/icon' import { useUI } from '@components/ui/context' +import { useCart } from '@lib/bigcommerce/cart' const CartSidebarView: FC = () => { + const { data, isEmpty } = useCart() const { closeSidebar } = useUI() + + console.log('CART', data, isEmpty) + return ( <>
diff --git a/lib/bigcommerce/api/cart.ts b/lib/bigcommerce/api/cart.ts index b54a5fc14..0ee234f26 100644 --- a/lib/bigcommerce/api/cart.ts +++ b/lib/bigcommerce/api/cart.ts @@ -23,6 +23,12 @@ export type Cart = { base_amount: number discount_amount: number cart_amount: number + line_items: { + custom_items: any[] + digital_items: any[] + gift_certificates: any[] + psysical_items: any[] + } // TODO: add missing fields } diff --git a/lib/bigcommerce/cart/index.tsx b/lib/bigcommerce/cart/index.tsx index d554443f0..dc4601987 100644 --- a/lib/bigcommerce/cart/index.tsx +++ b/lib/bigcommerce/cart/index.tsx @@ -18,8 +18,14 @@ export const CartProvider: FC = ({ children }) => { export function useCart() { const cart = useCommerceCart() - // TODO: Do something to make this prop work - cart.isEmpty = true + Object.defineProperty(cart, 'isEmpty', { + get() { + return Object.values(cart.data?.line_items ?? {}).every( + (items) => !items.length + ) + }, + set: (x) => x, + }) return cart }