diff --git a/components/cart/CartItem/CartItem.tsx b/components/cart/CartItem/CartItem.tsx index 71ab58966..1e9062e91 100644 --- a/components/cart/CartItem/CartItem.tsx +++ b/components/cart/CartItem/CartItem.tsx @@ -5,6 +5,7 @@ import Link from 'next/link' import s from './CartItem.module.css' import { Trash, Plus, Minus } from '@components/icons' import { useUI } from '@components/ui/context' +import type { LineItem } from '@framework/types' import usePrice from '@framework/product/use-price' import useUpdateItem from '@framework/cart/use-update-item' import useRemoveItem from '@framework/cart/use-remove-item' diff --git a/framework/bigcommerce/api/cart/index.ts b/framework/bigcommerce/api/cart/index.ts index 8988f3606..007e56ace 100644 --- a/framework/bigcommerce/api/cart/index.ts +++ b/framework/bigcommerce/api/cart/index.ts @@ -53,10 +53,7 @@ export type Cart = { export type CartHandlers = { getCart: BigcommerceHandler addItem: BigcommerceHandler> - updateItem: BigcommerceHandler< - Cart, - { cartId?: string } & Partial - > + updateItem: BigcommerceHandler removeItem: BigcommerceHandler< Cart, { cartId?: string } & Partial diff --git a/framework/bigcommerce/types.d.ts b/framework/bigcommerce/types.d.ts deleted file mode 100644 index d987381e7..000000000 --- a/framework/bigcommerce/types.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -interface Cart extends BaseCart { - lineItems: LineItem[] -} - -interface LineItem extends BaseLineItem {} diff --git a/framework/bigcommerce/types.ts b/framework/bigcommerce/types.ts new file mode 100644 index 000000000..fe1ca6c36 --- /dev/null +++ b/framework/bigcommerce/types.ts @@ -0,0 +1,11 @@ +import * as Core from '@commerce/types' + +export interface Cart extends Core.Cart { + lineItems: LineItem[] +} + +export interface LineItem extends Core.LineItem {} + +export interface UpdateLineItemBody extends Core.UpdateLineItemBody {} + +export interface UpdateLineItem extends Core.UpdateItemBody {} diff --git a/framework/commerce/cart/use-cart.tsx b/framework/commerce/cart/use-cart.tsx index ecc537539..94ccb73fc 100644 --- a/framework/commerce/cart/use-cart.tsx +++ b/framework/commerce/cart/use-cart.tsx @@ -1,15 +1,16 @@ import Cookies from 'js-cookie' import type { HookInput, HookFetcher, HookFetcherOptions } from '../utils/types' import useData, { ResponseState, SwrOptions } from '../utils/use-data' +import type { Cart } from '../types' import { useCommerce } from '..' export type CartResponse = ResponseState & { isEmpty?: boolean } export type CartInput = { - cartId?: BaseCart['id'] + cartId?: Cart['id'] } -export default function useCart( +export default function useCart( options: HookFetcherOptions, input: HookInput, fetcherFn: HookFetcher, diff --git a/framework/commerce/types.d.ts b/framework/commerce/types.d.ts index 9bb996470..9e69ec25d 100644 --- a/framework/commerce/types.d.ts +++ b/framework/commerce/types.d.ts @@ -45,105 +45,6 @@ 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[] - // A human-friendly unique string automatically generated from the product’s name - path: string - 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 - // Whether a customer needs to provide a shipping address when placing - // an order for the product variant. - requiresShipping: boolean - // The product variant’s price after all discounts are applied. - price: number - // Product variant’s price, as quoted by the manufacturer/distributor. - listPrice: number - // Image associated with the product variant. Falls back to the product image - // if no image is available. - image?: Image - // Indicates whether this product variant is in stock. - isInStock?: boolean - // Indicates if the product variant is available for sale. - availableForSale?: boolean - // 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: BaseLineItem[] - // 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[] -} - -// TODO: Remove this type in favor of BaseCart -interface Cart2 extends Entity { - id: string | undefined - currency: { code: string } - taxIncluded?: boolean - items: Pick & CartItem[] - subTotal: number | string - total: number | string - customerId: Customer['id'] -} - interface CartItem extends Entity { quantity: number productId: Product['id'] diff --git a/framework/commerce/types.ts b/framework/commerce/types.ts new file mode 100644 index 000000000..3bca844e0 --- /dev/null +++ b/framework/commerce/types.ts @@ -0,0 +1,108 @@ +export interface Discount { + // The value of the discount, can be an amount or percentage + value: number +} + +export interface LineItem { + id: string + variantId: string + name: string + quantity: number + discounts: Discount[] + // A human-friendly unique string automatically generated from the product’s name + path: string + variant: ProductVariant +} + +export interface Measurement { + value: number + unit: 'KILOGRAMS' | 'GRAMS' | 'POUNDS' | 'OUNCES' +} + +export interface Image { + url: string + altText?: string + width?: number + height?: number +} + +export interface ProductVariant { + 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 + // Whether a customer needs to provide a shipping address when placing + // an order for the product variant. + requiresShipping: boolean + // The product variant’s price after all discounts are applied. + price: number + // Product variant’s price, as quoted by the manufacturer/distributor. + listPrice: number + // Image associated with the product variant. Falls back to the product image + // if no image is available. + image?: Image + // Indicates whether this product variant is in stock. + isInStock?: boolean + // Indicates if the product variant is available for sale. + availableForSale?: boolean + // 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 +export interface Cart { + 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: LineItem[] + // 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?: Discount[] +} + +// interface OptionSelections { +// option_id: number +// option_value: number | string +// } + +export interface LineItemBody { + productId: string + variantId: string + quantity?: number + // optionSelections?: OptionSelections +} + +export interface UpdateLineItemBody { + itemId: string + item: LineItemBody +} + +export interface UpdateItemBody extends Partial { + cartId?: string +}