@hszrdm/create-commerce 1.1.0 → 1.3.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 +2 -2
- package/src/cli.mjs +5 -2
- package/templates/backend/.env.example +45 -0
- package/templates/backend/package.json +1 -1
- package/templates/storefront/README.md +14 -1
- package/templates/storefront/src/app/[countryCode]/products/[handle]/page.tsx +43 -9
- package/templates/storefront/src/app/account/page.tsx +12 -1
- package/templates/storefront/src/app/account/wishlist/page.tsx +9 -0
- package/templates/storefront/src/app/blog/[slug]/page.tsx +36 -0
- package/templates/storefront/src/app/blog/page.tsx +25 -0
- package/templates/storefront/src/app/contact/page.tsx +25 -0
- package/templates/storefront/src/app/core-ux.css +244 -0
- package/templates/storefront/src/app/customer-capabilities.css +60 -0
- package/templates/storefront/src/app/knowledge-base/[slug]/page.tsx +35 -0
- package/templates/storefront/src/app/knowledge-base/page.tsx +25 -0
- package/templates/storefront/src/app/layout.tsx +40 -12
- package/templates/storefront/src/app/newsletter/confirm/page.tsx +19 -0
- package/templates/storefront/src/app/newsletter/unsubscribe/page.tsx +19 -0
- package/templates/storefront/src/app/pages/[slug]/page.tsx +35 -0
- package/templates/storefront/src/app/quote-request/page.tsx +20 -0
- package/templates/storefront/src/components/accessibility-preferences.tsx +126 -0
- package/templates/storefront/src/components/accessible-dialog.tsx +88 -0
- package/templates/storefront/src/components/accessible-dropdown.tsx +98 -0
- package/templates/storefront/src/components/account-wishlist.tsx +95 -0
- package/templates/storefront/src/components/add-to-cart.tsx +110 -15
- package/templates/storefront/src/components/back-to-top.tsx +26 -0
- package/templates/storefront/src/components/breadcrumbs.tsx +26 -0
- package/templates/storefront/src/components/cart-view.tsx +72 -16
- package/templates/storefront/src/components/cms-content.tsx +75 -0
- package/templates/storefront/src/components/form-ux.tsx +47 -0
- package/templates/storefront/src/components/free-shipping-progress.tsx +35 -0
- package/templates/storefront/src/components/lead-form.tsx +203 -0
- package/templates/storefront/src/components/newsletter-action.tsx +66 -0
- package/templates/storefront/src/components/newsletter-signup.tsx +107 -0
- package/templates/storefront/src/components/product-gallery.tsx +64 -0
- package/templates/storefront/src/components/product-reviews.tsx +150 -0
- package/templates/storefront/src/components/quantity-stepper.tsx +41 -0
- package/templates/storefront/src/components/quote-request-form.tsx +212 -0
- package/templates/storefront/src/components/share-button.tsx +63 -0
- package/templates/storefront/src/components/storefront-header.tsx +72 -18
- package/templates/storefront/src/components/tier-progress-hint.tsx +32 -0
- package/templates/storefront/src/components/toast-provider.tsx +92 -0
- package/templates/storefront/src/components/variant-price-display.tsx +31 -0
- package/templates/storefront/src/components/wishlist-button.tsx +88 -0
- package/templates/storefront/src/lib/core/account.ts +3 -0
- package/templates/storefront/src/lib/core/cms-contract.ts +35 -0
- package/templates/storefront/src/lib/core/cms.ts +71 -11
- package/templates/storefront/src/lib/core/form-ux.ts +57 -0
- package/templates/storefront/src/lib/core/newsletter.ts +53 -0
- package/templates/storefront/src/lib/core/reviews.ts +40 -0
- package/templates/storefront/src/lib/core/share.ts +57 -0
- package/templates/storefront/src/lib/core/wishlist.ts +88 -0
- package/templates/storefront/src/lib/storefront-routes.ts +33 -0
- package/templates/storefront/src/proxy.ts +25 -28
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { FormEvent, useEffect, useState } from "react"
|
|
4
|
+
import { submitLead, type LeadFieldDefinition, type LeadFormDefinition } from "@/lib/core/leads"
|
|
5
|
+
import { useToast } from "@/components/toast-provider"
|
|
6
|
+
|
|
7
|
+
type Props = {
|
|
8
|
+
locale: "hu" | "en"
|
|
9
|
+
form: LeadFormDefinition
|
|
10
|
+
spamProtection: {
|
|
11
|
+
honeypot_field: string
|
|
12
|
+
turnstile: { enabled: boolean; site_key?: string | null }
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function LeadForm({ locale, form, spamProtection }: Props) {
|
|
17
|
+
const english = locale === "en"
|
|
18
|
+
const { showToast } = useToast()
|
|
19
|
+
const [submitting, setSubmitting] = useState(false)
|
|
20
|
+
const [turnstileToken, setTurnstileToken] = useState("")
|
|
21
|
+
const [startedAt] = useState(() => new Date().toISOString())
|
|
22
|
+
const turnstileCallback = "hszrdmLeadTurnstileSuccess"
|
|
23
|
+
const turnstileExpiredCallback = "hszrdmLeadTurnstileExpired"
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
if (!spamProtection.turnstile.enabled || !spamProtection.turnstile.site_key) return
|
|
27
|
+
const target = window as unknown as Record<string, unknown>
|
|
28
|
+
target[turnstileCallback] = (token: string) => setTurnstileToken(token)
|
|
29
|
+
target[turnstileExpiredCallback] = () => setTurnstileToken("")
|
|
30
|
+
if (!document.querySelector('script[data-hszrdm-turnstile="true"]')) {
|
|
31
|
+
const script = document.createElement("script")
|
|
32
|
+
script.src = "https://challenges.cloudflare.com/turnstile/v0/api.js"
|
|
33
|
+
script.async = true
|
|
34
|
+
script.defer = true
|
|
35
|
+
script.dataset.hszrdmTurnstile = "true"
|
|
36
|
+
document.head.appendChild(script)
|
|
37
|
+
}
|
|
38
|
+
return () => {
|
|
39
|
+
delete target[turnstileCallback]
|
|
40
|
+
delete target[turnstileExpiredCallback]
|
|
41
|
+
}
|
|
42
|
+
}, [spamProtection.turnstile.enabled, spamProtection.turnstile.site_key])
|
|
43
|
+
|
|
44
|
+
async function onSubmit(event: FormEvent<HTMLFormElement>) {
|
|
45
|
+
event.preventDefault()
|
|
46
|
+
if (submitting) return
|
|
47
|
+
const element = event.currentTarget
|
|
48
|
+
const data = new FormData(element)
|
|
49
|
+
const fields: Record<string, unknown> = {}
|
|
50
|
+
|
|
51
|
+
for (const field of form.fields) {
|
|
52
|
+
const raw = data.get(`field_${field.key}`)
|
|
53
|
+
if (field.type === "boolean") fields[field.key] = raw === "on"
|
|
54
|
+
else if (field.type === "number" && raw !== null && String(raw).trim()) fields[field.key] = Number(raw)
|
|
55
|
+
else if (raw !== null && String(raw).trim()) fields[field.key] = String(raw).trim()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
setSubmitting(true)
|
|
59
|
+
try {
|
|
60
|
+
const pageUrl = typeof window !== "undefined" ? window.location.href : undefined
|
|
61
|
+
const referrer = typeof document !== "undefined" ? document.referrer || undefined : undefined
|
|
62
|
+
await submitLead({
|
|
63
|
+
form_key: form.key,
|
|
64
|
+
name: value(data, "name"),
|
|
65
|
+
email: value(data, "email"),
|
|
66
|
+
phone: value(data, "phone"),
|
|
67
|
+
company: value(data, "company"),
|
|
68
|
+
message: value(data, "message"),
|
|
69
|
+
fields,
|
|
70
|
+
source: "storefront_contact",
|
|
71
|
+
page_url: pageUrl,
|
|
72
|
+
referrer,
|
|
73
|
+
privacy_consent: data.get("privacy_consent") === "on",
|
|
74
|
+
form_started_at: startedAt,
|
|
75
|
+
turnstile_token: turnstileToken || undefined,
|
|
76
|
+
[spamProtection.honeypot_field]: value(data, spamProtection.honeypot_field) || "",
|
|
77
|
+
})
|
|
78
|
+
element.reset()
|
|
79
|
+
setTurnstileToken("")
|
|
80
|
+
showToast({
|
|
81
|
+
tone: "success",
|
|
82
|
+
message: english ? "Your message has been sent." : "Az üzenetedet elküldtük.",
|
|
83
|
+
})
|
|
84
|
+
} catch (cause) {
|
|
85
|
+
showToast({
|
|
86
|
+
tone: "error",
|
|
87
|
+
message: cause instanceof Error
|
|
88
|
+
? cause.message
|
|
89
|
+
: (english ? "The message could not be sent." : "Az üzenetet nem sikerült elküldeni."),
|
|
90
|
+
})
|
|
91
|
+
} finally {
|
|
92
|
+
setSubmitting(false)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const required = new Set(form.required_standard_fields)
|
|
97
|
+
const turnstileRequired = spamProtection.turnstile.enabled && Boolean(spamProtection.turnstile.site_key)
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<form className="panel stack lead-form" onSubmit={onSubmit}>
|
|
101
|
+
<div className="form-grid">
|
|
102
|
+
<StandardField name="name" label={english ? "Name" : "Név"} required={required.has("name")} />
|
|
103
|
+
<StandardField name="email" label={english ? "Email address" : "E-mail-cím"} type="email" required={required.has("email")} />
|
|
104
|
+
<StandardField name="phone" label={english ? "Phone" : "Telefonszám"} type="tel" required={required.has("phone")} />
|
|
105
|
+
<StandardField name="company" label={english ? "Company" : "Cég"} required={required.has("company")} />
|
|
106
|
+
</div>
|
|
107
|
+
|
|
108
|
+
<div>
|
|
109
|
+
<label className="field-label" htmlFor="lead-message">{english ? "Message" : "Üzenet"}{required.has("message") ? <span className="required-mark">*</span> : null}</label>
|
|
110
|
+
<textarea id="lead-message" name="message" rows={6} required={required.has("message")} />
|
|
111
|
+
</div>
|
|
112
|
+
|
|
113
|
+
{form.fields.length ? (
|
|
114
|
+
<div className="form-grid">
|
|
115
|
+
{form.fields.map((field) => <LeadFieldControl field={field} key={field.key} />)}
|
|
116
|
+
</div>
|
|
117
|
+
) : null}
|
|
118
|
+
|
|
119
|
+
<div className="honeypot-field" aria-hidden="true">
|
|
120
|
+
<label htmlFor={`lead-${spamProtection.honeypot_field}`}>Website</label>
|
|
121
|
+
<input id={`lead-${spamProtection.honeypot_field}`} name={spamProtection.honeypot_field} type="text" tabIndex={-1} autoComplete="off" />
|
|
122
|
+
</div>
|
|
123
|
+
|
|
124
|
+
{form.privacy.required ? (
|
|
125
|
+
<label className="checkbox-row">
|
|
126
|
+
<input name="privacy_consent" type="checkbox" required />
|
|
127
|
+
<span>
|
|
128
|
+
{form.privacy.label}
|
|
129
|
+
{form.privacy.url ? <> <a href={form.privacy.url} target="_blank" rel="noreferrer">{english ? "Details" : "Részletek"}</a>.</> : null}
|
|
130
|
+
</span>
|
|
131
|
+
</label>
|
|
132
|
+
) : null}
|
|
133
|
+
|
|
134
|
+
{turnstileRequired ? (
|
|
135
|
+
<div
|
|
136
|
+
className="cf-turnstile"
|
|
137
|
+
data-sitekey={spamProtection.turnstile.site_key || undefined}
|
|
138
|
+
data-callback={turnstileCallback}
|
|
139
|
+
data-expired-callback={turnstileExpiredCallback}
|
|
140
|
+
/>
|
|
141
|
+
) : null}
|
|
142
|
+
|
|
143
|
+
<button className="button button-large" type="submit" disabled={submitting || (turnstileRequired && !turnstileToken)}>
|
|
144
|
+
{submitting ? (english ? "Sending…" : "Küldés…") : (english ? "Send message" : "Üzenet küldése")}
|
|
145
|
+
</button>
|
|
146
|
+
</form>
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function StandardField({ name, label, type = "text", required = false }: { name: string; label: string; type?: string; required?: boolean }) {
|
|
151
|
+
const id = `lead-${name}`
|
|
152
|
+
return (
|
|
153
|
+
<div>
|
|
154
|
+
<label className="field-label" htmlFor={id}>{label}{required ? <span className="required-mark">*</span> : null}</label>
|
|
155
|
+
<input id={id} name={name} type={type} required={required} />
|
|
156
|
+
</div>
|
|
157
|
+
)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function LeadFieldControl({ field }: { field: LeadFieldDefinition }) {
|
|
161
|
+
const id = `lead-field-${field.key}`
|
|
162
|
+
const name = `field_${field.key}`
|
|
163
|
+
if (field.type === "boolean") {
|
|
164
|
+
return (
|
|
165
|
+
<label className="checkbox-row" htmlFor={id}>
|
|
166
|
+
<input id={id} name={name} type="checkbox" required={field.required} />
|
|
167
|
+
<span>{field.label}</span>
|
|
168
|
+
</label>
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
if (field.type === "textarea") {
|
|
172
|
+
return (
|
|
173
|
+
<div>
|
|
174
|
+
<label className="field-label" htmlFor={id}>{field.label}{field.required ? <span className="required-mark">*</span> : null}</label>
|
|
175
|
+
<textarea id={id} name={name} rows={4} required={field.required} />
|
|
176
|
+
</div>
|
|
177
|
+
)
|
|
178
|
+
}
|
|
179
|
+
if (field.type === "select") {
|
|
180
|
+
return (
|
|
181
|
+
<div>
|
|
182
|
+
<label className="field-label" htmlFor={id}>{field.label}{field.required ? <span className="required-mark">*</span> : null}</label>
|
|
183
|
+
<select id={id} name={name} required={field.required} defaultValue="">
|
|
184
|
+
<option value="" disabled={field.required}>—</option>
|
|
185
|
+
{(field.options || []).map((option) => <option value={option} key={option}>{option}</option>)}
|
|
186
|
+
</select>
|
|
187
|
+
</div>
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
const type = field.type === "phone" ? "tel" : field.type
|
|
191
|
+
return (
|
|
192
|
+
<div>
|
|
193
|
+
<label className="field-label" htmlFor={id}>{field.label}{field.required ? <span className="required-mark">*</span> : null}</label>
|
|
194
|
+
<input id={id} name={name} type={type} required={field.required} />
|
|
195
|
+
</div>
|
|
196
|
+
)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function value(data: FormData, key: string) {
|
|
200
|
+
const raw = data.get(key)
|
|
201
|
+
const result = typeof raw === "string" ? raw.trim() : ""
|
|
202
|
+
return result || undefined
|
|
203
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState } from "react"
|
|
4
|
+
import { confirmNewsletter, unsubscribeNewsletter } from "@/lib/core/newsletter"
|
|
5
|
+
|
|
6
|
+
type Props = {
|
|
7
|
+
action: "confirm" | "unsubscribe"
|
|
8
|
+
token: string
|
|
9
|
+
locale: "hu" | "en"
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function NewsletterAction({ action, token, locale }: Props) {
|
|
13
|
+
const english = locale === "en"
|
|
14
|
+
const [status, setStatus] = useState<"idle" | "loading" | "success" | "error">("idle")
|
|
15
|
+
const [message, setMessage] = useState("")
|
|
16
|
+
|
|
17
|
+
const run = async () => {
|
|
18
|
+
if (!token || status === "loading" || status === "success") return
|
|
19
|
+
setStatus("loading")
|
|
20
|
+
setMessage("")
|
|
21
|
+
try {
|
|
22
|
+
const response = action === "confirm"
|
|
23
|
+
? await confirmNewsletter(token)
|
|
24
|
+
: await unsubscribeNewsletter(token)
|
|
25
|
+
setStatus("success")
|
|
26
|
+
setMessage(response.message)
|
|
27
|
+
} catch (cause) {
|
|
28
|
+
setStatus("error")
|
|
29
|
+
setMessage(cause instanceof Error ? cause.message : (english ? "The operation failed." : "A művelet nem sikerült."))
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
if (action === "confirm" && token) void run()
|
|
35
|
+
// The confirmation link intentionally performs the POST only in the browser.
|
|
36
|
+
// Server-side page fetching must never mutate newsletter state.
|
|
37
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
38
|
+
}, [action, token])
|
|
39
|
+
|
|
40
|
+
if (!token) {
|
|
41
|
+
return <div className="empty-state">{english ? "The link is invalid." : "A link érvénytelen."}</div>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (status === "success") {
|
|
45
|
+
return <div className="empty-state">{message}</div>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (status === "error") {
|
|
49
|
+
return <div className="empty-state"><p>{message}</p></div>
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (action === "confirm") {
|
|
53
|
+
return <div className="empty-state">{english ? "Confirming your subscription…" : "Feliratkozás megerősítése…"}</div>
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<div className="empty-state">
|
|
58
|
+
<p>{english ? "Do you want to unsubscribe from the newsletter?" : "Biztosan le szeretnél iratkozni a hírlevélről?"}</p>
|
|
59
|
+
<button type="button" className="button button-large" onClick={() => void run()} disabled={status === "loading"}>
|
|
60
|
+
{status === "loading"
|
|
61
|
+
? (english ? "Unsubscribing…" : "Leiratkozás…")
|
|
62
|
+
: (english ? "Unsubscribe" : "Leiratkozom")}
|
|
63
|
+
</button>
|
|
64
|
+
</div>
|
|
65
|
+
)
|
|
66
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { FormEvent, useEffect, useState } from "react"
|
|
4
|
+
import { newsletterConfig, subscribeNewsletter, type NewsletterConfig } from "@/lib/core/newsletter"
|
|
5
|
+
import { useToast } from "@/components/toast-provider"
|
|
6
|
+
|
|
7
|
+
type Props = {
|
|
8
|
+
locale: "hu" | "en"
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function NewsletterSignup({ locale }: Props) {
|
|
12
|
+
const english = locale === "en"
|
|
13
|
+
const { showToast } = useToast()
|
|
14
|
+
const [config, setConfig] = useState<NewsletterConfig | null>(null)
|
|
15
|
+
const [email, setEmail] = useState("")
|
|
16
|
+
const [consent, setConsent] = useState(false)
|
|
17
|
+
const [submitting, setSubmitting] = useState(false)
|
|
18
|
+
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
let active = true
|
|
21
|
+
void newsletterConfig()
|
|
22
|
+
.then((value) => {
|
|
23
|
+
if (active) setConfig(value)
|
|
24
|
+
})
|
|
25
|
+
.catch(() => {
|
|
26
|
+
if (active) setConfig(null)
|
|
27
|
+
})
|
|
28
|
+
return () => {
|
|
29
|
+
active = false
|
|
30
|
+
}
|
|
31
|
+
}, [])
|
|
32
|
+
|
|
33
|
+
if (!config?.enabled) return null
|
|
34
|
+
|
|
35
|
+
async function onSubmit(event: FormEvent<HTMLFormElement>) {
|
|
36
|
+
event.preventDefault()
|
|
37
|
+
if (!email.trim() || !consent || submitting) return
|
|
38
|
+
setSubmitting(true)
|
|
39
|
+
try {
|
|
40
|
+
const url = typeof window !== "undefined" ? new URL(window.location.href) : null
|
|
41
|
+
const utm = url
|
|
42
|
+
? Object.fromEntries(
|
|
43
|
+
["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"]
|
|
44
|
+
.map((key) => [key, url.searchParams.get(key) || ""])
|
|
45
|
+
.filter(([, value]) => value),
|
|
46
|
+
)
|
|
47
|
+
: undefined
|
|
48
|
+
const response = await subscribeNewsletter({
|
|
49
|
+
email: email.trim(),
|
|
50
|
+
consent: true,
|
|
51
|
+
source: "storefront_footer",
|
|
52
|
+
page_url: url?.toString(),
|
|
53
|
+
utm,
|
|
54
|
+
})
|
|
55
|
+
setEmail("")
|
|
56
|
+
setConsent(false)
|
|
57
|
+
showToast({ tone: "success", message: response.message })
|
|
58
|
+
} catch (cause) {
|
|
59
|
+
showToast({
|
|
60
|
+
tone: "error",
|
|
61
|
+
message: cause instanceof Error
|
|
62
|
+
? cause.message
|
|
63
|
+
: (english ? "Subscription failed." : "A feliratkozás nem sikerült."),
|
|
64
|
+
})
|
|
65
|
+
} finally {
|
|
66
|
+
setSubmitting(false)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<section className="footer-newsletter stack-sm" aria-labelledby="newsletter-title">
|
|
72
|
+
<div>
|
|
73
|
+
<p className="eyebrow">{english ? "Newsletter" : "Hírlevél"}</p>
|
|
74
|
+
<h2 id="newsletter-title">{english ? "Stay in the loop" : "Maradj képben"}</h2>
|
|
75
|
+
<p className="muted small">
|
|
76
|
+
{config.double_opt_in
|
|
77
|
+
? (english ? "We will send a confirmation link before subscribing you." : "Feliratkozás előtt megerősítő linket küldünk.")
|
|
78
|
+
: (english ? "Get updates and useful offers by email." : "Kérj e-mailes híreket és hasznos ajánlatokat.")}
|
|
79
|
+
</p>
|
|
80
|
+
</div>
|
|
81
|
+
<form className="newsletter-form" onSubmit={onSubmit}>
|
|
82
|
+
<label className="field-label" htmlFor="newsletter-email">{english ? "Email address" : "E-mail-cím"}</label>
|
|
83
|
+
<div className="newsletter-form-row">
|
|
84
|
+
<input
|
|
85
|
+
id="newsletter-email"
|
|
86
|
+
type="email"
|
|
87
|
+
autoComplete="email"
|
|
88
|
+
required
|
|
89
|
+
value={email}
|
|
90
|
+
onChange={(event) => setEmail(event.target.value)}
|
|
91
|
+
placeholder="email@example.com"
|
|
92
|
+
/>
|
|
93
|
+
<button className="button" type="submit" disabled={submitting || !consent || !email.trim()}>
|
|
94
|
+
{submitting ? (english ? "Sending…" : "Küldés…") : (english ? "Subscribe" : "Feliratkozom")}
|
|
95
|
+
</button>
|
|
96
|
+
</div>
|
|
97
|
+
<label className="checkbox-row">
|
|
98
|
+
<input type="checkbox" checked={consent} onChange={(event) => setConsent(event.target.checked)} required />
|
|
99
|
+
<span>
|
|
100
|
+
{english ? "I consent to receiving newsletter emails." : "Hozzájárulok a hírlevél e-mailek fogadásához."}
|
|
101
|
+
{config.consent_url ? <> <a href={config.consent_url} target="_blank" rel="noreferrer">{english ? "Privacy information" : "Adatkezelési tájékoztató"}</a>.</> : null}
|
|
102
|
+
</span>
|
|
103
|
+
</label>
|
|
104
|
+
</form>
|
|
105
|
+
</section>
|
|
106
|
+
)
|
|
107
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useEffect, useMemo, useState } from "react"
|
|
4
|
+
|
|
5
|
+
export type ProductGalleryImage = {
|
|
6
|
+
id?: string
|
|
7
|
+
url: string
|
|
8
|
+
alt?: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function ProductGallery({
|
|
12
|
+
title,
|
|
13
|
+
images,
|
|
14
|
+
emptyLabel = "Nincs kép",
|
|
15
|
+
galleryLabel = "Termékképek",
|
|
16
|
+
}: {
|
|
17
|
+
title: string
|
|
18
|
+
images: ProductGalleryImage[]
|
|
19
|
+
emptyLabel?: string
|
|
20
|
+
galleryLabel?: string
|
|
21
|
+
}) {
|
|
22
|
+
const normalized = useMemo(() => images.filter((image) => image.url), [images])
|
|
23
|
+
const [activeUrl, setActiveUrl] = useState(normalized[0]?.url || "")
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const listener = (event: Event) => {
|
|
27
|
+
const custom = event as CustomEvent<{ imageUrl?: string }>
|
|
28
|
+
const imageUrl = String(custom.detail?.imageUrl || "").trim()
|
|
29
|
+
if (imageUrl) setActiveUrl(imageUrl)
|
|
30
|
+
}
|
|
31
|
+
window.addEventListener("hszrdm:product-variant-change", listener)
|
|
32
|
+
return () => window.removeEventListener("hszrdm:product-variant-change", listener)
|
|
33
|
+
}, [])
|
|
34
|
+
|
|
35
|
+
const allImages = activeUrl && !normalized.some((image) => image.url === activeUrl)
|
|
36
|
+
? [{ url: activeUrl, alt: title }, ...normalized]
|
|
37
|
+
: normalized
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<div className="product-gallery">
|
|
41
|
+
<div className="product-main-image">
|
|
42
|
+
{activeUrl
|
|
43
|
+
? <img src={activeUrl} alt={title} />
|
|
44
|
+
: <span className="media-placeholder">{emptyLabel}</span>}
|
|
45
|
+
</div>
|
|
46
|
+
{allImages.length > 1 ? (
|
|
47
|
+
<div className="thumbnail-row" aria-label={galleryLabel}>
|
|
48
|
+
{allImages.slice(0, 8).map((image, index) => (
|
|
49
|
+
<button
|
|
50
|
+
type="button"
|
|
51
|
+
className={`thumbnail-button${image.url === activeUrl ? " is-active" : ""}`}
|
|
52
|
+
key={`${image.id || image.url}:${index}`}
|
|
53
|
+
aria-label={`${title} – ${index + 1}`}
|
|
54
|
+
aria-pressed={image.url === activeUrl}
|
|
55
|
+
onClick={() => setActiveUrl(image.url)}
|
|
56
|
+
>
|
|
57
|
+
<img src={image.url} alt="" loading="lazy" />
|
|
58
|
+
</button>
|
|
59
|
+
))}
|
|
60
|
+
</div>
|
|
61
|
+
) : null}
|
|
62
|
+
</div>
|
|
63
|
+
)
|
|
64
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { FormEvent, useEffect, useState } from "react"
|
|
4
|
+
import {
|
|
5
|
+
listProductReviews,
|
|
6
|
+
submitProductReview,
|
|
7
|
+
type ProductReview,
|
|
8
|
+
type ProductReviewSummary,
|
|
9
|
+
} from "@/lib/core/reviews"
|
|
10
|
+
import { useToast } from "@/components/toast-provider"
|
|
11
|
+
|
|
12
|
+
type Props = {
|
|
13
|
+
productId: string
|
|
14
|
+
locale: "hu" | "en"
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const EMPTY_SUMMARY: ProductReviewSummary = { average: 0, count: 0 }
|
|
18
|
+
|
|
19
|
+
export function ProductReviews({ productId, locale }: Props) {
|
|
20
|
+
const english = locale === "en"
|
|
21
|
+
const { showToast } = useToast()
|
|
22
|
+
const [reviews, setReviews] = useState<ProductReview[]>([])
|
|
23
|
+
const [summary, setSummary] = useState<ProductReviewSummary>(EMPTY_SUMMARY)
|
|
24
|
+
const [loading, setLoading] = useState(true)
|
|
25
|
+
const [loadError, setLoadError] = useState("")
|
|
26
|
+
const [rating, setRating] = useState(5)
|
|
27
|
+
const [body, setBody] = useState("")
|
|
28
|
+
const [submitting, setSubmitting] = useState(false)
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
let active = true
|
|
32
|
+
setLoading(true)
|
|
33
|
+
setLoadError("")
|
|
34
|
+
void listProductReviews(productId, { limit: 20 })
|
|
35
|
+
.then((result) => {
|
|
36
|
+
if (!active) return
|
|
37
|
+
setReviews(result.reviews)
|
|
38
|
+
setSummary(result.summary)
|
|
39
|
+
})
|
|
40
|
+
.catch((cause) => {
|
|
41
|
+
if (!active) return
|
|
42
|
+
setLoadError(cause instanceof Error ? cause.message : (english ? "Reviews are unavailable." : "A vélemények nem érhetők el."))
|
|
43
|
+
})
|
|
44
|
+
.finally(() => {
|
|
45
|
+
if (active) setLoading(false)
|
|
46
|
+
})
|
|
47
|
+
return () => {
|
|
48
|
+
active = false
|
|
49
|
+
}
|
|
50
|
+
}, [english, productId])
|
|
51
|
+
|
|
52
|
+
async function onSubmit(event: FormEvent<HTMLFormElement>) {
|
|
53
|
+
event.preventDefault()
|
|
54
|
+
if (!body.trim() || submitting) return
|
|
55
|
+
setSubmitting(true)
|
|
56
|
+
try {
|
|
57
|
+
await submitProductReview(productId, { rating, body: body.trim() })
|
|
58
|
+
setBody("")
|
|
59
|
+
setRating(5)
|
|
60
|
+
showToast({
|
|
61
|
+
tone: "success",
|
|
62
|
+
message: english
|
|
63
|
+
? "Thanks! Your review was submitted for moderation."
|
|
64
|
+
: "Köszönjük! A véleményed moderálásra elküldtük.",
|
|
65
|
+
})
|
|
66
|
+
} catch (cause) {
|
|
67
|
+
showToast({
|
|
68
|
+
tone: "error",
|
|
69
|
+
message: cause instanceof Error
|
|
70
|
+
? cause.message
|
|
71
|
+
: (english ? "The review could not be submitted." : "A véleményt nem sikerült elküldeni."),
|
|
72
|
+
})
|
|
73
|
+
} finally {
|
|
74
|
+
setSubmitting(false)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<section className="stack product-reviews" aria-labelledby="product-reviews-title">
|
|
80
|
+
<div className="section-heading">
|
|
81
|
+
<div>
|
|
82
|
+
<p className="eyebrow">{english ? "Customer feedback" : "Vásárlói visszajelzések"}</p>
|
|
83
|
+
<h2 id="product-reviews-title">{english ? "Reviews" : "Vélemények"}</h2>
|
|
84
|
+
</div>
|
|
85
|
+
<div className="review-summary" aria-label={english ? "Review summary" : "Értékelések összesítése"}>
|
|
86
|
+
<strong>{summary.count ? summary.average.toFixed(1) : "–"} / 5</strong>
|
|
87
|
+
<span>{summary.count} {english ? "reviews" : "vélemény"}</span>
|
|
88
|
+
</div>
|
|
89
|
+
</div>
|
|
90
|
+
|
|
91
|
+
<form className="panel stack-sm review-form" onSubmit={onSubmit}>
|
|
92
|
+
<div>
|
|
93
|
+
<label className="field-label" htmlFor="review-rating">{english ? "Rating" : "Értékelés"}</label>
|
|
94
|
+
<select id="review-rating" value={rating} onChange={(event) => setRating(Number(event.target.value))}>
|
|
95
|
+
{[5, 4, 3, 2, 1].map((value) => (
|
|
96
|
+
<option value={value} key={value}>{value} / 5</option>
|
|
97
|
+
))}
|
|
98
|
+
</select>
|
|
99
|
+
</div>
|
|
100
|
+
<div>
|
|
101
|
+
<label className="field-label" htmlFor="review-body">{english ? "Your review" : "Véleményed"}</label>
|
|
102
|
+
<textarea
|
|
103
|
+
id="review-body"
|
|
104
|
+
rows={5}
|
|
105
|
+
maxLength={5000}
|
|
106
|
+
required
|
|
107
|
+
value={body}
|
|
108
|
+
onChange={(event) => setBody(event.target.value)}
|
|
109
|
+
placeholder={english ? "Share your experience with this product." : "Írd le a tapasztalatodat a termékről."}
|
|
110
|
+
/>
|
|
111
|
+
<span className="field-hint">
|
|
112
|
+
{english ? "You must be signed in to submit a review." : "Vélemény beküldéséhez be kell jelentkezned."}
|
|
113
|
+
</span>
|
|
114
|
+
</div>
|
|
115
|
+
<button className="button" type="submit" disabled={submitting || !body.trim()}>
|
|
116
|
+
{submitting
|
|
117
|
+
? (english ? "Submitting…" : "Küldés…")
|
|
118
|
+
: (english ? "Submit review" : "Vélemény elküldése")}
|
|
119
|
+
</button>
|
|
120
|
+
</form>
|
|
121
|
+
|
|
122
|
+
{loading ? <div className="empty-state">{english ? "Loading reviews…" : "Vélemények betöltése…"}</div> : null}
|
|
123
|
+
{!loading && loadError ? <div className="empty-state">{loadError}</div> : null}
|
|
124
|
+
{!loading && !loadError && !reviews.length ? (
|
|
125
|
+
<div className="empty-state">{english ? "No approved reviews yet." : "Még nincs jóváhagyott vélemény."}</div>
|
|
126
|
+
) : null}
|
|
127
|
+
{reviews.length ? (
|
|
128
|
+
<div className="review-list">
|
|
129
|
+
{reviews.map((review) => (
|
|
130
|
+
<article className="panel review-card" key={review.id}>
|
|
131
|
+
<div className="review-card-heading">
|
|
132
|
+
<div>
|
|
133
|
+
<strong>{review.customer_name}</strong>
|
|
134
|
+
{review.verified_purchase ? <span className="verified-badge">{english ? "Verified purchase" : "Ellenőrzött vásárlás"}</span> : null}
|
|
135
|
+
</div>
|
|
136
|
+
<span aria-label={`${review.rating} / 5`}>{"★".repeat(review.rating)}{"☆".repeat(Math.max(0, 5 - review.rating))}</span>
|
|
137
|
+
</div>
|
|
138
|
+
<p>{review.body}</p>
|
|
139
|
+
{review.created_at ? (
|
|
140
|
+
<time className="muted small" dateTime={review.created_at}>
|
|
141
|
+
{new Intl.DateTimeFormat(english ? "en-US" : "hu-HU", { dateStyle: "medium" }).format(new Date(review.created_at))}
|
|
142
|
+
</time>
|
|
143
|
+
) : null}
|
|
144
|
+
</article>
|
|
145
|
+
))}
|
|
146
|
+
</div>
|
|
147
|
+
) : null}
|
|
148
|
+
</section>
|
|
149
|
+
)
|
|
150
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
export function QuantityStepper({
|
|
4
|
+
value,
|
|
5
|
+
onChange,
|
|
6
|
+
min = 1,
|
|
7
|
+
max = 999,
|
|
8
|
+
disabled = false,
|
|
9
|
+
label = "Mennyiség",
|
|
10
|
+
decreaseLabel,
|
|
11
|
+
increaseLabel,
|
|
12
|
+
}: {
|
|
13
|
+
value: number
|
|
14
|
+
onChange: (value: number) => void
|
|
15
|
+
min?: number
|
|
16
|
+
max?: number
|
|
17
|
+
disabled?: boolean
|
|
18
|
+
label?: string
|
|
19
|
+
decreaseLabel?: string
|
|
20
|
+
increaseLabel?: string
|
|
21
|
+
}) {
|
|
22
|
+
const clamped = Math.max(min, Math.min(max, Math.trunc(value || min)))
|
|
23
|
+
const set = (next: number) => onChange(Math.max(min, Math.min(max, Math.trunc(next || min))))
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<div className="quantity-stepper" role="group" aria-label={label}>
|
|
27
|
+
<button type="button" disabled={disabled || clamped <= min} aria-label={decreaseLabel || `${label}: csökkentés`} onClick={() => set(clamped - 1)}>−</button>
|
|
28
|
+
<input
|
|
29
|
+
type="number"
|
|
30
|
+
inputMode="numeric"
|
|
31
|
+
min={min}
|
|
32
|
+
max={max}
|
|
33
|
+
value={clamped}
|
|
34
|
+
disabled={disabled}
|
|
35
|
+
aria-label={label}
|
|
36
|
+
onChange={(event) => set(Number(event.target.value))}
|
|
37
|
+
/>
|
|
38
|
+
<button type="button" disabled={disabled || clamped >= max} aria-label={increaseLabel || `${label}: növelés`} onClick={() => set(clamped + 1)}>+</button>
|
|
39
|
+
</div>
|
|
40
|
+
)
|
|
41
|
+
}
|