From a726f51edd63e09bd813f5fee5f43091319ffa89 Mon Sep 17 00:00:00 2001 From: Martin Bavio Date: Fri, 30 Oct 2020 03:58:15 -0300 Subject: [PATCH] Abstract cart screen logic into its own hook --- lib/hooks/use-cart.ts | 32 ++++++++++++++++++++++++++++++ pages/cart.tsx | 46 ++++++++++++++++++++++--------------------- 2 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 lib/hooks/use-cart.ts diff --git a/lib/hooks/use-cart.ts b/lib/hooks/use-cart.ts new file mode 100644 index 000000000..b48b174ec --- /dev/null +++ b/lib/hooks/use-cart.ts @@ -0,0 +1,32 @@ +import useBigCommerceCart from '@bigcommerce/storefront-data-hooks/cart/use-cart' +import useBigCommercePrice from '@bigcommerce/storefront-data-hooks/use-price' + +export const useCart = () => { + const { data, error, isEmpty } = useBigCommerceCart() + const isLoading = data === undefined + + const { price: subtotal } = useBigCommercePrice( + data && { + amount: data.base_amount, + currencyCode: data.currency.code, + } + ) + const { price: total } = useBigCommercePrice( + data && { + amount: data.cart_amount, + currencyCode: data.currency.code, + } + ) + + const items = data?.line_items.physical_items ?? [] + + return { + items: data?.line_items.physical_items ?? [], + isLoading: data === undefined, + isError: error, + isEmpty, + subtotal, + total, + currency: data?.currency, + } +} diff --git a/pages/cart.tsx b/pages/cart.tsx index c7d7d588c..f1da4f959 100644 --- a/pages/cart.tsx +++ b/pages/cart.tsx @@ -1,8 +1,7 @@ import type { GetStaticPropsContext } from 'next' import { getConfig } from '@bigcommerce/storefront-data-hooks/api' import getAllPages from '@bigcommerce/storefront-data-hooks/api/operations/get-all-pages' -import useCart from '@bigcommerce/storefront-data-hooks/cart/use-cart' -import usePrice from '@bigcommerce/storefront-data-hooks/use-price' +import { useCart } from '@lib/hooks/use-cart' import { Layout } from '@components/core' import { Button } from '@components/ui' import { Bag, Cross, Check } from '@components/icons' @@ -21,28 +20,20 @@ export async function getStaticProps({ } export default function Cart() { - const { data, error, isEmpty } = useCart() - const isLoading = data === undefined - - const { price: subTotal } = usePrice( - data && { - amount: data.base_amount, - currencyCode: data.currency.code, - } - ) - const { price: total } = usePrice( - data && { - amount: data.cart_amount, - currencyCode: data.currency.code, - } - ) - - const items = data?.line_items.physical_items ?? [] + const { + items, + isLoading, + isError, + isEmpty, + subtotal, + total, + currency, + } = useCart() return (
My Cart - {error &&
Failed to load
} + {isError &&
Failed to load
} {isLoading &&
Loading...
} {isEmpty ? (
@@ -66,11 +57,22 @@ export default function Cart() { ))}
+
+
+ Before you leave, take a look at these items. We picked them + just for you: +
+
+ {[1, 2, 3, 4, 5, 6].map((x) => ( +
+ ))} +
+
@@ -78,7 +80,7 @@ export default function Cart() {
  • Subtotal - {subTotal} + {subtotal}
  • Taxes