-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCheckoutPage.tsx
155 lines (143 loc) · 4.22 KB
/
CheckoutPage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import {
KMCRecommendationLogic,
KlevuEvents,
KlevuFetch,
KlevuRecord,
KlevuResponseQueryObject,
kmcRecommendation,
sendRecommendationViewEvent,
} from "@klevu/core"
import { Button, Container, Grid, Typography } from "@mui/material"
import { CartItem, useCart } from "../cartContext"
import { Product } from "../components/product"
import groupBy from "lodash.groupby"
import { useSnackbar } from "notistack"
import { useEffect, useState } from "react"
import { RecommendationBanner } from "../components/recommendationBanner"
import { config } from "../config"
import { useNavigate } from "react-router-dom"
import { useGlobalVariables } from "../globalVariablesContext"
export function CheckoutPage() {
const [alsoBoughtProducts, setAlsoBoughtProducts] = useState<KlevuRecord[]>(
[]
)
const [alsoBoughtResult, setAlsoBoughtResult] = useState<
KlevuResponseQueryObject | undefined
>()
const cart = useCart()
const { enqueueSnackbar } = useSnackbar()
const navigate = useNavigate()
const { debugMode } = useGlobalVariables()
const fetchData = async () => {
if (!config.checkoutPageRecommendationId) {
return
}
const alsoBoughtId = "alsobought" + new Date().getTime()
let recsPayload
try {
recsPayload = await kmcRecommendation(
config.checkoutPageRecommendationId,
{
id: alsoBoughtId,
cartProductIds: cart.items.map((p) => p.id),
mode: "demo",
searchPrefs: debugMode ? ["debugQuery"] : undefined,
},
sendRecommendationViewEvent({
logic: KMCRecommendationLogic.BoughtTogether,
recsKey: "also-bought-together-demo",
title: "Also bought together KMC recommendation",
})
)
} catch (err) {
console.error("Failed to render checkout recs", err)
}
const result = await KlevuFetch(recsPayload || {})
if (recsPayload?.queries?.length > 0) {
setAlsoBoughtResult(result.queriesById(alsoBoughtId))
setAlsoBoughtProducts(result.queriesById(alsoBoughtId).records)
}
}
useEffect(() => {
fetchData()
}, [])
const buy = () => {
const groupedProducts = groupBy(cart.items, (i) => i.id)
const toBuy = Object.entries(groupedProducts).map((entry) => {
const data: KlevuRecord[] = entry[1] as KlevuRecord[]
const item: Parameters<typeof KlevuEvents.buy>[0]["items"][number] = {
product: data[0],
amount: data.length,
}
return item
})
KlevuEvents.buy({ items: toBuy })
enqueueSnackbar("Items bought!", {
variant: "info",
})
cart.clear()
}
const clear = () => {
enqueueSnackbar("Items cleared.", {
variant: "warning",
})
cart.clear()
}
return (
<Container maxWidth="lg">
<Typography variant="h2" style={{ textAlign: "center" }}>
Checkout - products in cart
</Typography>
<Grid
container
spacing={2}
style={{
margin: "24px",
}}
>
{cart.items.map((p, i) => (
<Grid item key={i}>
<Product
product={p}
hideAddToCart
onClick={(event) => {
navigate(
p.itemGroupId
? `/products/${p.itemGroupId}/${p.id}`
: `/products/${p.id}`
)
event.preventDefault()
return false
}}
/>
</Grid>
))}
</Grid>
<div style={{ display: "flex", justifyContent: "center", gap: "16px" }}>
<Button variant="outlined" color="secondary" onClick={clear}>
Clear
</Button>
<Button
variant="contained"
color="primary"
onClick={buy}
disabled={cart.items.length === 0}
>
Buy products
</Button>
</div>
{alsoBoughtProducts.length > 0 && (
<RecommendationBanner
products={alsoBoughtProducts}
title="Also bought together KMC recommendation"
productClick={(productId, variantId) => {
alsoBoughtResult.recommendationClickEvent?.({
productId,
variantId,
})
}}
/>
)}
</Container>
)
}