Updated normalizer for useData

This commit is contained in:
Luis Alvarez 2021-01-27 15:41:04 -05:00
parent 61fa423673
commit 7cf1ace9fb
4 changed files with 30 additions and 16 deletions

View File

@ -92,11 +92,11 @@ const CartSidebarView: FC = () => {
My Cart
</h2>
<ul className="py-6 space-y-6 sm:py-0 sm:space-y-0 sm:divide-y sm:divide-accents-3 border-t border-accents-3">
{data?.items?.map((item) => (
{data!.lineItems.map((item) => (
<CartItem
key={item.id}
item={item}
currencyCode={data?.currency.code!}
currencyCode={data!.currency.code}
/>
))}
</ul>

View File

@ -34,9 +34,7 @@ export function extendHook(
descriptors: {
isEmpty: {
get() {
return Object.values(response.data?.line_items ?? {}).every(
(items) => !items.length
)
return (response.data?.lineItems.length ?? 0) <= 0
},
enumerable: true,
},

View File

@ -69,14 +69,30 @@ export function normalizeProduct(productNode: any): Product {
}
export function normalizeCart(data: BigcommerceCart): Cart {
const d: BaseCart = data && {
// const d: BaseCart = data && {
// id: data.id,
// customerId: String(data.customer_id),
// email: data.email,
// createdAt: data.created_time,
// currency: data.currency,
// taxesIncluded: data.tax_included,
// lineItems: data.line_items as any,
// lineItemsSubtotalPrice: data.base_amount,
// subtotalPrice: data.base_amount + data.discount_amount,
// totalPrice: data.cart_amount,
// discounts: data.discounts?.map((discount) => ({
// value: discount.discounted_amount,
// })),
// }
return {
id: data.id,
customerId: String(data.customer_id),
email: data.email,
createdAt: data.created_time,
currency: data.currency,
taxesIncluded: data.tax_included,
lineItems: data.line_items as any,
lineItems: data.line_items.physical_items.map(itemsToProducts) as any,
lineItemsSubtotalPrice: data.base_amount,
subtotalPrice: data.base_amount + data.discount_amount,
totalPrice: data.cart_amount,
@ -85,14 +101,14 @@ export function normalizeCart(data: BigcommerceCart): Cart {
})),
}
return update(data as any, {
$auto: {
items: { $set: data?.line_items?.physical_items?.map(itemsToProducts) },
subTotal: { $set: data?.base_amount },
total: { $set: data?.cart_amount },
},
$unset: ['created_time', 'coupons', 'line_items', 'email'],
})
// return update(data as any, {
// $auto: {
// items: { $set: data?.line_items?.physical_items?.map(itemsToProducts) },
// subTotal: { $set: data?.base_amount },
// total: { $set: data?.cart_amount },
// },
// $unset: ['created_time', 'coupons', 'line_items', 'email'],
// })
}
function itemsToProducts(item: any): CartItem {

View File

@ -114,7 +114,7 @@ interface BaseCart {
currency: { code: string }
// Specifies if taxes are included in the line items.
taxesIncluded: boolean
lineItems: Pick<Product, 'id' | 'name' | 'prices'> & CartItem[]
lineItems: BaseLineItem[]
// The sum of all the prices of all the items in the cart.
// Duties, taxes, shipping and discounts excluded.
lineItemsSubtotalPrice: number