forked from crowetic/commerce
More updates
This commit is contained in:
parent
a8ae360cd4
commit
48b484011d
@ -12,6 +12,7 @@ import useRemoveItem from '@framework/cart/use-remove-item'
|
|||||||
const Item = ({
|
const Item = ({
|
||||||
item,
|
item,
|
||||||
currencyCode,
|
currencyCode,
|
||||||
|
...rest
|
||||||
}: {
|
}: {
|
||||||
item: CartItem
|
item: CartItem
|
||||||
currencyCode: string
|
currencyCode: string
|
||||||
@ -79,6 +80,7 @@ const Item = ({
|
|||||||
className={cn('flex flex-row space-x-8 py-8', {
|
className={cn('flex flex-row space-x-8 py-8', {
|
||||||
'opacity-75 pointer-events-none': removing,
|
'opacity-75 pointer-events-none': removing,
|
||||||
})}
|
})}
|
||||||
|
{...rest}
|
||||||
>
|
>
|
||||||
<div className="w-16 h-16 bg-violet relative overflow-hidden">
|
<div className="w-16 h-16 bg-violet relative overflow-hidden">
|
||||||
<Image
|
<Image
|
||||||
|
@ -15,14 +15,14 @@ const CartSidebarView: FC = () => {
|
|||||||
|
|
||||||
const { price: subTotal } = usePrice(
|
const { price: subTotal } = usePrice(
|
||||||
data && {
|
data && {
|
||||||
amount: data.subTotal,
|
amount: Number(data.subTotal),
|
||||||
currencyCode: data.currency?.code || 'USD',
|
currencyCode: data.currency?.code || 'USD',
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const { price: total } = usePrice(
|
const { price: total } = usePrice(
|
||||||
data && {
|
data && {
|
||||||
amount: data.total,
|
amount: Number(data.total),
|
||||||
currencyCode: data.currency?.code || 'USD',
|
currencyCode: data.currency?.code || 'USD',
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -94,7 +94,7 @@ const CartSidebarView: FC = () => {
|
|||||||
My Cart
|
My Cart
|
||||||
</h2>
|
</h2>
|
||||||
<ul className="py-6 space-y-6 sm:py-0 sm:space-y-0 sm:divide-y sm:divide-accents-3 border-t border-accents-3">
|
<ul className="py-6 space-y-6 sm:py-0 sm:space-y-0 sm:divide-y sm:divide-accents-3 border-t border-accents-3">
|
||||||
{data?.products?.map((item) => (
|
{data?.items?.map((item) => (
|
||||||
<CartItem
|
<CartItem
|
||||||
key={item.id}
|
key={item.id}
|
||||||
item={item}
|
item={item}
|
||||||
|
@ -20,7 +20,7 @@ const ProductCard: FC<Props> = ({
|
|||||||
...props
|
...props
|
||||||
}) => {
|
}) => {
|
||||||
return (
|
return (
|
||||||
<Link href={`product/${product.slug}`} {...props}>
|
<Link href={`/product/${product.slug}`} {...props}>
|
||||||
<a
|
<a
|
||||||
className={cn(s.root, { [s.simple]: variant === 'simple' }, className)}
|
className={cn(s.root, { [s.simple]: variant === 'simple' }, className)}
|
||||||
>
|
>
|
||||||
|
@ -1,18 +1,19 @@
|
|||||||
|
|
||||||
|
import { normalizeCart } from '../lib/normalize'
|
||||||
import type { HookFetcher } from '@commerce/utils/types'
|
import type { HookFetcher } from '@commerce/utils/types'
|
||||||
import type { SwrOptions } from '@commerce/utils/use-data'
|
import type { SwrOptions } from '@commerce/utils/use-data'
|
||||||
import useCommerceCart, { CartInput } from '@commerce/cart/use-cart'
|
import useCommerceCart, { CartInput } from '@commerce/cart/use-cart'
|
||||||
import type { Cart } from '../api/cart'
|
import type { Cart as BigCommerceCart } from '../api/cart'
|
||||||
import { normalizeCart } from '../lib/normalize'
|
import update from "@framework/lib/immutability"
|
||||||
import update from 'immutability-helper'
|
|
||||||
|
|
||||||
const defaultOpts = {
|
const defaultOpts = {
|
||||||
url: '/api/bigcommerce/cart',
|
url: '/api/bigcommerce/cart',
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
}
|
}
|
||||||
|
|
||||||
export type { Cart }
|
type UseCartResponse = BigCommerceCart & Cart
|
||||||
|
|
||||||
export const fetcher: HookFetcher<Cart | null, CartInput> = (
|
export const fetcher: HookFetcher<UseCartResponse | null , CartInput> = (
|
||||||
options,
|
options,
|
||||||
{ cartId },
|
{ cartId },
|
||||||
fetch
|
fetch
|
||||||
@ -22,7 +23,7 @@ export const fetcher: HookFetcher<Cart | null, CartInput> = (
|
|||||||
|
|
||||||
export function extendHook(
|
export function extendHook(
|
||||||
customFetcher: typeof fetcher,
|
customFetcher: typeof fetcher,
|
||||||
swrOptions?: SwrOptions<Cart | null, CartInput>
|
swrOptions?: SwrOptions<UseCartResponse | null, CartInput>
|
||||||
) {
|
) {
|
||||||
const useCart = () => {
|
const useCart = () => {
|
||||||
const response = useCommerceCart(defaultOpts, [], customFetcher, {
|
const response = useCommerceCart(defaultOpts, [], customFetcher, {
|
||||||
@ -41,9 +42,11 @@ export function extendHook(
|
|||||||
set: (x) => x,
|
set: (x) => x,
|
||||||
})
|
})
|
||||||
|
|
||||||
return update(response, {
|
|
||||||
|
|
||||||
|
return response.data ? update(response, {
|
||||||
data: { $set: normalizeCart(response.data ) }
|
data: { $set: normalizeCart(response.data ) }
|
||||||
})
|
}) : response
|
||||||
}
|
}
|
||||||
|
|
||||||
useCart.extend = extendHook
|
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 { 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) {
|
function normalizeProductOption(productOption:any) {
|
||||||
const {
|
const {
|
||||||
@ -78,11 +71,11 @@ export function normalizeProduct(productNode: any): Product {
|
|||||||
export function normalizeCart(data: any): Cart {
|
export function normalizeCart(data: any): Cart {
|
||||||
return update(data, {
|
return update(data, {
|
||||||
$auto: {
|
$auto: {
|
||||||
products: { $set: data?.line_items?.physical_items?.map(itemsToProducts)},
|
items: { $set: data?.line_items?.physical_items?.map(itemsToProducts)},
|
||||||
subTotal: { $set: data.base_amount },
|
subTotal: { $set: data?.base_amount },
|
||||||
total: { $set: data.cart_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
|
id: string | undefined
|
||||||
currency: { code: string }
|
currency: { code: string }
|
||||||
taxIncluded?: boolean
|
taxIncluded?: boolean
|
||||||
products: Pick<Product, 'id' | 'name' | 'prices'> & CartItem[]
|
items: Pick<Product, 'id' | 'name' | 'prices'> & CartItem[]
|
||||||
subTotal: number | string
|
subTotal: number | string
|
||||||
total: number | string
|
total: number | string
|
||||||
|
customerId: Customer['id']
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CartItem extends Entity {
|
interface CartItem extends Entity {
|
||||||
|
@ -8,6 +8,7 @@ import { Button } from '@components/ui'
|
|||||||
import { Bag, Cross, Check } from '@components/icons'
|
import { Bag, Cross, Check } from '@components/icons'
|
||||||
import { CartItem } from '@components/cart'
|
import { CartItem } from '@components/cart'
|
||||||
import { Text } from '@components/ui'
|
import { Text } from '@components/ui'
|
||||||
|
import products from '@framework/api/catalog/products'
|
||||||
|
|
||||||
export async function getStaticProps({
|
export async function getStaticProps({
|
||||||
preview,
|
preview,
|
||||||
@ -21,34 +22,23 @@ export async function getStaticProps({
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function Cart() {
|
export default function Cart() {
|
||||||
|
const error = null
|
||||||
|
const success = null
|
||||||
const { data, isEmpty } = useCart()
|
const { data, isEmpty } = useCart()
|
||||||
const loading = !data
|
|
||||||
|
|
||||||
if (loading) {
|
|
||||||
// Load skeleton
|
|
||||||
return <div>Loading</div>
|
|
||||||
}
|
|
||||||
|
|
||||||
const { price: subTotal } = usePrice(
|
const { price: subTotal } = usePrice(
|
||||||
data && {
|
data && {
|
||||||
amount: data.base_amount,
|
amount: Number(data.subTotal),
|
||||||
currencyCode: data.currency.code,
|
currencyCode: data.currency.code || 'USD',
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
const { price: total } = usePrice(
|
const { price: total } = usePrice(
|
||||||
data && {
|
data && {
|
||||||
amount: data.cart_amount,
|
amount: Number(data.total),
|
||||||
currencyCode: data.currency.code,
|
currencyCode: data.currency?.code || 'USD',
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// const items = data?.line_items.physical_items ?? []
|
|
||||||
|
|
||||||
// const error = null
|
|
||||||
// const success = null
|
|
||||||
|
|
||||||
return <div>hola</div>
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="grid lg:grid-cols-12">
|
<div className="grid lg:grid-cols-12">
|
||||||
<div className="lg:col-span-8">
|
<div className="lg:col-span-8">
|
||||||
@ -88,7 +78,7 @@ export default function Cart() {
|
|||||||
<Text variant="pageHeading">My Cart</Text>
|
<Text variant="pageHeading">My Cart</Text>
|
||||||
<Text variant="sectionHeading">Review your Order</Text>
|
<Text variant="sectionHeading">Review your Order</Text>
|
||||||
<ul className="py-6 space-y-6 sm:py-0 sm:space-y-0 sm:divide-y sm:divide-accents-2 border-b border-accents-2">
|
<ul className="py-6 space-y-6 sm:py-0 sm:space-y-0 sm:divide-y sm:divide-accents-2 border-b border-accents-2">
|
||||||
{items.map((item) => (
|
{data?.items.map((item) => (
|
||||||
<CartItem
|
<CartItem
|
||||||
key={item.id}
|
key={item.id}
|
||||||
item={item}
|
item={item}
|
||||||
|
@ -110,9 +110,9 @@ export default function Search({
|
|||||||
fill="currentColor"
|
fill="currentColor"
|
||||||
>
|
>
|
||||||
<path
|
<path
|
||||||
fill-rule="evenodd"
|
fillRule="evenodd"
|
||||||
d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
|
d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
|
||||||
clip-rule="evenodd"
|
clipRule="evenodd"
|
||||||
/>
|
/>
|
||||||
</svg>
|
</svg>
|
||||||
</button>
|
</button>
|
||||||
@ -209,9 +209,9 @@ export default function Search({
|
|||||||
fill="currentColor"
|
fill="currentColor"
|
||||||
>
|
>
|
||||||
<path
|
<path
|
||||||
fill-rule="evenodd"
|
fillRule="evenodd"
|
||||||
d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
|
d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
|
||||||
clip-rule="evenodd"
|
clipRule="evenodd"
|
||||||
/>
|
/>
|
||||||
</svg>
|
</svg>
|
||||||
</button>
|
</button>
|
||||||
@ -384,9 +384,9 @@ export default function Search({
|
|||||||
fill="currentColor"
|
fill="currentColor"
|
||||||
>
|
>
|
||||||
<path
|
<path
|
||||||
fill-rule="evenodd"
|
fillRule="evenodd"
|
||||||
d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
|
d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
|
||||||
clip-rule="evenodd"
|
clipRule="evenodd"
|
||||||
/>
|
/>
|
||||||
</svg>
|
</svg>
|
||||||
</button>
|
</button>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user