@hszrdm/create-commerce 1.4.0 → 1.5.0
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/package.json +1 -1
- package/templates/backend/.env.example +13 -0
- package/templates/backend/README.md +4 -0
- package/templates/backend/package.json +3 -1
- package/templates/backend/scripts/core-backup.mjs +281 -0
- package/templates/backend/scripts/core-restore.mjs +179 -0
- package/templates/backend/scripts/lib/core-backup-common.mjs +237 -0
- package/templates/backend/scripts/production-assets/deploy.sh +2 -1
- package/templates/backend/scripts/production-assets/install.sh +2 -1
- package/templates/backend/scripts/production-assets/restore.sh +4 -1
- package/templates/backend/src/scripts/core-init.ts +3 -1
- package/templates/storefront/src/app/cart-recovery/page.tsx +35 -0
- package/templates/storefront/src/app/ertekeles/page.tsx +25 -0
- package/templates/storefront/src/app/fiok-aktivalas/page.tsx +35 -0
- package/templates/storefront/src/components/abandoned-cart-recovery.tsx +139 -0
- package/templates/storefront/src/components/account-activation-form.tsx +333 -0
- package/templates/storefront/src/components/add-to-cart.tsx +56 -0
- package/templates/storefront/src/components/analytics.tsx +15 -2
- package/templates/storefront/src/components/review-request-form.tsx +419 -0
- package/templates/storefront/src/lib/core/abandoned-cart.ts +47 -0
- package/templates/storefront/src/lib/core/account-activation.ts +75 -0
- package/templates/storefront/src/lib/core/back-in-stock.ts +25 -0
- package/templates/storefront/src/lib/core/reviews.ts +37 -5
- package/templates/storefront/src/lib/storefront-routes.ts +3 -0
- package/templates/storefront/src/proxy.ts +4 -0
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import Link from "next/link"
|
|
4
|
+
import { usePathname, useSearchParams } from "next/navigation"
|
|
5
|
+
import { useEffect, useMemo, useState, type FormEvent } from "react"
|
|
6
|
+
import { accountApi } from "@/lib/core/account"
|
|
7
|
+
import { CoreApiError } from "@/lib/core/client"
|
|
8
|
+
import {
|
|
9
|
+
loadReviewRequest,
|
|
10
|
+
submitProductReview,
|
|
11
|
+
type ReviewRequest,
|
|
12
|
+
type ReviewRequestProduct,
|
|
13
|
+
} from "@/lib/core/reviews"
|
|
14
|
+
import type { CoreConfig } from "@/lib/core/types"
|
|
15
|
+
import {
|
|
16
|
+
localeFromPathname,
|
|
17
|
+
productPath,
|
|
18
|
+
storefrontLocale,
|
|
19
|
+
STOREFRONT_ROUTES,
|
|
20
|
+
type StorefrontLocale,
|
|
21
|
+
} from "@/lib/storefront-routes"
|
|
22
|
+
|
|
23
|
+
type Draft = {
|
|
24
|
+
rating: number
|
|
25
|
+
body: string
|
|
26
|
+
busy: boolean
|
|
27
|
+
error: string | null
|
|
28
|
+
submittedStatus: "pending" | "published" | "rejected" | null
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function ReviewRequestForm({ config }: { config: CoreConfig }) {
|
|
32
|
+
const pathname = usePathname()
|
|
33
|
+
const search = useSearchParams()
|
|
34
|
+
const fallbackLocale = storefrontLocale(config.store.locale)
|
|
35
|
+
const locale = localeFromPathname(pathname, fallbackLocale)
|
|
36
|
+
const copy = reviewCopy(locale)
|
|
37
|
+
const routes = STOREFRONT_ROUTES[locale]
|
|
38
|
+
const token = search.get("token")?.trim() || ""
|
|
39
|
+
const account = useMemo(() => accountApi(config), [config])
|
|
40
|
+
|
|
41
|
+
const [request, setRequest] = useState<ReviewRequest | null>(null)
|
|
42
|
+
const [drafts, setDrafts] = useState<Record<string, Draft>>({})
|
|
43
|
+
const [loading, setLoading] = useState(true)
|
|
44
|
+
const [loadError, setLoadError] = useState<string | null>(null)
|
|
45
|
+
const [authenticated, setAuthenticated] = useState<boolean | null>(null)
|
|
46
|
+
const [loginEmail, setLoginEmail] = useState("")
|
|
47
|
+
const [loginPassword, setLoginPassword] = useState("")
|
|
48
|
+
const [loginBusy, setLoginBusy] = useState(false)
|
|
49
|
+
const [loginError, setLoginError] = useState<string | null>(null)
|
|
50
|
+
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
let cancelled = false
|
|
53
|
+
if (!token) {
|
|
54
|
+
setLoadError(copy.missingToken)
|
|
55
|
+
setLoading(false)
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
setLoading(true)
|
|
60
|
+
setLoadError(null)
|
|
61
|
+
void loadReviewRequest(token)
|
|
62
|
+
.then((payload) => {
|
|
63
|
+
if (cancelled) return
|
|
64
|
+
setRequest(payload)
|
|
65
|
+
setDrafts((current) => {
|
|
66
|
+
const next = { ...current }
|
|
67
|
+
for (const product of payload.products) {
|
|
68
|
+
next[product.product_id] ||= emptyDraft()
|
|
69
|
+
}
|
|
70
|
+
return next
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
.catch((cause) => {
|
|
74
|
+
if (cancelled) return
|
|
75
|
+
setLoadError(reviewRequestError(cause, locale))
|
|
76
|
+
})
|
|
77
|
+
.finally(() => {
|
|
78
|
+
if (!cancelled) setLoading(false)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
return () => {
|
|
82
|
+
cancelled = true
|
|
83
|
+
}
|
|
84
|
+
}, [copy.missingToken, locale, token])
|
|
85
|
+
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
let cancelled = false
|
|
88
|
+
void account
|
|
89
|
+
.profile()
|
|
90
|
+
.then(() => {
|
|
91
|
+
if (!cancelled) setAuthenticated(true)
|
|
92
|
+
})
|
|
93
|
+
.catch((cause) => {
|
|
94
|
+
if (cancelled) return
|
|
95
|
+
if (cause instanceof CoreApiError && cause.status === 401) {
|
|
96
|
+
setAuthenticated(false)
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
setAuthenticated(false)
|
|
100
|
+
})
|
|
101
|
+
return () => {
|
|
102
|
+
cancelled = true
|
|
103
|
+
}
|
|
104
|
+
}, [account])
|
|
105
|
+
|
|
106
|
+
async function login(event: FormEvent<HTMLFormElement>) {
|
|
107
|
+
event.preventDefault()
|
|
108
|
+
if (loginBusy) return
|
|
109
|
+
setLoginBusy(true)
|
|
110
|
+
setLoginError(null)
|
|
111
|
+
try {
|
|
112
|
+
await account.login(loginEmail.trim(), loginPassword)
|
|
113
|
+
setAuthenticated(true)
|
|
114
|
+
setLoginPassword("")
|
|
115
|
+
} catch (cause) {
|
|
116
|
+
setLoginError(cause instanceof Error ? cause.message : copy.loginFailed)
|
|
117
|
+
} finally {
|
|
118
|
+
setLoginBusy(false)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function submit(product: ReviewRequestProduct, event: FormEvent<HTMLFormElement>) {
|
|
123
|
+
event.preventDefault()
|
|
124
|
+
const draft = drafts[product.product_id] || emptyDraft()
|
|
125
|
+
if (!authenticated) {
|
|
126
|
+
setDraft(product.product_id, { error: copy.loginRequired })
|
|
127
|
+
return
|
|
128
|
+
}
|
|
129
|
+
if (!draft.body.trim() || draft.busy || product.reviewed) return
|
|
130
|
+
|
|
131
|
+
setDraft(product.product_id, { busy: true, error: null })
|
|
132
|
+
try {
|
|
133
|
+
const result = await submitProductReview(product.product_id, {
|
|
134
|
+
rating: draft.rating,
|
|
135
|
+
body: draft.body.trim(),
|
|
136
|
+
review_request_token: token,
|
|
137
|
+
})
|
|
138
|
+
const status = result.review.status || "pending"
|
|
139
|
+
setDraft(product.product_id, {
|
|
140
|
+
busy: false,
|
|
141
|
+
body: "",
|
|
142
|
+
submittedStatus: status,
|
|
143
|
+
})
|
|
144
|
+
setRequest((current) => {
|
|
145
|
+
if (!current) return current
|
|
146
|
+
const products = current.products.map((item) =>
|
|
147
|
+
item.product_id === product.product_id ? { ...item, reviewed: true } : item,
|
|
148
|
+
)
|
|
149
|
+
if (products.length > 0 && products.every((item) => item.reviewed) && typeof window !== "undefined") {
|
|
150
|
+
window.history.replaceState(window.history.state, "", pathname)
|
|
151
|
+
}
|
|
152
|
+
return { ...current, products }
|
|
153
|
+
})
|
|
154
|
+
} catch (cause) {
|
|
155
|
+
if (cause instanceof CoreApiError && cause.status === 401) {
|
|
156
|
+
setAuthenticated(false)
|
|
157
|
+
setDraft(product.product_id, { busy: false, error: copy.loginRequired })
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
setDraft(product.product_id, {
|
|
161
|
+
busy: false,
|
|
162
|
+
error: cause instanceof Error ? cause.message : copy.submitFailed,
|
|
163
|
+
})
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function setDraft(productId: string, patch: Partial<Draft>) {
|
|
168
|
+
setDrafts((current) => ({
|
|
169
|
+
...current,
|
|
170
|
+
[productId]: { ...(current[productId] || emptyDraft()), ...patch },
|
|
171
|
+
}))
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (loading) return <div className="empty-state">{copy.checking}</div>
|
|
175
|
+
|
|
176
|
+
if (loadError || !request) {
|
|
177
|
+
return (
|
|
178
|
+
<section className="panel stack narrow-card">
|
|
179
|
+
<div>
|
|
180
|
+
<p className="eyebrow">{copy.eyebrow}</p>
|
|
181
|
+
<h1>{copy.invalidTitle}</h1>
|
|
182
|
+
</div>
|
|
183
|
+
<p className="error">{loadError || copy.invalidText}</p>
|
|
184
|
+
<Link className="button secondary" href={routes.home}>{copy.backHome}</Link>
|
|
185
|
+
</section>
|
|
186
|
+
)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const remaining = request.products.filter((product) => !product.reviewed).length
|
|
190
|
+
|
|
191
|
+
return (
|
|
192
|
+
<div className="stack">
|
|
193
|
+
<section className="panel stack-sm">
|
|
194
|
+
<div>
|
|
195
|
+
<p className="eyebrow">{copy.eyebrow}</p>
|
|
196
|
+
<h1>{copy.title}</h1>
|
|
197
|
+
</div>
|
|
198
|
+
<p className="muted">
|
|
199
|
+
{copy.orderLabel} #{String(request.order.display_id || request.order.id)} · {remaining
|
|
200
|
+
? copy.remaining.replace("{count}", String(remaining))
|
|
201
|
+
: copy.complete}
|
|
202
|
+
</p>
|
|
203
|
+
{request.reward?.available_after_publish ? (
|
|
204
|
+
<p className="muted small">{copy.rewardHint}</p>
|
|
205
|
+
) : null}
|
|
206
|
+
</section>
|
|
207
|
+
|
|
208
|
+
{authenticated === false ? (
|
|
209
|
+
<section className="panel stack narrow-card">
|
|
210
|
+
<div>
|
|
211
|
+
<p className="eyebrow">{copy.signInEyebrow}</p>
|
|
212
|
+
<h2>{copy.signInTitle}</h2>
|
|
213
|
+
</div>
|
|
214
|
+
<p className="muted">{copy.signInText}</p>
|
|
215
|
+
<form className="stack-sm" onSubmit={login}>
|
|
216
|
+
<label>
|
|
217
|
+
{copy.email}
|
|
218
|
+
<input
|
|
219
|
+
autoComplete="email"
|
|
220
|
+
type="email"
|
|
221
|
+
required
|
|
222
|
+
value={loginEmail}
|
|
223
|
+
onChange={(event) => setLoginEmail(event.target.value)}
|
|
224
|
+
/>
|
|
225
|
+
</label>
|
|
226
|
+
<label>
|
|
227
|
+
{copy.password}
|
|
228
|
+
<input
|
|
229
|
+
autoComplete="current-password"
|
|
230
|
+
type="password"
|
|
231
|
+
required
|
|
232
|
+
value={loginPassword}
|
|
233
|
+
onChange={(event) => setLoginPassword(event.target.value)}
|
|
234
|
+
/>
|
|
235
|
+
</label>
|
|
236
|
+
{loginError ? <p className="error">{loginError}</p> : null}
|
|
237
|
+
<button className="button button-large" disabled={loginBusy} type="submit">
|
|
238
|
+
{loginBusy ? copy.signingIn : copy.signIn}
|
|
239
|
+
</button>
|
|
240
|
+
</form>
|
|
241
|
+
<Link className="text-link" href={routes.account}>{copy.accountHelp}</Link>
|
|
242
|
+
</section>
|
|
243
|
+
) : null}
|
|
244
|
+
|
|
245
|
+
{request.products.length ? (
|
|
246
|
+
<div className="stack">
|
|
247
|
+
{request.products.map((product) => {
|
|
248
|
+
const draft = drafts[product.product_id] || emptyDraft()
|
|
249
|
+
const reviewed = product.reviewed
|
|
250
|
+
return (
|
|
251
|
+
<section className="panel stack" key={product.product_id}>
|
|
252
|
+
<div className="section-title-row">
|
|
253
|
+
<div>
|
|
254
|
+
<p className="eyebrow">{copy.productEyebrow}</p>
|
|
255
|
+
<h2>{product.title}</h2>
|
|
256
|
+
</div>
|
|
257
|
+
{reviewed ? <span className="status-pill success-pill">{copy.reviewed}</span> : null}
|
|
258
|
+
</div>
|
|
259
|
+
|
|
260
|
+
{product.handle ? (
|
|
261
|
+
<Link className="text-link" href={productPath(locale, product.handle)}>{copy.viewProduct}</Link>
|
|
262
|
+
) : null}
|
|
263
|
+
|
|
264
|
+
{reviewed ? (
|
|
265
|
+
<div className="empty-state">
|
|
266
|
+
{draft.submittedStatus === "published" ? copy.publishedSuccess : draft.submittedStatus === "pending" ? copy.pendingSuccess : copy.alreadyReviewed}
|
|
267
|
+
</div>
|
|
268
|
+
) : (
|
|
269
|
+
<form className="stack-sm review-form" onSubmit={(event) => void submit(product, event)}>
|
|
270
|
+
<fieldset disabled={draft.busy || authenticated !== true} className="stack-sm">
|
|
271
|
+
<legend className="field-label">{copy.rating}</legend>
|
|
272
|
+
<div className="button-row" role="radiogroup" aria-label={copy.rating}>
|
|
273
|
+
{[1, 2, 3, 4, 5].map((value) => (
|
|
274
|
+
<button
|
|
275
|
+
className="button secondary"
|
|
276
|
+
aria-checked={draft.rating === value}
|
|
277
|
+
role="radio"
|
|
278
|
+
type="button"
|
|
279
|
+
key={value}
|
|
280
|
+
onClick={() => setDraft(product.product_id, { rating: value })}
|
|
281
|
+
>
|
|
282
|
+
{draft.rating >= value ? "★" : "☆"}
|
|
283
|
+
</button>
|
|
284
|
+
))}
|
|
285
|
+
</div>
|
|
286
|
+
</fieldset>
|
|
287
|
+
<label>
|
|
288
|
+
{copy.reviewBody}
|
|
289
|
+
<textarea
|
|
290
|
+
rows={5}
|
|
291
|
+
maxLength={5000}
|
|
292
|
+
required
|
|
293
|
+
disabled={draft.busy || authenticated !== true}
|
|
294
|
+
value={draft.body}
|
|
295
|
+
onChange={(event) => setDraft(product.product_id, { body: event.target.value })}
|
|
296
|
+
placeholder={copy.placeholder}
|
|
297
|
+
/>
|
|
298
|
+
</label>
|
|
299
|
+
{authenticated !== true ? <p className="muted small">{copy.loginRequired}</p> : null}
|
|
300
|
+
{draft.error ? <p className="error">{draft.error}</p> : null}
|
|
301
|
+
<button
|
|
302
|
+
className="button"
|
|
303
|
+
type="submit"
|
|
304
|
+
disabled={draft.busy || authenticated !== true || !draft.body.trim()}
|
|
305
|
+
>
|
|
306
|
+
{draft.busy ? copy.submitting : copy.submit}
|
|
307
|
+
</button>
|
|
308
|
+
</form>
|
|
309
|
+
)}
|
|
310
|
+
</section>
|
|
311
|
+
)
|
|
312
|
+
})}
|
|
313
|
+
</div>
|
|
314
|
+
) : (
|
|
315
|
+
<div className="empty-state">{copy.noProducts}</div>
|
|
316
|
+
)}
|
|
317
|
+
</div>
|
|
318
|
+
)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function emptyDraft(): Draft {
|
|
322
|
+
return {
|
|
323
|
+
rating: 5,
|
|
324
|
+
body: "",
|
|
325
|
+
busy: false,
|
|
326
|
+
error: null,
|
|
327
|
+
submittedStatus: null,
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function reviewRequestError(cause: unknown, locale: StorefrontLocale) {
|
|
332
|
+
if (cause instanceof CoreApiError) {
|
|
333
|
+
if (cause.status === 400) return locale === "en" ? "This review link is invalid or has expired." : "Ez az értékelési link érvénytelen vagy lejárt."
|
|
334
|
+
if (cause.status === 404) return locale === "en" ? "The order linked to this review request could not be found." : "Az értékelési linkhez tartozó rendelés nem található."
|
|
335
|
+
}
|
|
336
|
+
return cause instanceof Error
|
|
337
|
+
? cause.message
|
|
338
|
+
: locale === "en"
|
|
339
|
+
? "The review request could not be loaded."
|
|
340
|
+
: "Az értékelési kérés betöltése nem sikerült."
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function reviewCopy(locale: StorefrontLocale) {
|
|
344
|
+
if (locale === "en") {
|
|
345
|
+
return {
|
|
346
|
+
eyebrow: "Product reviews",
|
|
347
|
+
title: "Tell us what you think",
|
|
348
|
+
checking: "Checking review link…",
|
|
349
|
+
missingToken: "The review token is missing. Open the full link from the email you received.",
|
|
350
|
+
invalidTitle: "Review link unavailable",
|
|
351
|
+
invalidText: "This review link cannot be used.",
|
|
352
|
+
backHome: "Back to store",
|
|
353
|
+
orderLabel: "Order",
|
|
354
|
+
remaining: "{count} product(s) left to review",
|
|
355
|
+
complete: "all products reviewed",
|
|
356
|
+
rewardHint: "If review rewards are enabled, the reward is issued after the review is published and may arrive by email.",
|
|
357
|
+
signInEyebrow: "Verification",
|
|
358
|
+
signInTitle: "Sign in to submit your review",
|
|
359
|
+
signInText: "The signed review link identifies the order, and signing in confirms that you are the customer who placed it.",
|
|
360
|
+
email: "Email",
|
|
361
|
+
password: "Password",
|
|
362
|
+
signingIn: "Signing in…",
|
|
363
|
+
signIn: "Sign in",
|
|
364
|
+
loginFailed: "Sign in failed.",
|
|
365
|
+
accountHelp: "Account and password help",
|
|
366
|
+
productEyebrow: "Purchased product",
|
|
367
|
+
reviewed: "Reviewed",
|
|
368
|
+
viewProduct: "View product",
|
|
369
|
+
rating: "Rating",
|
|
370
|
+
reviewBody: "Your review",
|
|
371
|
+
placeholder: "Share your experience with this product.",
|
|
372
|
+
loginRequired: "Sign in with the account that placed this order to submit a review.",
|
|
373
|
+
submitting: "Submitting…",
|
|
374
|
+
submit: "Submit review",
|
|
375
|
+
submitFailed: "The review could not be submitted.",
|
|
376
|
+
publishedSuccess: "Thank you! Your review has been published.",
|
|
377
|
+
pendingSuccess: "Thank you! Your review was submitted for moderation.",
|
|
378
|
+
alreadyReviewed: "You have already reviewed this product.",
|
|
379
|
+
noProducts: "There are no reviewable products in this order.",
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
return {
|
|
384
|
+
eyebrow: "Termékértékelés",
|
|
385
|
+
title: "Írd meg a véleményed",
|
|
386
|
+
checking: "Értékelési link ellenőrzése…",
|
|
387
|
+
missingToken: "Hiányzik az értékelési token. Nyisd meg újra az e-mailben kapott teljes hivatkozást.",
|
|
388
|
+
invalidTitle: "Az értékelési link nem használható",
|
|
389
|
+
invalidText: "Ez az értékelési link nem használható.",
|
|
390
|
+
backHome: "Vissza a webshopba",
|
|
391
|
+
orderLabel: "Rendelés",
|
|
392
|
+
remaining: "Még {count} termék vár értékelésre",
|
|
393
|
+
complete: "minden terméket értékeltél",
|
|
394
|
+
rewardHint: "Ha a webshopban aktív az értékelési jutalom, a kedvezmény a vélemény publikálása után jár, és e-mailben is megérkezhet.",
|
|
395
|
+
signInEyebrow: "Azonosítás",
|
|
396
|
+
signInTitle: "Jelentkezz be az értékeléshez",
|
|
397
|
+
signInText: "Az aláírt értékelési link azonosítja a rendelést, a bejelentkezés pedig megerősíti, hogy te vagy a vásárló.",
|
|
398
|
+
email: "E-mail",
|
|
399
|
+
password: "Jelszó",
|
|
400
|
+
signingIn: "Bejelentkezés…",
|
|
401
|
+
signIn: "Bejelentkezés",
|
|
402
|
+
loginFailed: "A bejelentkezés nem sikerült.",
|
|
403
|
+
accountHelp: "Fiók- és jelszókezelés",
|
|
404
|
+
productEyebrow: "Megvásárolt termék",
|
|
405
|
+
reviewed: "Értékelve",
|
|
406
|
+
viewProduct: "Termék megtekintése",
|
|
407
|
+
rating: "Értékelés",
|
|
408
|
+
reviewBody: "Véleményed",
|
|
409
|
+
placeholder: "Írd le a tapasztalatodat a termékről.",
|
|
410
|
+
loginRequired: "Az értékelés elküldéséhez jelentkezz be azzal a fiókkal, amellyel a rendelést leadtad.",
|
|
411
|
+
submitting: "Küldés…",
|
|
412
|
+
submit: "Vélemény elküldése",
|
|
413
|
+
submitFailed: "A véleményt nem sikerült elküldeni.",
|
|
414
|
+
publishedSuccess: "Köszönjük! A véleményed megjelent.",
|
|
415
|
+
pendingSuccess: "Köszönjük! A véleményed moderálásra elküldtük.",
|
|
416
|
+
alreadyReviewed: "Ezt a terméket már értékelted.",
|
|
417
|
+
noProducts: "Ebben a rendelésben nincs értékelhető termék.",
|
|
418
|
+
}
|
|
419
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { CoreApiError, medusaFetch } from "./client"
|
|
2
|
+
import type { CoreConfig } from "./types"
|
|
3
|
+
|
|
4
|
+
export type AbandonedCartRecoveryResult = {
|
|
5
|
+
valid: boolean
|
|
6
|
+
reason?: string
|
|
7
|
+
cart_id?: string
|
|
8
|
+
locale?: "hu-HU" | "en-US" | string
|
|
9
|
+
coupon_code?: string | null
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function abandonedCartApi(config: CoreConfig) {
|
|
13
|
+
const recoveryEndpoint =
|
|
14
|
+
config.checkout.endpoints.abandoned_cart_recovery ||
|
|
15
|
+
"/store/abandoned-cart/recover"
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
recover: async (token: string) => {
|
|
19
|
+
const query = new URLSearchParams({ token })
|
|
20
|
+
try {
|
|
21
|
+
return await medusaFetch<AbandonedCartRecoveryResult>(
|
|
22
|
+
`${recoveryEndpoint}?${query.toString()}`,
|
|
23
|
+
{ cache: "no-store" },
|
|
24
|
+
)
|
|
25
|
+
} catch (cause) {
|
|
26
|
+
if (
|
|
27
|
+
cause instanceof CoreApiError &&
|
|
28
|
+
(cause.status === 400 || cause.status === 410) &&
|
|
29
|
+
isRecord(cause.payload)
|
|
30
|
+
) {
|
|
31
|
+
return {
|
|
32
|
+
valid: false,
|
|
33
|
+
reason:
|
|
34
|
+
typeof cause.payload.reason === "string"
|
|
35
|
+
? cause.payload.reason
|
|
36
|
+
: "invalid",
|
|
37
|
+
} satisfies AbandonedCartRecoveryResult
|
|
38
|
+
}
|
|
39
|
+
throw cause
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
46
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value)
|
|
47
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { CoreApiError, medusaFetch } from "./client"
|
|
2
|
+
import type { CoreConfig } from "./types"
|
|
3
|
+
|
|
4
|
+
export type CustomerActivationReward = {
|
|
5
|
+
enabled?: boolean
|
|
6
|
+
type?: "percentage" | "fixed"
|
|
7
|
+
value?: number
|
|
8
|
+
currency_code?: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type CustomerActivationValidation = {
|
|
12
|
+
valid: boolean
|
|
13
|
+
reason?: "missing" | "expired" | "used" | "invalid" | string | null
|
|
14
|
+
customer_id?: string
|
|
15
|
+
masked_email?: string
|
|
16
|
+
first_name?: string | null
|
|
17
|
+
expires_at?: string | null
|
|
18
|
+
reward?: CustomerActivationReward | null
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type CustomerActivationResult = {
|
|
22
|
+
activated: true
|
|
23
|
+
customer_id: string
|
|
24
|
+
coupon_code: string | null
|
|
25
|
+
reward: CustomerActivationReward | null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function customerActivationApi(config: CoreConfig) {
|
|
29
|
+
const validationEndpoint =
|
|
30
|
+
config.account.endpoints.customer_activation || "/store/customer-activation"
|
|
31
|
+
const completionEndpoint =
|
|
32
|
+
config.account.endpoints.customer_activation_complete ||
|
|
33
|
+
"/store/customer-activation/complete"
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
validate: async (customerId: string, token: string) => {
|
|
37
|
+
const query = new URLSearchParams({ customer: customerId, token })
|
|
38
|
+
try {
|
|
39
|
+
return await medusaFetch<CustomerActivationValidation>(
|
|
40
|
+
`${validationEndpoint}?${query.toString()}`,
|
|
41
|
+
{ cache: "no-store" },
|
|
42
|
+
)
|
|
43
|
+
} catch (cause) {
|
|
44
|
+
if (
|
|
45
|
+
cause instanceof CoreApiError &&
|
|
46
|
+
(cause.status === 400 || cause.status === 410) &&
|
|
47
|
+
isRecord(cause.payload)
|
|
48
|
+
) {
|
|
49
|
+
return {
|
|
50
|
+
valid: false,
|
|
51
|
+
reason:
|
|
52
|
+
typeof cause.payload.reason === "string"
|
|
53
|
+
? cause.payload.reason
|
|
54
|
+
: "invalid",
|
|
55
|
+
} satisfies CustomerActivationValidation
|
|
56
|
+
}
|
|
57
|
+
throw cause
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
complete: (customerId: string, token: string, password: string) =>
|
|
62
|
+
medusaFetch<CustomerActivationResult>(completionEndpoint, {
|
|
63
|
+
method: "POST",
|
|
64
|
+
body: JSON.stringify({
|
|
65
|
+
customer_id: customerId,
|
|
66
|
+
token,
|
|
67
|
+
password,
|
|
68
|
+
}),
|
|
69
|
+
}),
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
74
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value)
|
|
75
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { medusaFetch } from "./client"
|
|
2
|
+
|
|
3
|
+
export type BackInStockResponse = {
|
|
4
|
+
subscribed: boolean
|
|
5
|
+
already_available?: boolean
|
|
6
|
+
already_subscribed?: boolean
|
|
7
|
+
reactivated?: boolean
|
|
8
|
+
subscription_id?: string
|
|
9
|
+
availability?: number | null
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function subscribeBackInStock(input: {
|
|
13
|
+
email: string
|
|
14
|
+
variantId: string
|
|
15
|
+
locale: string
|
|
16
|
+
}) {
|
|
17
|
+
return medusaFetch<BackInStockResponse>("/store/back-in-stock", {
|
|
18
|
+
method: "POST",
|
|
19
|
+
body: JSON.stringify({
|
|
20
|
+
email: input.email,
|
|
21
|
+
variant_id: input.variantId,
|
|
22
|
+
locale: input.locale,
|
|
23
|
+
}),
|
|
24
|
+
})
|
|
25
|
+
}
|
|
@@ -7,7 +7,7 @@ export type ProductReview = {
|
|
|
7
7
|
rating: number
|
|
8
8
|
body: string
|
|
9
9
|
verified_purchase: boolean
|
|
10
|
-
status?: "pending" | "
|
|
10
|
+
status?: "pending" | "published" | "rejected"
|
|
11
11
|
created_at?: string
|
|
12
12
|
updated_at?: string
|
|
13
13
|
}
|
|
@@ -17,6 +17,25 @@ export type ProductReviewSummary = {
|
|
|
17
17
|
count: number
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
export type ReviewRequestProduct = {
|
|
21
|
+
product_id: string
|
|
22
|
+
title: string
|
|
23
|
+
handle: string | null
|
|
24
|
+
thumbnail: string | null
|
|
25
|
+
reviewed: boolean
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type ReviewRequest = {
|
|
29
|
+
order: {
|
|
30
|
+
id: string
|
|
31
|
+
display_id: string | number | null
|
|
32
|
+
}
|
|
33
|
+
products: ReviewRequestProduct[]
|
|
34
|
+
reward: {
|
|
35
|
+
available_after_publish: boolean
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
20
39
|
export function listProductReviews(productId: string, options: { limit?: number; offset?: number } = {}) {
|
|
21
40
|
const params = new URLSearchParams()
|
|
22
41
|
if (options.limit != null) params.set("limit", String(options.limit))
|
|
@@ -32,9 +51,22 @@ export function listProductReviews(productId: string, options: { limit?: number;
|
|
|
32
51
|
})
|
|
33
52
|
}
|
|
34
53
|
|
|
35
|
-
export function
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
54
|
+
export function loadReviewRequest(token: string) {
|
|
55
|
+
const params = new URLSearchParams({ token })
|
|
56
|
+
return medusaFetch<ReviewRequest>(`/store/review-request?${params.toString()}`, {
|
|
57
|
+
cache: "no-store",
|
|
39
58
|
})
|
|
40
59
|
}
|
|
60
|
+
|
|
61
|
+
export function submitProductReview(
|
|
62
|
+
productId: string,
|
|
63
|
+
input: { rating: number; body: string; review_request_token?: string },
|
|
64
|
+
) {
|
|
65
|
+
return medusaFetch<{ review: ProductReview; reward_error?: string | null }>(
|
|
66
|
+
`/store/products/${encodeURIComponent(productId)}/reviews`,
|
|
67
|
+
{
|
|
68
|
+
method: "POST",
|
|
69
|
+
body: JSON.stringify(input),
|
|
70
|
+
},
|
|
71
|
+
)
|
|
72
|
+
}
|
|
@@ -5,6 +5,7 @@ export const STOREFRONT_ROUTES = {
|
|
|
5
5
|
products: "/hu/termekek",
|
|
6
6
|
categories: "/hu/kategoriak",
|
|
7
7
|
cart: "/hu/kosar",
|
|
8
|
+
cartRecovery: "/hu/kosar-visszaallitas",
|
|
8
9
|
checkout: "/hu/penztar",
|
|
9
10
|
checkoutSuccess: "/hu/penztar/sikeres-rendeles",
|
|
10
11
|
account: "/hu/fiokom",
|
|
@@ -24,6 +25,7 @@ export const STOREFRONT_ROUTES = {
|
|
|
24
25
|
products: "/en/products",
|
|
25
26
|
categories: "/en/categories",
|
|
26
27
|
cart: "/en/cart",
|
|
28
|
+
cartRecovery: "/en/cart-recovery",
|
|
27
29
|
checkout: "/en/checkout",
|
|
28
30
|
checkoutSuccess: "/en/checkout/success",
|
|
29
31
|
account: "/en/account",
|
|
@@ -101,6 +103,7 @@ export function equivalentLocalePath(
|
|
|
101
103
|
return `${target.categories}/${pathname.slice(source.categories.length + 1)}`
|
|
102
104
|
}
|
|
103
105
|
if (pathname === source.cart) return target.cart
|
|
106
|
+
if (pathname === source.cartRecovery) return target.cartRecovery
|
|
104
107
|
if (pathname === source.checkout) return target.checkout
|
|
105
108
|
if (pathname === source.checkoutSuccess) return target.checkoutSuccess
|
|
106
109
|
if (pathname === source.account) return target.account
|
|
@@ -52,6 +52,7 @@ function legacyRedirect(pathname: string, locale: StorefrontLocale) {
|
|
|
52
52
|
if (pathname === "/") return STOREFRONT_ROUTES[locale].home
|
|
53
53
|
|
|
54
54
|
if (pathname === "/cart") return STOREFRONT_ROUTES[locale].cart
|
|
55
|
+
if (pathname === "/cart-recovery" || pathname === "/kosar-visszaallitas") return STOREFRONT_ROUTES[locale].cartRecovery
|
|
55
56
|
if (pathname === "/checkout") return STOREFRONT_ROUTES[locale].checkout
|
|
56
57
|
if (pathname === "/checkout/success") return STOREFRONT_ROUTES[locale].checkoutSuccess
|
|
57
58
|
if (pathname === "/account") return STOREFRONT_ROUTES[locale].account
|
|
@@ -72,6 +73,7 @@ function legacyRedirect(pathname: string, locale: StorefrontLocale) {
|
|
|
72
73
|
if (pathname === "/hu/categories") return STOREFRONT_ROUTES.hu.categories
|
|
73
74
|
if (pathname.startsWith("/hu/categories/")) return `${STOREFRONT_ROUTES.hu.categories}/${pathname.slice("/hu/categories/".length)}`
|
|
74
75
|
if (pathname === "/hu/cart") return STOREFRONT_ROUTES.hu.cart
|
|
76
|
+
if (pathname === "/hu/cart-recovery") return STOREFRONT_ROUTES.hu.cartRecovery
|
|
75
77
|
if (pathname === "/hu/checkout") return STOREFRONT_ROUTES.hu.checkout
|
|
76
78
|
if (pathname === "/hu/checkout/success") return STOREFRONT_ROUTES.hu.checkoutSuccess
|
|
77
79
|
if (pathname === "/hu/account") return STOREFRONT_ROUTES.hu.account
|
|
@@ -79,6 +81,7 @@ function legacyRedirect(pathname: string, locale: StorefrontLocale) {
|
|
|
79
81
|
if (pathname.startsWith("/hu/account/orders/")) return `${STOREFRONT_ROUTES.hu.orders}/${pathname.slice("/hu/account/orders/".length)}`
|
|
80
82
|
if (pathname === "/hu/reset-password") return STOREFRONT_ROUTES.hu.resetPassword
|
|
81
83
|
if (pathname === "/hu/legal") return STOREFRONT_ROUTES.hu.legal
|
|
84
|
+
if (pathname === "/en/kosar-visszaallitas") return STOREFRONT_ROUTES.en.cartRecovery
|
|
82
85
|
|
|
83
86
|
return null
|
|
84
87
|
}
|
|
@@ -96,6 +99,7 @@ function internalRewrite(pathname: string, locale: StorefrontLocale) {
|
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
if (pathname === routes.cart) return "/cart"
|
|
102
|
+
if (pathname === routes.cartRecovery) return "/cart-recovery"
|
|
99
103
|
if (pathname === routes.checkout) return "/checkout"
|
|
100
104
|
if (pathname === routes.checkoutSuccess) return "/checkout/success"
|
|
101
105
|
if (pathname === routes.account) return "/account"
|