@hanzo/commerce 7.3.1 → 7.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/Icons.tsx +34 -34
- package/components/add-to-cart-widget.tsx +183 -183
- package/components/buy/buy-card.tsx +259 -259
- package/components/buy/carousel-buy-card.tsx +242 -242
- package/components/buy/multi-family/all-variants-carousel.tsx +261 -261
- package/components/buy/multi-family/family-carousel/index.tsx +77 -77
- package/components/buy/multi-family/family-carousel/slide.tsx +83 -83
- package/components/buy/multi-family/family-carousel/state.ts +87 -87
- package/components/buy/multi-family/index.ts +2 -2
- package/components/buy/single-family-selector.tsx +90 -90
- package/components/buy/title-and-byline.tsx +25 -25
- package/components/cart/cart-panel/cart-line-item.tsx +76 -76
- package/components/cart/cart-panel/index.tsx +154 -154
- package/components/cart/cart-panel/promo-code.tsx +106 -106
- package/components/cart/cart-panel/total-area.tsx +60 -60
- package/components/checkout/payment-step-form/card-icon-row.tsx +26 -26
- package/components/checkout/payment-step-form/card-icons/amex.tsx +32 -32
- package/components/checkout/payment-step-form/card-icons/diners-club.tsx +13 -13
- package/components/checkout/payment-step-form/card-icons/discover.tsx +25 -25
- package/components/checkout/payment-step-form/card-icons/jcb.tsx +26 -26
- package/components/checkout/payment-step-form/card-icons/mastercard.tsx +27 -27
- package/components/checkout/payment-step-form/card-icons/visa.tsx +25 -25
- package/components/checkout/payment-step-form/cc-button.tsx +17 -17
- package/components/checkout/payment-step-form/contact-form.tsx +49 -49
- package/components/checkout/payment-step-form/crypto-icons/btc.tsx +11 -11
- package/components/checkout/payment-step-form/crypto-icons/eth.tsx +20 -20
- package/components/checkout/payment-step-form/crypto-icons/usdt.tsx +13 -13
- package/components/checkout/payment-step-form/index.tsx +122 -122
- package/components/checkout/payment-step-form/methods/bank-transfer.tsx +79 -79
- package/components/checkout/payment-step-form/methods/card.tsx +232 -232
- package/components/checkout/payment-step-form/methods/crypto.tsx +227 -227
- package/components/checkout/payment-step-form/methods/index.ts +22 -22
- package/components/checkout/shipping-step-form.tsx +172 -172
- package/components/index.ts +11 -11
- package/components/item/product-card.tsx +48 -48
- package/components/item-selector/button.tsx +188 -188
- package/components/item-selector/carousel/index.tsx +197 -197
- package/components/item-selector/carousel/slider.tsx +40 -40
- package/components/item-selector/index.ts +5 -5
- package/components/item-selector/quantity-indicator.tsx +48 -48
- package/components/node-tabs/index.tsx +91 -91
- package/components/node-tabs/node-image.tsx +31 -31
- package/index.ts +12 -12
- package/package.json +60 -60
- package/service/context.tsx +45 -45
- package/service/debug.ts +41 -41
- package/service/impls/standalone/actual-line-item.ts +135 -135
- package/service/impls/standalone/get-instance.ts +64 -64
- package/service/impls/standalone/index.ts +579 -579
- package/service/impls/standalone/order/firebase.ts +14 -14
- package/service/impls/standalone/order/index.ts +128 -128
- package/service/impls/standalone/persistence.ts +33 -33
- package/service/path-utils.ts +25 -25
- package/service/sep.ts +6 -6
- package/tsconfig.json +10 -10
- package/types/README.md +1 -1
- package/types/category-node.ts +49 -49
- package/types/checkout.ts +46 -46
- package/types/commerce-config.ts +13 -13
- package/types/commerce-service.ts +127 -127
- package/types/family.ts +25 -25
- package/types/index.ts +15 -15
- package/types/item-selector.ts +96 -96
- package/types/line-item.ts +29 -29
- package/types/multi-family-selector-props.ts +19 -19
- package/types/product.ts +20 -20
- package/types/promo.ts +9 -9
- package/types/selection-ui-specifier.ts +51 -51
- package/types/string-mutator.ts +13 -13
- package/types/token-separators.ts +7 -7
- package/util/analytics.ts +20 -20
- package/util/countries.ts +195 -195
- package/util/error.ts +34 -34
- package/util/index.ts +70 -70
- package/util/item-selector-options-accessor.ts +34 -34
- package/util/line-item-ref.ts +23 -23
- package/util/multi-family-selector-options-accessor.ts +14 -14
- package/util/obs-string-mutator.ts +22 -22
- package/util/product-media-accessor.ts +58 -58
- package/util/promo-codes.ts +108 -108
- package/util/selection-ui-specifiers.ts +29 -29
- package/util/square-payment.ts +42 -42
- package/util/use-sync-sku-param-w-current-item.ts +88 -88
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
|
|
3
|
-
import React from 'react'
|
|
4
|
-
import { observer } from 'mobx-react-lite'
|
|
5
|
-
|
|
6
|
-
import { cn } from '@hanzo/ui/util'
|
|
7
|
-
import { Image } from '@hanzo/ui/primitives'
|
|
8
|
-
|
|
9
|
-
import type { LineItem } from '../../../types'
|
|
10
|
-
import { formatCurrencyValue } from '../../../util'
|
|
11
|
-
import AddToCartWidget from '../../add-to-cart-widget'
|
|
12
|
-
import { useCommerce } from '../../../service/context'
|
|
13
|
-
|
|
14
|
-
const DEF_IMG_SIZE=40
|
|
15
|
-
|
|
16
|
-
const renderTitle = (t: string): React.ReactNode => {
|
|
17
|
-
const toks = t.split(', ')
|
|
18
|
-
if (toks.length === 2) {
|
|
19
|
-
return <><p>{toks[0]},</p><p className=''>{toks[1]}</p></>
|
|
20
|
-
}
|
|
21
|
-
return <p>{t}</p>
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const CartLineItem: React.FC<{
|
|
25
|
-
item: LineItem
|
|
26
|
-
className?: string
|
|
27
|
-
imgSizePx?: number
|
|
28
|
-
showPromoCode?: boolean
|
|
29
|
-
itemClicked?: (item: LineItem) => void
|
|
30
|
-
selected?: boolean
|
|
31
|
-
}> = observer(({
|
|
32
|
-
item,
|
|
33
|
-
className='',
|
|
34
|
-
imgSizePx=DEF_IMG_SIZE,
|
|
35
|
-
showPromoCode=false,
|
|
36
|
-
itemClicked,
|
|
37
|
-
selected=false
|
|
38
|
-
}) => {
|
|
39
|
-
|
|
40
|
-
const cmmc = useCommerce()
|
|
41
|
-
const promoPrice = showPromoCode ? cmmc.itemPromoPrice(item) : undefined
|
|
42
|
-
|
|
43
|
-
return (
|
|
44
|
-
<div
|
|
45
|
-
className={cn(
|
|
46
|
-
'flex flex-col justify-start items-start text-sm font-sans',
|
|
47
|
-
'border border-background rounded-sm p-1',
|
|
48
|
-
selected ? 'border-foreground' : '',
|
|
49
|
-
(itemClicked && !selected ? 'cursor-pointer hover:!border-muted-4' : 'cursor-default '),
|
|
50
|
-
className
|
|
51
|
-
)}
|
|
52
|
-
onClick={() => {itemClicked && itemClicked(item)}}
|
|
53
|
-
>
|
|
54
|
-
<div className='flex flex-row justify-between items-center gap-1' >
|
|
55
|
-
{item.img ? (
|
|
56
|
-
<Image def={item.img} constrainTo={{w: imgSizePx, h: imgSizePx}} />
|
|
57
|
-
) : ( // placeholder so things align
|
|
58
|
-
<div style={{height: imgSizePx, width: imgSizePx}} className='bg-level-3'/>
|
|
59
|
-
)}
|
|
60
|
-
<div className='grow leading-tight ml-1'>{renderTitle(item.title)}</div>
|
|
61
|
-
</div>
|
|
62
|
-
<div className='flex flex-row items-center justify-between w-full'>
|
|
63
|
-
<div className='flex flex-row items-center'>
|
|
64
|
-
<AddToCartWidget variant='minimal' item={item} />
|
|
65
|
-
{item.quantity > 1 && (<span className='pl-2.5'>{'@' + formatCurrencyValue(item.price)}</span>)}
|
|
66
|
-
</div>
|
|
67
|
-
<div className='flex flex-row gap-1 items-center justify-end'>
|
|
68
|
-
<span className={promoPrice ? 'line-through text-muted-2' : ''}>{formatCurrencyValue(item.price * item.quantity)}</span>
|
|
69
|
-
{promoPrice && <span>{formatCurrencyValue(promoPrice * item.quantity)}</span>}
|
|
70
|
-
</div>
|
|
71
|
-
</div>
|
|
72
|
-
</div>
|
|
73
|
-
)
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
export default CartLineItem
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import React from 'react'
|
|
4
|
+
import { observer } from 'mobx-react-lite'
|
|
5
|
+
|
|
6
|
+
import { cn } from '@hanzo/ui/util'
|
|
7
|
+
import { Image } from '@hanzo/ui/primitives'
|
|
8
|
+
|
|
9
|
+
import type { LineItem } from '../../../types'
|
|
10
|
+
import { formatCurrencyValue } from '../../../util'
|
|
11
|
+
import AddToCartWidget from '../../add-to-cart-widget'
|
|
12
|
+
import { useCommerce } from '../../../service/context'
|
|
13
|
+
|
|
14
|
+
const DEF_IMG_SIZE=40
|
|
15
|
+
|
|
16
|
+
const renderTitle = (t: string): React.ReactNode => {
|
|
17
|
+
const toks = t.split(', ')
|
|
18
|
+
if (toks.length === 2) {
|
|
19
|
+
return <><p>{toks[0]},</p><p className=''>{toks[1]}</p></>
|
|
20
|
+
}
|
|
21
|
+
return <p>{t}</p>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const CartLineItem: React.FC<{
|
|
25
|
+
item: LineItem
|
|
26
|
+
className?: string
|
|
27
|
+
imgSizePx?: number
|
|
28
|
+
showPromoCode?: boolean
|
|
29
|
+
itemClicked?: (item: LineItem) => void
|
|
30
|
+
selected?: boolean
|
|
31
|
+
}> = observer(({
|
|
32
|
+
item,
|
|
33
|
+
className='',
|
|
34
|
+
imgSizePx=DEF_IMG_SIZE,
|
|
35
|
+
showPromoCode=false,
|
|
36
|
+
itemClicked,
|
|
37
|
+
selected=false
|
|
38
|
+
}) => {
|
|
39
|
+
|
|
40
|
+
const cmmc = useCommerce()
|
|
41
|
+
const promoPrice = showPromoCode ? cmmc.itemPromoPrice(item) : undefined
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<div
|
|
45
|
+
className={cn(
|
|
46
|
+
'flex flex-col justify-start items-start text-sm font-sans',
|
|
47
|
+
'border border-background rounded-sm p-1',
|
|
48
|
+
selected ? 'border-foreground' : '',
|
|
49
|
+
(itemClicked && !selected ? 'cursor-pointer hover:!border-muted-4' : 'cursor-default '),
|
|
50
|
+
className
|
|
51
|
+
)}
|
|
52
|
+
onClick={() => {itemClicked && itemClicked(item)}}
|
|
53
|
+
>
|
|
54
|
+
<div className='flex flex-row justify-between items-center gap-1' >
|
|
55
|
+
{item.img ? (
|
|
56
|
+
<Image def={item.img} constrainTo={{w: imgSizePx, h: imgSizePx}} />
|
|
57
|
+
) : ( // placeholder so things align
|
|
58
|
+
<div style={{height: imgSizePx, width: imgSizePx}} className='bg-level-3'/>
|
|
59
|
+
)}
|
|
60
|
+
<div className='grow leading-tight ml-1'>{renderTitle(item.title)}</div>
|
|
61
|
+
</div>
|
|
62
|
+
<div className='flex flex-row items-center justify-between w-full'>
|
|
63
|
+
<div className='flex flex-row items-center'>
|
|
64
|
+
<AddToCartWidget variant='minimal' item={item} />
|
|
65
|
+
{item.quantity > 1 && (<span className='pl-2.5'>{'@' + formatCurrencyValue(item.price)}</span>)}
|
|
66
|
+
</div>
|
|
67
|
+
<div className='flex flex-row gap-1 items-center justify-end'>
|
|
68
|
+
<span className={promoPrice ? 'line-through text-muted-2' : ''}>{formatCurrencyValue(item.price * item.quantity)}</span>
|
|
69
|
+
{promoPrice && <span>{formatCurrencyValue(promoPrice * item.quantity)}</span>}
|
|
70
|
+
</div>
|
|
71
|
+
</div>
|
|
72
|
+
</div>
|
|
73
|
+
)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
export default CartLineItem
|
|
@@ -1,154 +1,154 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
import React, { useLayoutEffect, type PropsWithChildren } from 'react'
|
|
3
|
-
import { reaction } from 'mobx'
|
|
4
|
-
import { observer } from 'mobx-react-lite'
|
|
5
|
-
|
|
6
|
-
import { Button, ScrollArea } from '@hanzo/ui/primitives'
|
|
7
|
-
import { cn } from '@hanzo/ui/util'
|
|
8
|
-
|
|
9
|
-
import type { LineItem } from '../../../types'
|
|
10
|
-
import { useCommerce } from '../../../service/context'
|
|
11
|
-
import { sendFBEvent, sendGAEvent } from '../../../util/analytics'
|
|
12
|
-
|
|
13
|
-
import CartLineItem from './cart-line-item'
|
|
14
|
-
import TotalArea from './total-area'
|
|
15
|
-
|
|
16
|
-
const CartPanel: React.FC<PropsWithChildren & {
|
|
17
|
-
/** fix size and scroll after 'scrollAfter items in teh cart. */
|
|
18
|
-
scrollAfter?: number
|
|
19
|
-
scrollHeightClx?: string
|
|
20
|
-
imgSizePx?: number
|
|
21
|
-
className?: string
|
|
22
|
-
itemClx?: string
|
|
23
|
-
listClx?: string
|
|
24
|
-
noItemsClx?: string
|
|
25
|
-
totalClx?: string
|
|
26
|
-
buttonClx?: string
|
|
27
|
-
showPromoCode?: boolean
|
|
28
|
-
showShipping?: boolean
|
|
29
|
-
selectItems?: boolean
|
|
30
|
-
/** if not provided, 'checkout' button will be rendered */
|
|
31
|
-
handleCheckout?: () => void
|
|
32
|
-
}> = observer(({
|
|
33
|
-
/** If provided, 'children' is rendered above the items. eg, a heading. */
|
|
34
|
-
children,
|
|
35
|
-
scrollAfter=5,
|
|
36
|
-
scrollHeightClx='h-[70vh]',
|
|
37
|
-
imgSizePx=40,
|
|
38
|
-
className='',
|
|
39
|
-
itemClx='',
|
|
40
|
-
listClx='',
|
|
41
|
-
noItemsClx='',
|
|
42
|
-
totalClx='',
|
|
43
|
-
buttonClx='',
|
|
44
|
-
showPromoCode=false,
|
|
45
|
-
showShipping=false,
|
|
46
|
-
selectItems=false,
|
|
47
|
-
handleCheckout,
|
|
48
|
-
}) => {
|
|
49
|
-
|
|
50
|
-
const cmmc = useCommerce()
|
|
51
|
-
|
|
52
|
-
useLayoutEffect(() => {
|
|
53
|
-
if (!cmmc) {
|
|
54
|
-
return
|
|
55
|
-
}
|
|
56
|
-
if (!cmmc.cartEmpty && !cmmc.currentItem) {
|
|
57
|
-
cmmc.setCurrentItem(cmmc.cartItems[0].sku)
|
|
58
|
-
}
|
|
59
|
-
// return mobx disposer
|
|
60
|
-
return reaction(() => (
|
|
61
|
-
cmmc.cartItems.length
|
|
62
|
-
),
|
|
63
|
-
(itemCount) => {
|
|
64
|
-
if (itemCount > 0) {
|
|
65
|
-
cmmc.setCurrentItem(cmmc.cartItems[0].sku)
|
|
66
|
-
}
|
|
67
|
-
})
|
|
68
|
-
}, [])
|
|
69
|
-
|
|
70
|
-
if (!cmmc) {
|
|
71
|
-
return <div />
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const _handleCheckout = () => {
|
|
75
|
-
sendGAEvent('begin_checkout', {
|
|
76
|
-
currency: 'USD',
|
|
77
|
-
value: cmmc.cartTotal,
|
|
78
|
-
items: cmmc.cartItems.map((item) => ({
|
|
79
|
-
item_id: item.sku,
|
|
80
|
-
item_name: item.title,
|
|
81
|
-
item_category: item.familyId,
|
|
82
|
-
price: item.price,
|
|
83
|
-
quantity: item.quantity
|
|
84
|
-
})),
|
|
85
|
-
})
|
|
86
|
-
sendFBEvent('InitiateCheckout', {
|
|
87
|
-
content_ids: cmmc.cartItems.map((item) => item.sku),
|
|
88
|
-
contents: cmmc.cartItems.map(item => ({
|
|
89
|
-
id: item.sku,
|
|
90
|
-
quantity: item.quantity
|
|
91
|
-
})),
|
|
92
|
-
num_items: cmmc.cartItems.length,
|
|
93
|
-
value: cmmc.cartTotal,
|
|
94
|
-
currency: 'USD',
|
|
95
|
-
})
|
|
96
|
-
handleCheckout && handleCheckout()
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const itemClicked = (item: LineItem) => {
|
|
100
|
-
cmmc.setCurrentItem(item.sku)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const MainStuff: React.FC = observer(() => (<>
|
|
104
|
-
{cmmc.cartEmpty ? (
|
|
105
|
-
<p className={cn('text-center my-3', noItemsClx)}>No items</p>
|
|
106
|
-
) : (<>
|
|
107
|
-
{cmmc.cartItems.map((item) => (
|
|
108
|
-
<CartLineItem
|
|
109
|
-
key={`mobile-${item.sku}`}
|
|
110
|
-
imgSizePx={imgSizePx}
|
|
111
|
-
item={item}
|
|
112
|
-
className={cn('mb-2', itemClx)}
|
|
113
|
-
showPromoCode={showPromoCode}
|
|
114
|
-
itemClicked={selectItems ? itemClicked : undefined}
|
|
115
|
-
selected={selectItems ? cmmc.currentItem?.sku === item.sku : false}
|
|
116
|
-
/>
|
|
117
|
-
))}
|
|
118
|
-
</>)}
|
|
119
|
-
<TotalArea clx={totalClx} showPromoCode={showPromoCode} showShipping={showShipping}/>
|
|
120
|
-
</>))
|
|
121
|
-
|
|
122
|
-
const scrolling = (cmmc.cartItems.length > scrollAfter)
|
|
123
|
-
|
|
124
|
-
return (
|
|
125
|
-
<div className={cn(
|
|
126
|
-
'border p-4 rounded-lg flex flex-col',
|
|
127
|
-
className,
|
|
128
|
-
scrolling ? scrollHeightClx : 'h-auto'
|
|
129
|
-
)}>
|
|
130
|
-
{children}
|
|
131
|
-
{scrolling ? (
|
|
132
|
-
<ScrollArea className={cn('mt-2 w-full shrink py-0', listClx)}>
|
|
133
|
-
<MainStuff />
|
|
134
|
-
</ScrollArea>
|
|
135
|
-
) : (
|
|
136
|
-
<div className={cn('mt-2 w-full', listClx)}>
|
|
137
|
-
<MainStuff />
|
|
138
|
-
</div>
|
|
139
|
-
)}
|
|
140
|
-
{handleCheckout && !cmmc.cartEmpty && (
|
|
141
|
-
<Button
|
|
142
|
-
onClick={_handleCheckout}
|
|
143
|
-
variant='primary'
|
|
144
|
-
rounded='lg'
|
|
145
|
-
className={cn('mt-12 mx-auto w-full sm:max-w-[220px]', buttonClx)}
|
|
146
|
-
>
|
|
147
|
-
Checkout
|
|
148
|
-
</Button>
|
|
149
|
-
)}
|
|
150
|
-
</div>
|
|
151
|
-
)
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
export default CartPanel
|
|
1
|
+
'use client'
|
|
2
|
+
import React, { useLayoutEffect, type PropsWithChildren } from 'react'
|
|
3
|
+
import { reaction } from 'mobx'
|
|
4
|
+
import { observer } from 'mobx-react-lite'
|
|
5
|
+
|
|
6
|
+
import { Button, ScrollArea } from '@hanzo/ui/primitives'
|
|
7
|
+
import { cn } from '@hanzo/ui/util'
|
|
8
|
+
|
|
9
|
+
import type { LineItem } from '../../../types'
|
|
10
|
+
import { useCommerce } from '../../../service/context'
|
|
11
|
+
import { sendFBEvent, sendGAEvent } from '../../../util/analytics'
|
|
12
|
+
|
|
13
|
+
import CartLineItem from './cart-line-item'
|
|
14
|
+
import TotalArea from './total-area'
|
|
15
|
+
|
|
16
|
+
const CartPanel: React.FC<PropsWithChildren & {
|
|
17
|
+
/** fix size and scroll after 'scrollAfter items in teh cart. */
|
|
18
|
+
scrollAfter?: number
|
|
19
|
+
scrollHeightClx?: string
|
|
20
|
+
imgSizePx?: number
|
|
21
|
+
className?: string
|
|
22
|
+
itemClx?: string
|
|
23
|
+
listClx?: string
|
|
24
|
+
noItemsClx?: string
|
|
25
|
+
totalClx?: string
|
|
26
|
+
buttonClx?: string
|
|
27
|
+
showPromoCode?: boolean
|
|
28
|
+
showShipping?: boolean
|
|
29
|
+
selectItems?: boolean
|
|
30
|
+
/** if not provided, 'checkout' button will be rendered */
|
|
31
|
+
handleCheckout?: () => void
|
|
32
|
+
}> = observer(({
|
|
33
|
+
/** If provided, 'children' is rendered above the items. eg, a heading. */
|
|
34
|
+
children,
|
|
35
|
+
scrollAfter=5,
|
|
36
|
+
scrollHeightClx='h-[70vh]',
|
|
37
|
+
imgSizePx=40,
|
|
38
|
+
className='',
|
|
39
|
+
itemClx='',
|
|
40
|
+
listClx='',
|
|
41
|
+
noItemsClx='',
|
|
42
|
+
totalClx='',
|
|
43
|
+
buttonClx='',
|
|
44
|
+
showPromoCode=false,
|
|
45
|
+
showShipping=false,
|
|
46
|
+
selectItems=false,
|
|
47
|
+
handleCheckout,
|
|
48
|
+
}) => {
|
|
49
|
+
|
|
50
|
+
const cmmc = useCommerce()
|
|
51
|
+
|
|
52
|
+
useLayoutEffect(() => {
|
|
53
|
+
if (!cmmc) {
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
if (!cmmc.cartEmpty && !cmmc.currentItem) {
|
|
57
|
+
cmmc.setCurrentItem(cmmc.cartItems[0].sku)
|
|
58
|
+
}
|
|
59
|
+
// return mobx disposer
|
|
60
|
+
return reaction(() => (
|
|
61
|
+
cmmc.cartItems.length
|
|
62
|
+
),
|
|
63
|
+
(itemCount) => {
|
|
64
|
+
if (itemCount > 0) {
|
|
65
|
+
cmmc.setCurrentItem(cmmc.cartItems[0].sku)
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
}, [])
|
|
69
|
+
|
|
70
|
+
if (!cmmc) {
|
|
71
|
+
return <div />
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const _handleCheckout = () => {
|
|
75
|
+
sendGAEvent('begin_checkout', {
|
|
76
|
+
currency: 'USD',
|
|
77
|
+
value: cmmc.cartTotal,
|
|
78
|
+
items: cmmc.cartItems.map((item) => ({
|
|
79
|
+
item_id: item.sku,
|
|
80
|
+
item_name: item.title,
|
|
81
|
+
item_category: item.familyId,
|
|
82
|
+
price: item.price,
|
|
83
|
+
quantity: item.quantity
|
|
84
|
+
})),
|
|
85
|
+
})
|
|
86
|
+
sendFBEvent('InitiateCheckout', {
|
|
87
|
+
content_ids: cmmc.cartItems.map((item) => item.sku),
|
|
88
|
+
contents: cmmc.cartItems.map(item => ({
|
|
89
|
+
id: item.sku,
|
|
90
|
+
quantity: item.quantity
|
|
91
|
+
})),
|
|
92
|
+
num_items: cmmc.cartItems.length,
|
|
93
|
+
value: cmmc.cartTotal,
|
|
94
|
+
currency: 'USD',
|
|
95
|
+
})
|
|
96
|
+
handleCheckout && handleCheckout()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const itemClicked = (item: LineItem) => {
|
|
100
|
+
cmmc.setCurrentItem(item.sku)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const MainStuff: React.FC = observer(() => (<>
|
|
104
|
+
{cmmc.cartEmpty ? (
|
|
105
|
+
<p className={cn('text-center my-3', noItemsClx)}>No items</p>
|
|
106
|
+
) : (<>
|
|
107
|
+
{cmmc.cartItems.map((item) => (
|
|
108
|
+
<CartLineItem
|
|
109
|
+
key={`mobile-${item.sku}`}
|
|
110
|
+
imgSizePx={imgSizePx}
|
|
111
|
+
item={item}
|
|
112
|
+
className={cn('mb-2', itemClx)}
|
|
113
|
+
showPromoCode={showPromoCode}
|
|
114
|
+
itemClicked={selectItems ? itemClicked : undefined}
|
|
115
|
+
selected={selectItems ? cmmc.currentItem?.sku === item.sku : false}
|
|
116
|
+
/>
|
|
117
|
+
))}
|
|
118
|
+
</>)}
|
|
119
|
+
<TotalArea clx={totalClx} showPromoCode={showPromoCode} showShipping={showShipping}/>
|
|
120
|
+
</>))
|
|
121
|
+
|
|
122
|
+
const scrolling = (cmmc.cartItems.length > scrollAfter)
|
|
123
|
+
|
|
124
|
+
return (
|
|
125
|
+
<div className={cn(
|
|
126
|
+
'border p-4 rounded-lg flex flex-col',
|
|
127
|
+
className,
|
|
128
|
+
scrolling ? scrollHeightClx : 'h-auto'
|
|
129
|
+
)}>
|
|
130
|
+
{children}
|
|
131
|
+
{scrolling ? (
|
|
132
|
+
<ScrollArea className={cn('mt-2 w-full shrink py-0', listClx)}>
|
|
133
|
+
<MainStuff />
|
|
134
|
+
</ScrollArea>
|
|
135
|
+
) : (
|
|
136
|
+
<div className={cn('mt-2 w-full', listClx)}>
|
|
137
|
+
<MainStuff />
|
|
138
|
+
</div>
|
|
139
|
+
)}
|
|
140
|
+
{handleCheckout && !cmmc.cartEmpty && (
|
|
141
|
+
<Button
|
|
142
|
+
onClick={_handleCheckout}
|
|
143
|
+
variant='primary'
|
|
144
|
+
rounded='lg'
|
|
145
|
+
className={cn('mt-12 mx-auto w-full sm:max-w-[220px]', buttonClx)}
|
|
146
|
+
>
|
|
147
|
+
Checkout
|
|
148
|
+
</Button>
|
|
149
|
+
)}
|
|
150
|
+
</div>
|
|
151
|
+
)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
export default CartPanel
|
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
|
|
3
|
-
import { useEffect, useState } from 'react'
|
|
4
|
-
import { useSearchParams } from 'next/navigation'
|
|
5
|
-
import { observer } from 'mobx-react-lite'
|
|
6
|
-
|
|
7
|
-
import { useForm } from 'react-hook-form'
|
|
8
|
-
import * as z from 'zod'
|
|
9
|
-
import { zodResolver } from '@hookform/resolvers/zod'
|
|
10
|
-
import { Check } from 'lucide-react'
|
|
11
|
-
|
|
12
|
-
import {
|
|
13
|
-
Button,
|
|
14
|
-
Form,
|
|
15
|
-
FormControl,
|
|
16
|
-
FormField,
|
|
17
|
-
FormItem,
|
|
18
|
-
Input
|
|
19
|
-
} from '@hanzo/ui/primitives'
|
|
20
|
-
|
|
21
|
-
import { useCommerce } from '../../../service/context'
|
|
22
|
-
import type { Promo } from '../../../types'
|
|
23
|
-
import getPromoFromApi from '../../../util/promo-codes'
|
|
24
|
-
|
|
25
|
-
const formSchema = z.object({
|
|
26
|
-
code: z.string().min(1, 'Invalid code'),
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
const PromoCode = observer(() => {
|
|
30
|
-
const cmmc = useCommerce()
|
|
31
|
-
const searchParams = useSearchParams()
|
|
32
|
-
|
|
33
|
-
const [codeAccepted, setCodeAccepted] = useState<boolean>(false)
|
|
34
|
-
|
|
35
|
-
useEffect(() => {
|
|
36
|
-
const code = searchParams.get('code')
|
|
37
|
-
if (code) {
|
|
38
|
-
getPromoFromApi(code).then((promo?: Promo) => {
|
|
39
|
-
if (promo) {
|
|
40
|
-
form.setValue('code', code)
|
|
41
|
-
setCodeAccepted(true)
|
|
42
|
-
cmmc.setAppliedPromo(promo)
|
|
43
|
-
}
|
|
44
|
-
})
|
|
45
|
-
}
|
|
46
|
-
}, [searchParams])
|
|
47
|
-
|
|
48
|
-
useEffect(() => {
|
|
49
|
-
if (cmmc.appliedPromo) {
|
|
50
|
-
form.setValue('code', cmmc.appliedPromo.code)
|
|
51
|
-
setCodeAccepted(true)
|
|
52
|
-
}
|
|
53
|
-
}, [cmmc.appliedPromo])
|
|
54
|
-
|
|
55
|
-
const form = useForm<z.infer<typeof formSchema>>({
|
|
56
|
-
resolver: zodResolver(formSchema),
|
|
57
|
-
defaultValues: {
|
|
58
|
-
code: '',
|
|
59
|
-
},
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
const applyPromoCode = async (values: z.infer<typeof formSchema>) => {
|
|
63
|
-
const { code } = values
|
|
64
|
-
const promo = await getPromoFromApi(code)
|
|
65
|
-
if (!promo) {
|
|
66
|
-
form.setError('code', { message: 'Invalid code' })
|
|
67
|
-
return
|
|
68
|
-
}
|
|
69
|
-
setCodeAccepted(true)
|
|
70
|
-
cmmc.setAppliedPromo(promo)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const removePromoCode = () => {
|
|
74
|
-
cmmc.setAppliedPromo(null)
|
|
75
|
-
setCodeAccepted(false)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return (
|
|
79
|
-
<div className='flex flex-col gap-2 border-t py-1'>
|
|
80
|
-
<Form {...form}>
|
|
81
|
-
<form onSubmit={form.handleSubmit(applyPromoCode)}>
|
|
82
|
-
<div className='flex gap-2 items-center'>
|
|
83
|
-
<FormField
|
|
84
|
-
control={form.control}
|
|
85
|
-
name='code'
|
|
86
|
-
render={({ field }) => (
|
|
87
|
-
<FormItem className='w-full'>
|
|
88
|
-
<FormControl>
|
|
89
|
-
<Input {...field} className='h-9' placeholder='Discount or invite code' onInput={removePromoCode}/>
|
|
90
|
-
</FormControl>
|
|
91
|
-
</FormItem>
|
|
92
|
-
)}
|
|
93
|
-
/>
|
|
94
|
-
{codeAccepted ? (
|
|
95
|
-
<Check/>
|
|
96
|
-
) : (
|
|
97
|
-
<Button variant='outline' className='!w-fit !min-w-0 font-inter text-muted text-sm'>Apply</Button>
|
|
98
|
-
)}
|
|
99
|
-
</div>
|
|
100
|
-
</form>
|
|
101
|
-
</Form>
|
|
102
|
-
</div>
|
|
103
|
-
)
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
export default PromoCode
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState } from 'react'
|
|
4
|
+
import { useSearchParams } from 'next/navigation'
|
|
5
|
+
import { observer } from 'mobx-react-lite'
|
|
6
|
+
|
|
7
|
+
import { useForm } from 'react-hook-form'
|
|
8
|
+
import * as z from 'zod'
|
|
9
|
+
import { zodResolver } from '@hookform/resolvers/zod'
|
|
10
|
+
import { Check } from 'lucide-react'
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
Button,
|
|
14
|
+
Form,
|
|
15
|
+
FormControl,
|
|
16
|
+
FormField,
|
|
17
|
+
FormItem,
|
|
18
|
+
Input
|
|
19
|
+
} from '@hanzo/ui/primitives'
|
|
20
|
+
|
|
21
|
+
import { useCommerce } from '../../../service/context'
|
|
22
|
+
import type { Promo } from '../../../types'
|
|
23
|
+
import getPromoFromApi from '../../../util/promo-codes'
|
|
24
|
+
|
|
25
|
+
const formSchema = z.object({
|
|
26
|
+
code: z.string().min(1, 'Invalid code'),
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const PromoCode = observer(() => {
|
|
30
|
+
const cmmc = useCommerce()
|
|
31
|
+
const searchParams = useSearchParams()
|
|
32
|
+
|
|
33
|
+
const [codeAccepted, setCodeAccepted] = useState<boolean>(false)
|
|
34
|
+
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
const code = searchParams.get('code')
|
|
37
|
+
if (code) {
|
|
38
|
+
getPromoFromApi(code).then((promo?: Promo) => {
|
|
39
|
+
if (promo) {
|
|
40
|
+
form.setValue('code', code)
|
|
41
|
+
setCodeAccepted(true)
|
|
42
|
+
cmmc.setAppliedPromo(promo)
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
}, [searchParams])
|
|
47
|
+
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (cmmc.appliedPromo) {
|
|
50
|
+
form.setValue('code', cmmc.appliedPromo.code)
|
|
51
|
+
setCodeAccepted(true)
|
|
52
|
+
}
|
|
53
|
+
}, [cmmc.appliedPromo])
|
|
54
|
+
|
|
55
|
+
const form = useForm<z.infer<typeof formSchema>>({
|
|
56
|
+
resolver: zodResolver(formSchema),
|
|
57
|
+
defaultValues: {
|
|
58
|
+
code: '',
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
const applyPromoCode = async (values: z.infer<typeof formSchema>) => {
|
|
63
|
+
const { code } = values
|
|
64
|
+
const promo = await getPromoFromApi(code)
|
|
65
|
+
if (!promo) {
|
|
66
|
+
form.setError('code', { message: 'Invalid code' })
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
setCodeAccepted(true)
|
|
70
|
+
cmmc.setAppliedPromo(promo)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const removePromoCode = () => {
|
|
74
|
+
cmmc.setAppliedPromo(null)
|
|
75
|
+
setCodeAccepted(false)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<div className='flex flex-col gap-2 border-t py-1'>
|
|
80
|
+
<Form {...form}>
|
|
81
|
+
<form onSubmit={form.handleSubmit(applyPromoCode)}>
|
|
82
|
+
<div className='flex gap-2 items-center'>
|
|
83
|
+
<FormField
|
|
84
|
+
control={form.control}
|
|
85
|
+
name='code'
|
|
86
|
+
render={({ field }) => (
|
|
87
|
+
<FormItem className='w-full'>
|
|
88
|
+
<FormControl>
|
|
89
|
+
<Input {...field} className='h-9' placeholder='Discount or invite code' onInput={removePromoCode}/>
|
|
90
|
+
</FormControl>
|
|
91
|
+
</FormItem>
|
|
92
|
+
)}
|
|
93
|
+
/>
|
|
94
|
+
{codeAccepted ? (
|
|
95
|
+
<Check/>
|
|
96
|
+
) : (
|
|
97
|
+
<Button variant='outline' className='!w-fit !min-w-0 font-inter text-muted text-sm'>Apply</Button>
|
|
98
|
+
)}
|
|
99
|
+
</div>
|
|
100
|
+
</form>
|
|
101
|
+
</Form>
|
|
102
|
+
</div>
|
|
103
|
+
)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
export default PromoCode
|