Added more cart types

This commit is contained in:
Luis Alvarez 2021-01-25 13:51:15 -05:00
parent 8784e05183
commit a49d0a18f8
5 changed files with 107 additions and 6 deletions

View File

@ -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
}

View File

@ -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 }
>
}

View File

@ -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<UseCartResponse | null, CartInput> = (
options,

View File

@ -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 },

82
framework/types.d.ts vendored
View File

@ -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 variants 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<Product, 'id' | 'name' | 'prices'> & 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 }