mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
More updates
This commit is contained in:
@@ -1,18 +1,19 @@
|
||||
|
||||
import { normalizeCart } from '../lib/normalize'
|
||||
import type { HookFetcher } from '@commerce/utils/types'
|
||||
import type { SwrOptions } from '@commerce/utils/use-data'
|
||||
import useCommerceCart, { CartInput } from '@commerce/cart/use-cart'
|
||||
import type { Cart } from '../api/cart'
|
||||
import { normalizeCart } from '../lib/normalize'
|
||||
import update from 'immutability-helper'
|
||||
import type { Cart as BigCommerceCart } from '../api/cart'
|
||||
import update from "@framework/lib/immutability"
|
||||
|
||||
const defaultOpts = {
|
||||
url: '/api/bigcommerce/cart',
|
||||
method: 'GET',
|
||||
}
|
||||
|
||||
export type { Cart }
|
||||
type UseCartResponse = BigCommerceCart & Cart
|
||||
|
||||
export const fetcher: HookFetcher<Cart | null, CartInput> = (
|
||||
export const fetcher: HookFetcher<UseCartResponse | null , CartInput> = (
|
||||
options,
|
||||
{ cartId },
|
||||
fetch
|
||||
@@ -22,7 +23,7 @@ export const fetcher: HookFetcher<Cart | null, CartInput> = (
|
||||
|
||||
export function extendHook(
|
||||
customFetcher: typeof fetcher,
|
||||
swrOptions?: SwrOptions<Cart | null, CartInput>
|
||||
swrOptions?: SwrOptions<UseCartResponse | null, CartInput>
|
||||
) {
|
||||
const useCart = () => {
|
||||
const response = useCommerceCart(defaultOpts, [], customFetcher, {
|
||||
@@ -41,9 +42,11 @@ export function extendHook(
|
||||
set: (x) => x,
|
||||
})
|
||||
|
||||
return update(response, {
|
||||
data: { $set: normalizeCart(response.data) }
|
||||
})
|
||||
|
||||
|
||||
return response.data ? update(response, {
|
||||
data: { $set: normalizeCart(response.data ) }
|
||||
}) : response
|
||||
}
|
||||
|
||||
useCart.extend = extendHook
|
||||
|
18
framework/bigcommerce/lib/immutability.ts
Normal file
18
framework/bigcommerce/lib/immutability.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
import update, { Context } from 'immutability-helper';
|
||||
|
||||
const c = new Context();
|
||||
|
||||
c.extend('$auto', function(value, object) {
|
||||
return object ?
|
||||
c.update(object, value):
|
||||
c.update({}, value);
|
||||
});
|
||||
|
||||
c.extend('$autoArray', function(value, object) {
|
||||
return object ?
|
||||
c.update(object, value):
|
||||
c.update([], value);
|
||||
});
|
||||
|
||||
export default c.update
|
@@ -1,12 +1,5 @@
|
||||
import update from "@framework/lib/immutability"
|
||||
import { Cart, CartItem, Product } from '../../types'
|
||||
import update, { extend } from "immutability-helper"
|
||||
|
||||
// Allows auto creation when needed
|
||||
extend('$auto', function(value, object) {
|
||||
return object ?
|
||||
update(object, value):
|
||||
update({}, value);
|
||||
});
|
||||
|
||||
function normalizeProductOption(productOption:any) {
|
||||
const {
|
||||
@@ -78,11 +71,11 @@ export function normalizeProduct(productNode: any): Product {
|
||||
export function normalizeCart(data: any): Cart {
|
||||
return update(data, {
|
||||
$auto: {
|
||||
products: { $set: data?.line_items?.physical_items?.map(itemsToProducts)},
|
||||
subTotal: { $set: data.base_amount },
|
||||
total: { $set: data.cart_amount }
|
||||
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']
|
||||
$unset: ['created_time', 'coupons', 'line_items', 'email']
|
||||
})
|
||||
}
|
||||
|
||||
|
3
framework/types.d.ts
vendored
3
framework/types.d.ts
vendored
@@ -49,9 +49,10 @@ interface Cart extends Entity {
|
||||
id: string | undefined
|
||||
currency: { code: string }
|
||||
taxIncluded?: boolean
|
||||
products: Pick<Product, 'id' | 'name' | 'prices'> & CartItem[]
|
||||
items: Pick<Product, 'id' | 'name' | 'prices'> & CartItem[]
|
||||
subTotal: number | string
|
||||
total: number | string
|
||||
customerId: Customer['id']
|
||||
}
|
||||
|
||||
interface CartItem extends Entity {
|
||||
|
Reference in New Issue
Block a user