Switch away from global types

This commit is contained in:
Luis Alvarez 2021-01-29 11:30:17 -05:00
parent 2ffe1ab71b
commit 1de3e76513
7 changed files with 124 additions and 110 deletions

View File

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

View File

@ -53,10 +53,7 @@ export type Cart = {
export type CartHandlers = {
getCart: BigcommerceHandler<Cart, { cartId?: string }>
addItem: BigcommerceHandler<Cart, { cartId?: string } & Partial<AddItemBody>>
updateItem: BigcommerceHandler<
Cart,
{ cartId?: string } & Partial<UpdateItemBody>
>
updateItem: BigcommerceHandler<Cart, UpdateItemBody>
removeItem: BigcommerceHandler<
Cart,
{ cartId?: string } & Partial<RemoveItemBody>

View File

@ -1,5 +0,0 @@
interface Cart extends BaseCart {
lineItems: LineItem[]
}
interface LineItem extends BaseLineItem {}

View File

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

View File

@ -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<Data> = ResponseState<Data> & { isEmpty?: boolean }
export type CartInput = {
cartId?: BaseCart['id']
cartId?: Cart['id']
}
export default function useCart<Data extends BaseCart | null>(
export default function useCart<Data extends Cart | null>(
options: HookFetcherOptions,
input: HookInput,
fetcherFn: HookFetcher<Data, CartInput>,

View File

@ -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 products 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 variants 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 variants price after all discounts are applied.
price: number
// Product variants 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<Product, 'id' | 'name' | 'prices'> & CartItem[]
subTotal: number | string
total: number | string
customerId: Customer['id']
}
interface CartItem extends Entity {
quantity: number
productId: Product['id']

108
framework/commerce/types.ts Normal file
View File

@ -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 products 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 variants 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 variants price after all discounts are applied.
price: number
// Product variants 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<UpdateLineItemBody> {
cartId?: string
}