From a49d0a18f8c182eeb40c77b2bb76b8c0ca7867c2 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Mon, 25 Jan 2021 13:51:15 -0500 Subject: [PATCH] Added more cart types --- framework/bigcommerce/api/cart/index.ts | 2 + framework/bigcommerce/api/catalog/products.ts | 2 +- framework/bigcommerce/cart/use-cart.tsx | 4 +- framework/bigcommerce/lib/normalize.ts | 23 +++++- framework/types.d.ts | 82 +++++++++++++++++++ 5 files changed, 107 insertions(+), 6 deletions(-) diff --git a/framework/bigcommerce/api/cart/index.ts b/framework/bigcommerce/api/cart/index.ts index 715480bd8..8988f3606 100644 --- a/framework/bigcommerce/api/cart/index.ts +++ b/framework/bigcommerce/api/cart/index.ts @@ -45,6 +45,8 @@ export type Cart = { gift_certificates: any[] physical_items: any[] } + created_time: string + discounts?: { id: number; discounted_amount: number }[] // TODO: add missing fields } diff --git a/framework/bigcommerce/api/catalog/products.ts b/framework/bigcommerce/api/catalog/products.ts index fe5b807c6..d13dcd3c3 100644 --- a/framework/bigcommerce/api/catalog/products.ts +++ b/framework/bigcommerce/api/catalog/products.ts @@ -15,7 +15,7 @@ export type SearchProductsData = { export type ProductsHandlers = { getProducts: BigcommerceHandler< SearchProductsData, - { search?: 'string'; category?: string; brand?: string; sort?: string } + { search?: string; category?: string; brand?: string; sort?: string } > } diff --git a/framework/bigcommerce/cart/use-cart.tsx b/framework/bigcommerce/cart/use-cart.tsx index c1a9bed7a..0d2070f63 100644 --- a/framework/bigcommerce/cart/use-cart.tsx +++ b/framework/bigcommerce/cart/use-cart.tsx @@ -3,14 +3,14 @@ import type { HookFetcher } from '@commerce/utils/types' import type { SwrOptions } from '@commerce/utils/use-data' import useResponse from '@commerce/utils/use-response' import useCommerceCart, { CartInput } from '@commerce/cart/use-cart' -import type { Cart as BigCommerceCart } from '../api/cart' +import type { Cart as BigcommerceCart } from '../api/cart' const defaultOpts = { url: '/api/bigcommerce/cart', method: 'GET', } -type UseCartResponse = BigCommerceCart & Cart +type UseCartResponse = BigcommerceCart & Cart export const fetcher: HookFetcher = ( options, diff --git a/framework/bigcommerce/lib/normalize.ts b/framework/bigcommerce/lib/normalize.ts index bbce17214..535e77501 100644 --- a/framework/bigcommerce/lib/normalize.ts +++ b/framework/bigcommerce/lib/normalize.ts @@ -1,4 +1,5 @@ -import update from '@framework/lib/immutability' +import type { Cart as BigCommerceCart } from '../api/cart' +import update from './immutability' function normalizeProductOption(productOption: any) { const { @@ -67,8 +68,24 @@ export function normalizeProduct(productNode: any): Product { }) } -export function normalizeCart(data: any): Cart { - return update(data, { +export function normalizeCart(data: BigCommerceCart): Cart { + 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 update(data as any, { $auto: { items: { $set: data?.line_items?.physical_items?.map(itemsToProducts) }, subTotal: { $set: data?.base_amount }, diff --git a/framework/types.d.ts b/framework/types.d.ts index 7943cfaef..e5b1ed7c1 100644 --- a/framework/types.d.ts +++ b/framework/types.d.ts @@ -45,6 +45,88 @@ interface ProductPrice { extendedListPrice?: number } +interface DiscountBase { + // The value of the discount, can be an amount or percentage + value: number +} + +interface BaseLineItem { + id: string + variantId: string + name: string + quantity: number + discounts: DiscountBase[] + variant: BaseProductVariant +} + +interface Measurement { + value: number + unit: 'KILOGRAMS' | 'GRAMS' | 'POUNDS' | 'OUNCES' +} + +interface Image { + url: string + altText?: string + width?: number + height?: number +} + +interface BaseProductVariant { + id: string + // The SKU (stock keeping unit) associated with the product variant. + sku: string + // The product variant’s title, or the product's name. + name: string + // Indicates whether this product variant is in stock. + isInStock: boolean + // Indicates if the product variant is available for sale. + availableForSale: boolean + // Whether a customer needs to provide a shipping address when placing + // an order for the product variant. + requiresShipping: boolean + // Image associated with the product variant. Falls back to the product image + // if no image is available. + image: Image + // The variant's weight. If a weight was not explicitly specified on the + // variant this will be the product's weight. + weight?: Measurement + // The variant's height. If a height was not explicitly specified on the + // variant, this will be the product's height. + height?: Measurement + // The variant's width. If a width was not explicitly specified on the + // variant, this will be the product's width. + width?: Measurement + // The variant's depth. If a depth was not explicitly specified on the + // variant, this will be the product's depth. + depth?: Measurement +} + +// Shopping cart, a.k.a Checkout +interface BaseCart { + id: string + // ID of the customer to which the cart belongs. + customerId?: string + // The email assigned to this cart + email?: string + // The date and time when the cart was created. + createdAt: string + // The currency used for this cart + currency: { code: string } + // Specifies if taxes are included in the line items. + taxesIncluded: boolean + lineItems: Pick & CartItem[] + // The sum of all the prices of all the items in the cart. + // Duties, taxes, shipping and discounts excluded. + lineItemsSubtotalPrice: number + // Price of the cart before duties, shipping and taxes. + subtotalPrice: number + // The sum of all the prices of all the items in the cart. + // Duties, taxes and discounts included. + totalPrice: number + // Discounts that have been applied on the cart. + discounts?: DiscountBase[] +} + interface Cart extends Entity { id: string | undefined currency: { code: string }