@hszrdm/create-commerce 1.2.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/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 +2 -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/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 +11 -2
- 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/cms-content.tsx +75 -0
- package/templates/storefront/src/components/lead-form.tsx +203 -0
- package/templates/storefront/src/components/newsletter-signup.tsx +107 -0
- package/templates/storefront/src/components/product-reviews.tsx +150 -0
- package/templates/storefront/src/components/quote-request-form.tsx +212 -0
- package/templates/storefront/src/lib/core/cms.ts +5 -4
- package/templates/storefront/src/lib/storefront-routes.ts +30 -0
- package/templates/storefront/src/proxy.ts +22 -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,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,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,212 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { FormEvent, useState } from "react"
|
|
4
|
+
import { submitQuoteRequest } from "@/lib/core/quotes"
|
|
5
|
+
import { useToast } from "@/components/toast-provider"
|
|
6
|
+
|
|
7
|
+
type ExtraField = {
|
|
8
|
+
key: string
|
|
9
|
+
label: string
|
|
10
|
+
type: "text" | "textarea" | "number" | "boolean" | "select"
|
|
11
|
+
required: boolean
|
|
12
|
+
options?: string[]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type Props = {
|
|
16
|
+
locale: "hu" | "en"
|
|
17
|
+
config: {
|
|
18
|
+
enabled: boolean
|
|
19
|
+
order_types: string[]
|
|
20
|
+
extra_fields: Array<Record<string, unknown>>
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function QuoteRequestForm({ locale, config }: Props) {
|
|
25
|
+
const english = locale === "en"
|
|
26
|
+
const { showToast } = useToast()
|
|
27
|
+
const [submitting, setSubmitting] = useState(false)
|
|
28
|
+
const extraFields = config.extra_fields.map(normalizeExtraField).filter((field): field is ExtraField => Boolean(field))
|
|
29
|
+
|
|
30
|
+
if (!config.enabled) {
|
|
31
|
+
return <div className="empty-state">{english ? "Quote requests are currently unavailable." : "Az ajánlatkérés jelenleg nem elérhető."}</div>
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function onSubmit(event: FormEvent<HTMLFormElement>) {
|
|
35
|
+
event.preventDefault()
|
|
36
|
+
if (submitting) return
|
|
37
|
+
const form = event.currentTarget
|
|
38
|
+
const data = new FormData(form)
|
|
39
|
+
const orderType = String(data.get("order_type") || "one_time") === "recurring" ? "recurring" : "one_time"
|
|
40
|
+
const itemTitle = String(data.get("item_title") || "").trim()
|
|
41
|
+
const itemQuantity = Number(data.get("item_quantity") || 1)
|
|
42
|
+
const extra_fields: Record<string, unknown> = {}
|
|
43
|
+
|
|
44
|
+
for (const field of extraFields) {
|
|
45
|
+
const raw = data.get(`extra_${field.key}`)
|
|
46
|
+
if (field.type === "boolean") extra_fields[field.key] = raw === "on"
|
|
47
|
+
else if (field.type === "number" && raw !== null && String(raw).trim()) extra_fields[field.key] = Number(raw)
|
|
48
|
+
else if (raw !== null && String(raw).trim()) extra_fields[field.key] = String(raw).trim()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
setSubmitting(true)
|
|
52
|
+
try {
|
|
53
|
+
await submitQuoteRequest({
|
|
54
|
+
company_name: text(data.get("company_name")),
|
|
55
|
+
tax_number: text(data.get("tax_number")),
|
|
56
|
+
company_address: text(data.get("company_address")),
|
|
57
|
+
contact_name: String(data.get("contact_name") || "").trim(),
|
|
58
|
+
contact_email: String(data.get("contact_email") || "").trim(),
|
|
59
|
+
contact_phone: text(data.get("contact_phone")),
|
|
60
|
+
order_type: orderType,
|
|
61
|
+
recurrence: orderType === "recurring" ? text(data.get("recurrence")) : undefined,
|
|
62
|
+
note: text(data.get("note")),
|
|
63
|
+
extra_fields,
|
|
64
|
+
items: [{
|
|
65
|
+
title: itemTitle,
|
|
66
|
+
quantity: Number.isInteger(itemQuantity) && itemQuantity > 0 ? itemQuantity : 1,
|
|
67
|
+
unit_label: text(data.get("item_unit")),
|
|
68
|
+
custom_request: text(data.get("item_request")),
|
|
69
|
+
is_custom: true,
|
|
70
|
+
}],
|
|
71
|
+
})
|
|
72
|
+
form.reset()
|
|
73
|
+
showToast({
|
|
74
|
+
tone: "success",
|
|
75
|
+
message: english ? "Your quote request has been sent." : "Az ajánlatkérésedet elküldtük.",
|
|
76
|
+
})
|
|
77
|
+
} catch (cause) {
|
|
78
|
+
showToast({
|
|
79
|
+
tone: "error",
|
|
80
|
+
message: cause instanceof Error
|
|
81
|
+
? cause.message
|
|
82
|
+
: (english ? "The quote request could not be sent." : "Az ajánlatkérést nem sikerült elküldeni."),
|
|
83
|
+
})
|
|
84
|
+
} finally {
|
|
85
|
+
setSubmitting(false)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<form className="panel stack quote-request-form" onSubmit={onSubmit}>
|
|
91
|
+
<div className="form-grid">
|
|
92
|
+
<Field name="contact_name" label={english ? "Contact name" : "Kapcsolattartó neve"} required />
|
|
93
|
+
<Field name="contact_email" label={english ? "Email address" : "E-mail-cím"} type="email" required />
|
|
94
|
+
<Field name="contact_phone" label={english ? "Phone" : "Telefonszám"} type="tel" />
|
|
95
|
+
<Field name="company_name" label={english ? "Company" : "Cégnév"} />
|
|
96
|
+
<Field name="tax_number" label={english ? "Tax number" : "Adószám"} />
|
|
97
|
+
<Field name="company_address" label={english ? "Company address" : "Cég címe"} />
|
|
98
|
+
</div>
|
|
99
|
+
|
|
100
|
+
<div className="form-grid">
|
|
101
|
+
<div>
|
|
102
|
+
<label className="field-label" htmlFor="quote-order-type">{english ? "Order type" : "Rendelés típusa"}</label>
|
|
103
|
+
<select id="quote-order-type" name="order_type" defaultValue="one_time">
|
|
104
|
+
<option value="one_time">{english ? "One-time" : "Egyszeri"}</option>
|
|
105
|
+
<option value="recurring">{english ? "Recurring" : "Rendszeres"}</option>
|
|
106
|
+
</select>
|
|
107
|
+
</div>
|
|
108
|
+
<Field name="recurrence" label={english ? "Recurrence, if applicable" : "Gyakoriság, ha rendszeres"} placeholder={english ? "e.g. monthly" : "pl. havonta"} />
|
|
109
|
+
</div>
|
|
110
|
+
|
|
111
|
+
<fieldset className="form-fieldset stack-sm">
|
|
112
|
+
<legend>{english ? "Requested item" : "Ajánlatkérési tétel"}</legend>
|
|
113
|
+
<div className="form-grid">
|
|
114
|
+
<Field name="item_title" label={english ? "Item / service" : "Termék / szolgáltatás"} required />
|
|
115
|
+
<Field name="item_quantity" label={english ? "Quantity" : "Mennyiség"} type="number" min="1" defaultValue="1" required />
|
|
116
|
+
<Field name="item_unit" label={english ? "Unit" : "Egység"} placeholder={english ? "pcs, m, kg…" : "db, m, kg…"} />
|
|
117
|
+
</div>
|
|
118
|
+
<div>
|
|
119
|
+
<label className="field-label" htmlFor="quote-item-request">{english ? "Custom request" : "Egyedi igény"}</label>
|
|
120
|
+
<textarea id="quote-item-request" name="item_request" rows={4} />
|
|
121
|
+
</div>
|
|
122
|
+
</fieldset>
|
|
123
|
+
|
|
124
|
+
{extraFields.length ? (
|
|
125
|
+
<fieldset className="form-fieldset stack-sm">
|
|
126
|
+
<legend>{english ? "Additional details" : "További adatok"}</legend>
|
|
127
|
+
<div className="form-grid">
|
|
128
|
+
{extraFields.map((field) => <ExtraFieldControl field={field} key={field.key} />)}
|
|
129
|
+
</div>
|
|
130
|
+
</fieldset>
|
|
131
|
+
) : null}
|
|
132
|
+
|
|
133
|
+
<div>
|
|
134
|
+
<label className="field-label" htmlFor="quote-note">{english ? "Message" : "Megjegyzés"}</label>
|
|
135
|
+
<textarea id="quote-note" name="note" rows={5} />
|
|
136
|
+
</div>
|
|
137
|
+
|
|
138
|
+
<button className="button button-large" type="submit" disabled={submitting}>
|
|
139
|
+
{submitting ? (english ? "Sending…" : "Küldés…") : (english ? "Request a quote" : "Ajánlatot kérek")}
|
|
140
|
+
</button>
|
|
141
|
+
</form>
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function Field(props: {
|
|
146
|
+
name: string
|
|
147
|
+
label: string
|
|
148
|
+
type?: string
|
|
149
|
+
required?: boolean
|
|
150
|
+
placeholder?: string
|
|
151
|
+
min?: string
|
|
152
|
+
defaultValue?: string
|
|
153
|
+
}) {
|
|
154
|
+
const id = `quote-${props.name}`
|
|
155
|
+
return (
|
|
156
|
+
<div>
|
|
157
|
+
<label className="field-label" htmlFor={id}>{props.label}{props.required ? <span className="required-mark">*</span> : null}</label>
|
|
158
|
+
<input id={id} name={props.name} type={props.type || "text"} required={props.required} placeholder={props.placeholder} min={props.min} defaultValue={props.defaultValue} />
|
|
159
|
+
</div>
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function ExtraFieldControl({ field }: { field: ExtraField }) {
|
|
164
|
+
const id = `quote-extra-${field.key}`
|
|
165
|
+
if (field.type === "boolean") {
|
|
166
|
+
return (
|
|
167
|
+
<label className="checkbox-row" htmlFor={id}>
|
|
168
|
+
<input id={id} name={`extra_${field.key}`} type="checkbox" required={field.required} />
|
|
169
|
+
<span>{field.label}</span>
|
|
170
|
+
</label>
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
if (field.type === "textarea") {
|
|
174
|
+
return (
|
|
175
|
+
<div>
|
|
176
|
+
<label className="field-label" htmlFor={id}>{field.label}{field.required ? <span className="required-mark">*</span> : null}</label>
|
|
177
|
+
<textarea id={id} name={`extra_${field.key}`} rows={4} required={field.required} />
|
|
178
|
+
</div>
|
|
179
|
+
)
|
|
180
|
+
}
|
|
181
|
+
if (field.type === "select") {
|
|
182
|
+
return (
|
|
183
|
+
<div>
|
|
184
|
+
<label className="field-label" htmlFor={id}>{field.label}{field.required ? <span className="required-mark">*</span> : null}</label>
|
|
185
|
+
<select id={id} name={`extra_${field.key}`} required={field.required} defaultValue="">
|
|
186
|
+
<option value="" disabled={field.required}>—</option>
|
|
187
|
+
{(field.options || []).map((option) => <option value={option} key={option}>{option}</option>)}
|
|
188
|
+
</select>
|
|
189
|
+
</div>
|
|
190
|
+
)
|
|
191
|
+
}
|
|
192
|
+
return <Field name={`extra_${field.key}`} label={field.label} type={field.type === "number" ? "number" : "text"} required={field.required} />
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function normalizeExtraField(value: Record<string, unknown>): ExtraField | null {
|
|
196
|
+
const key = typeof value.key === "string" ? value.key : ""
|
|
197
|
+
const label = typeof value.label === "string" ? value.label : key
|
|
198
|
+
const type = typeof value.type === "string" ? value.type : "text"
|
|
199
|
+
if (!key || !["text", "textarea", "number", "boolean", "select"].includes(type)) return null
|
|
200
|
+
return {
|
|
201
|
+
key,
|
|
202
|
+
label,
|
|
203
|
+
type: type as ExtraField["type"],
|
|
204
|
+
required: value.required === true,
|
|
205
|
+
options: Array.isArray(value.options) ? value.options.filter((item): item is string => typeof item === "string") : undefined,
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function text(value: FormDataEntryValue | null) {
|
|
210
|
+
const result = typeof value === "string" ? value.trim() : ""
|
|
211
|
+
return result || undefined
|
|
212
|
+
}
|