From 835298ac920f66c107003e1f81b19e30efdc819c Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Tue, 6 Oct 2020 17:16:06 -0500 Subject: [PATCH] Added name and price of the cart item --- components/cart/CartItem/CartItem.tsx | 24 +++++++++-- .../cart/CartSidebarView/CartSidebarView.tsx | 6 ++- components/ui/context.tsx | 1 - utils/format-item-price.ts | 41 +++++++++++++++++++ 4 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 utils/format-item-price.ts diff --git a/components/cart/CartItem/CartItem.tsx b/components/cart/CartItem/CartItem.tsx index d668c31bf..692217979 100644 --- a/components/cart/CartItem/CartItem.tsx +++ b/components/cart/CartItem/CartItem.tsx @@ -1,11 +1,29 @@ import { Trash } from '@components/icon' +import { useCommerce } from '@lib/bigcommerce' +import formatVariantPrice from 'utils/format-item-price' + +const CartItem = ({ + item, + currencyCode, +}: { + item: any + currencyCode: string +}) => { + const { locale } = useCommerce() + const { price } = formatVariantPrice({ + listPrice: item.extended_list_price, + salePrice: item.extended_sale_price, + currencyCode, + locale, + }) + + console.log('ITEM', item) -const CartItem = () => { return (
  • - T-Shirt + {item.name}
    - {
    - $50.00 + {price} diff --git a/components/cart/CartSidebarView/CartSidebarView.tsx b/components/cart/CartSidebarView/CartSidebarView.tsx index c918cf67a..5fa3c04d5 100644 --- a/components/cart/CartSidebarView/CartSidebarView.tsx +++ b/components/cart/CartSidebarView/CartSidebarView.tsx @@ -50,7 +50,11 @@ const CartSidebarView: FC = () => {
      {items.map((item) => ( - + ))}
    diff --git a/components/ui/context.tsx b/components/ui/context.tsx index 0c29063f9..f9014c194 100644 --- a/components/ui/context.tsx +++ b/components/ui/context.tsx @@ -43,7 +43,6 @@ export const useUI = () => { } function uiReducer(state: State, action: Action) { - console.log(action) switch (action.type) { case 'OPEN_SIDEBAR': { return { diff --git a/utils/format-item-price.ts b/utils/format-item-price.ts new file mode 100644 index 000000000..180f12182 --- /dev/null +++ b/utils/format-item-price.ts @@ -0,0 +1,41 @@ +function formatPrice({ + amount, + currencyCode, + locale, +}: { + amount: number + currencyCode: string + locale: string +}) { + const formatCurrency = new Intl.NumberFormat(locale, { + style: 'currency', + currency: currencyCode, + }) + + return formatCurrency.format(amount) +} + +export default function formatVariantPrice({ + listPrice, + salePrice, + currencyCode, + locale, +}: { + listPrice: number + salePrice: number + currencyCode: string + locale: string +}) { + const hasDiscount = listPrice > salePrice + const formatDiscount = new Intl.NumberFormat(locale, { style: 'percent' }) + const discount = hasDiscount + ? formatDiscount.format((listPrice - salePrice) / listPrice) + : null + + const price = formatPrice({ amount: salePrice, currencyCode, locale }) + const compareAtPrice = hasDiscount + ? formatPrice({ amount: listPrice, currencyCode, locale }) + : null + + return { price, compareAtPrice, discount } +}