create-brainerce-store 1.87.0 → 1.88.1

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.
Files changed (41) hide show
  1. package/dist/index.js +9 -2
  2. package/messages/en.json +10 -1
  3. package/messages/he.json +10 -1
  4. package/package.json +1 -1
  5. package/templates/nextjs/base/.eslintrc.json +1 -0
  6. package/templates/nextjs/base/AI-GUIDE.md +224 -221
  7. package/templates/nextjs/base/src/app/api/store/[...path]/route.ts +296 -296
  8. package/templates/nextjs/base/src/app/checkout/page.tsx +89 -6
  9. package/templates/nextjs/base/src/app/products/layout.tsx.ejs +18 -0
  10. package/templates/nextjs/base/src/components/auth/register-form.tsx +381 -375
  11. package/templates/nextjs/base/src/components/checkout/checkout-form.tsx +14 -8
  12. package/templates/nextjs/base/src/core/hooks/use-page-exists.ts +37 -0
  13. package/templates/nextjs/base/src/core/lib/add-to-cart-error.ts +27 -2
  14. package/templates/nextjs/base/src/core/lib/brainerce.server.ts +121 -121
  15. package/templates/nextjs/base/src/core/lib/coupon-error.ts +29 -0
  16. package/templates/nextjs/base/src/ui/cart/cart-bundle-offer.tsx +6 -2
  17. package/templates/nextjs/base/src/ui/cart/cart-upgrade-banner.tsx +6 -4
  18. package/templates/nextjs/base/src/ui/cart/coupon-input.tsx +138 -134
  19. package/templates/nextjs/base/src/ui/cart/tax-estimate-line.tsx +10 -3
  20. package/templates/nextjs/base/src/ui/layout/newsletter-signup.tsx +5 -1
  21. package/templates/nextjs/base/src/ui/layout/site-footer.tsx.ejs +2 -2
  22. package/templates/nextjs/base/src/ui/product/frequently-bought-together.tsx +6 -2
  23. package/templates/nextjs/base/src/ui/product/product-card.tsx +6 -2
  24. package/templates/nextjs/base/src/ui/product/product-client-section.tsx +4 -36
  25. package/templates/nextjs/designs/atelier/ui/cart/cart-bundle-offer.tsx +6 -2
  26. package/templates/nextjs/designs/atelier/ui/cart/cart-upgrade-banner.tsx +6 -4
  27. package/templates/nextjs/designs/atelier/ui/cart/coupon-input.tsx +146 -142
  28. package/templates/nextjs/designs/atelier/ui/cart/tax-estimate-line.tsx +10 -3
  29. package/templates/nextjs/designs/atelier/ui/product/frequently-bought-together.tsx +6 -2
  30. package/templates/nextjs/designs/atelier/ui/product/product-card.tsx +6 -2
  31. package/templates/nextjs/designs/atelier/ui/product/product-client-section.tsx +3 -28
  32. package/templates/nextjs/ui-canvas/cart/cart-bundle-offer.tsx +6 -2
  33. package/templates/nextjs/ui-canvas/cart/cart-upgrade-banner.tsx +6 -4
  34. package/templates/nextjs/ui-canvas/cart/coupon-input.tsx +123 -119
  35. package/templates/nextjs/ui-canvas/cart/tax-estimate-line.tsx +4 -3
  36. package/templates/nextjs/ui-canvas/layout/newsletter-signup.tsx +5 -1
  37. package/templates/nextjs/ui-canvas/layout/site-footer.tsx.ejs +2 -2
  38. package/templates/nextjs/ui-canvas/product/frequently-bought-together.tsx +6 -2
  39. package/templates/nextjs/ui-canvas/product/product-card.tsx +6 -2
  40. package/templates/nextjs/ui-canvas/product/product-client-section.tsx +3 -28
  41. package/templates/nextjs/base/src/app/products/layout.tsx +0 -18
@@ -1,375 +1,381 @@
1
- 'use client';
2
-
3
- import { useState, useMemo, useEffect } from 'react';
4
- import { useTranslations } from '@/core/lib/translations';
5
- import { cn } from '@/core/lib/utils';
6
- import { LoadingSpinner } from '@/ui/shared/loading-spinner';
7
- import { getPasswordError } from '@/core/lib/validation';
8
- import { useStoreInfo } from '@/core/providers/store-provider';
9
- import { toBirthdayNumber } from '@/core/lib/birthday';
10
- import { BirthdayPicker } from '@/components/shared/birthday-picker';
11
- import { readReferralCookie } from '@/core/lib/referral';
12
- import { useStoreCapabilities } from '@/core/providers/store-provider';
13
-
14
- interface RegisterData {
15
- firstName: string;
16
- lastName: string;
17
- email: string;
18
- password: string;
19
- acceptsMarketing: boolean;
20
- /**
21
- * Birthday month and day, never a year. Sent together or left out entirely:
22
- * one without the other is rejected with HTTP 400. Omitted rather than
23
- * nulled when the shopper leaves the field empty, because `registerCustomer()`
24
- * takes numbers only.
25
- */
26
- birthMonth?: number;
27
- birthDay?: number;
28
- /**
29
- * Loyalty referral share code, captured from a `?ref=` link that may have
30
- * been clicked days ago. Omitted when nothing was captured; never invented.
31
- */
32
- referralCode?: string;
33
- }
34
-
35
- interface RegisterFormProps {
36
- onSubmit: (data: RegisterData) => Promise<void>;
37
- error?: string | null;
38
- className?: string;
39
- }
40
-
41
- function getPasswordStrength(password: string): { label: string; color: string; width: string } {
42
- if (password.length === 0) return { label: '', color: '', width: 'w-0' };
43
- if (password.length < 8) return { label: 'tooShort', color: 'bg-destructive', width: 'w-1/4' };
44
-
45
- let score = 0;
46
- if (password.length >= 8) score++;
47
- if (/[A-Z]/.test(password)) score++;
48
- if (/[0-9]/.test(password)) score++;
49
- if (/[^A-Za-z0-9]/.test(password)) score++;
50
-
51
- if (score <= 1) return { label: 'weak', color: 'bg-orange-500', width: 'w-1/3' };
52
- if (score <= 2) return { label: 'fair', color: 'bg-yellow-500', width: 'w-1/2' };
53
- if (score <= 3) return { label: 'good', color: 'bg-primary', width: 'w-3/4' };
54
- return { label: 'strong', color: 'bg-green-500', width: 'w-full' };
55
- }
56
-
57
- export function RegisterForm({ onSubmit, error, className }: RegisterFormProps) {
58
- const t = useTranslations('auth');
59
- const tf = useTranslations('checkoutForm');
60
- const tc = useTranslations('common');
61
- const { storeInfo } = useStoreInfo();
62
- const [firstName, setFirstName] = useState('');
63
- const [lastName, setLastName] = useState('');
64
- const [email, setEmail] = useState('');
65
- const [password, setPassword] = useState('');
66
- const [privacyAccepted, setPrivacyAccepted] = useState(false);
67
- const [privacyError, setPrivacyError] = useState(false);
68
- const [passwordError, setPasswordError] = useState<string | null>(null);
69
- const [acceptsMarketing, setAcceptsMarketing] = useState(false);
70
- const { capabilities } = useStoreCapabilities();
71
-
72
- /**
73
- * Whether to promise a birthday gift beside the picker.
74
- *
75
- * The gate is inverted relative to `ReferralGreeting`, and deliberately so.
76
- * There, an undecided capability must not suppress a real referral, so only
77
- * an explicit `false` hides anything. Here the text makes a PROMISE, and a
78
- * promise may only be printed on an explicit `true`: a store with birthday
79
- * rewards switched off — which is the default — would otherwise tell every
80
- * customer a gift is coming and send nothing, and `requireBirthday` can make
81
- * handing over the date compulsory on top of that.
82
- */
83
- const birthdayGiftOn = capabilities?.features.hasBirthdayRewards === true;
84
- const [birthMonth, setBirthMonth] = useState('');
85
- const [birthDay, setBirthDay] = useState('');
86
- const [birthdayError, setBirthdayError] = useState<string | null>(null);
87
- const [loading, setLoading] = useState(false);
88
- const [referralCode, setReferralCode] = useState<string | null>(null);
89
-
90
- /**
91
- * A referral code captured from a `?ref=` link, possibly on a different day.
92
- * Read in an effect rather than during render because it comes from
93
- * `document.cookie`, which does not exist during SSR: reading it inline would
94
- * render one thing on the server and another on the client and trip a
95
- * hydration mismatch.
96
- */
97
- useEffect(() => {
98
- setReferralCode(readReferralCookie());
99
- }, []);
100
-
101
- /**
102
- * The merchant can make the birthday mandatory at registration on this sales
103
- * channel; the backend then rejects a register call without it. The flag is a
104
- * rendering hint only, and absent means optional, so the fields stay optional
105
- * unless it is explicitly true.
106
- */
107
- const birthdayRequired = storeInfo?.requireBirthday === true;
108
-
109
- const strength = useMemo(() => getPasswordStrength(password), [password]);
110
-
111
- /**
112
- * The picker hands back both halves at once, or two nulls when the shopper
113
- * clears the field. State stays as strings so the submit path below is
114
- * unchanged.
115
- */
116
- function selectBirthday(month: number | null, day: number | null) {
117
- setBirthMonth(month ? String(month) : '');
118
- setBirthDay(day ? String(day) : '');
119
- setBirthdayError(null);
120
- }
121
-
122
- async function handleSubmit(e: React.FormEvent) {
123
- e.preventDefault();
124
- if (loading) return;
125
-
126
- const pwCode = getPasswordError(password);
127
- if (pwCode) {
128
- setPasswordError(t(pwCode));
129
- return;
130
- }
131
- setPasswordError(null);
132
-
133
- const month = toBirthdayNumber(birthMonth);
134
- const day = toBirthdayNumber(birthDay);
135
-
136
- if (birthdayRequired && (month === null || day === null)) {
137
- setBirthdayError(tc('birthdayRequired'));
138
- return;
139
- }
140
- // Half a birthday is an HTTP 400 at the API, so catch it here rather than
141
- // letting the shopper find out from a failed registration.
142
- if ((month === null) !== (day === null)) {
143
- setBirthdayError(tc('birthdayIncomplete'));
144
- return;
145
- }
146
- setBirthdayError(null);
147
-
148
- if (!privacyAccepted) {
149
- setPrivacyError(true);
150
- return;
151
- }
152
-
153
- try {
154
- setLoading(true);
155
- await onSubmit({
156
- firstName,
157
- lastName,
158
- email,
159
- password,
160
- acceptsMarketing,
161
- // Omitted, never nulled, when the shopper left the field empty:
162
- // `registerCustomer()` takes numbers only.
163
- ...(month !== null && day !== null ? { birthMonth: month, birthDay: day } : {}),
164
- // Same rule for the referral: send it only when one was captured. The
165
- // API validates it after the fact, so a stale code cannot cost this
166
- // shopper their registration.
167
- ...(referralCode ? { referralCode } : {}),
168
- });
169
- } finally {
170
- setLoading(false);
171
- }
172
- }
173
-
174
- return (
175
- <form onSubmit={handleSubmit} className={cn('space-y-4', className)}>
176
- {error && (
177
- <div className="bg-destructive/10 border-destructive/20 text-destructive rounded-lg border px-4 py-3 text-sm">
178
- {error}
179
- </div>
180
- )}
181
-
182
- {/* A referral captured earlier is about to be applied. Worth saying out
183
- loud: the shopper was promised a welcome gift on a page they may have
184
- left days ago, and silence here reads as the offer having expired.
185
- Renders nothing when no code was captured. */}
186
- {referralCode && (
187
- <p className="border-primary/20 bg-primary/5 text-muted-foreground rounded-lg border px-4 py-3 text-sm">
188
- {t('referralApplied')}
189
- </p>
190
- )}
191
-
192
- <div className="grid grid-cols-2 gap-3">
193
- <div>
194
- <label
195
- htmlFor="register-first-name"
196
- className="text-foreground mb-1.5 block text-sm font-medium"
197
- >
198
- {tf('firstName')}
199
- </label>
200
- <input
201
- id="register-first-name"
202
- type="text"
203
- required
204
- value={firstName}
205
- onChange={(e) => setFirstName(e.target.value)}
206
- placeholder={t('firstNamePlaceholder')}
207
- autoComplete="given-name"
208
- className="border-border bg-background text-foreground placeholder:text-muted-foreground focus:ring-primary/20 focus:border-primary h-10 w-full rounded border px-3 text-sm focus:outline-none focus:ring-2"
209
- />
210
- </div>
211
-
212
- <div>
213
- <label
214
- htmlFor="register-last-name"
215
- className="text-foreground mb-1.5 block text-sm font-medium"
216
- >
217
- {tf('lastName')}
218
- </label>
219
- <input
220
- id="register-last-name"
221
- type="text"
222
- required
223
- value={lastName}
224
- onChange={(e) => setLastName(e.target.value)}
225
- placeholder={t('lastNamePlaceholder')}
226
- autoComplete="family-name"
227
- className="border-border bg-background text-foreground placeholder:text-muted-foreground focus:ring-primary/20 focus:border-primary h-10 w-full rounded border px-3 text-sm focus:outline-none focus:ring-2"
228
- />
229
- </div>
230
- </div>
231
-
232
- <div>
233
- <label
234
- htmlFor="register-email"
235
- className="text-foreground mb-1.5 block text-sm font-medium"
236
- >
237
- {t('email')}
238
- </label>
239
- <input
240
- id="register-email"
241
- type="email"
242
- required
243
- value={email}
244
- onChange={(e) => setEmail(e.target.value)}
245
- placeholder={t('emailPlaceholder')}
246
- autoComplete="email"
247
- className="border-border bg-background text-foreground placeholder:text-muted-foreground focus:ring-primary/20 focus:border-primary h-10 w-full rounded border px-3 text-sm focus:outline-none focus:ring-2"
248
- />
249
- </div>
250
-
251
- <div>
252
- <label
253
- htmlFor="register-password"
254
- className="text-foreground mb-1.5 block text-sm font-medium"
255
- >
256
- {t('password')}
257
- </label>
258
- <input
259
- id="register-password"
260
- type="password"
261
- required
262
- minLength={8}
263
- value={password}
264
- onChange={(e) => {
265
- setPassword(e.target.value);
266
- if (passwordError) setPasswordError(null);
267
- }}
268
- placeholder={t('atLeastChars')}
269
- autoComplete="new-password"
270
- className="border-border bg-background text-foreground placeholder:text-muted-foreground focus:ring-primary/20 focus:border-primary h-10 w-full rounded border px-3 text-sm focus:outline-none focus:ring-2"
271
- />
272
- {password.length > 0 && (
273
- <div className="mt-2">
274
- <div className="bg-muted h-1.5 w-full overflow-hidden rounded-full">
275
- <div
276
- className={cn(
277
- 'h-full rounded-full transition-all duration-300',
278
- strength.color,
279
- strength.width
280
- )}
281
- />
282
- </div>
283
- <p className="text-muted-foreground mt-1 text-xs">
284
- {strength.label
285
- ? t(strength.label as 'tooShort' | 'weak' | 'fair' | 'good' | 'strong')
286
- : ''}
287
- </p>
288
- </div>
289
- )}
290
- {passwordError && <p className="text-destructive mt-1 text-xs">{passwordError}</p>}
291
- </div>
292
-
293
- {/* Birthday: month and day only, never a year. Optional unless the
294
- merchant made it mandatory on this sales channel. */}
295
- <div>
296
- <label
297
- htmlFor="register-birthday"
298
- className="text-foreground mb-1.5 block text-sm font-medium"
299
- >
300
- {tc('birthday')}
301
- {birthdayRequired && <span className="text-destructive"> *</span>}
302
- </label>
303
- <BirthdayPicker
304
- id="register-birthday"
305
- month={toBirthdayNumber(birthMonth)}
306
- day={toBirthdayNumber(birthDay)}
307
- onChange={selectBirthday}
308
- required={birthdayRequired}
309
- invalid={birthdayError !== null}
310
- />
311
- {birthdayGiftOn && (
312
- <p className="text-muted-foreground mt-1.5 text-xs">{tc('birthdayGiftNote')}</p>
313
- )}
314
- {birthdayError && <p className="text-destructive mt-1 text-xs">{birthdayError}</p>}
315
- </div>
316
-
317
- {/* Privacy Policy (required) */}
318
- <div>
319
- <label className="flex cursor-pointer items-start gap-2">
320
- <input
321
- type="checkbox"
322
- checked={privacyAccepted}
323
- onChange={(e) => {
324
- setPrivacyAccepted(e.target.checked);
325
- setPrivacyError(false);
326
- }}
327
- className="accent-primary mt-0.5"
328
- />
329
- <span className="text-muted-foreground text-sm">
330
- {t('privacyAcceptPrefix')}{' '}
331
- <a
332
- href="/privacy"
333
- target="_blank"
334
- rel="noopener noreferrer"
335
- className="text-primary underline underline-offset-2"
336
- >
337
- {t('privacyPolicyLink')}
338
- </a>{' '}
339
- <span className="text-destructive">*</span>
340
- </span>
341
- </label>
342
- {privacyError && <p className="text-destructive mt-1 text-xs">{t('privacyRequired')}</p>}
343
- </div>
344
-
345
- {/* Marketing consent (optional) */}
346
- <label className="flex cursor-pointer items-start gap-2">
347
- <input
348
- type="checkbox"
349
- checked={acceptsMarketing}
350
- onChange={(e) => setAcceptsMarketing(e.target.checked)}
351
- className="accent-primary mt-0.5"
352
- />
353
- <span className="text-muted-foreground text-sm">{t('acceptsMarketing')}</span>
354
- </label>
355
-
356
- <button
357
- type="submit"
358
- disabled={loading}
359
- className="bg-primary text-primary-foreground flex h-10 w-full items-center justify-center gap-2 rounded text-sm font-medium transition-opacity hover:opacity-90 disabled:cursor-not-allowed disabled:opacity-50"
360
- >
361
- {loading ? (
362
- <>
363
- <LoadingSpinner
364
- size="sm"
365
- className="border-primary-foreground/30 border-t-primary-foreground"
366
- />
367
- {t('creatingAccount')}
368
- </>
369
- ) : (
370
- t('createAccount')
371
- )}
372
- </button>
373
- </form>
374
- );
375
- }
1
+ 'use client';
2
+
3
+ import { useState, useMemo, useEffect } from 'react';
4
+ import { useTranslations } from '@/core/lib/translations';
5
+ import { cn } from '@/core/lib/utils';
6
+ import { LoadingSpinner } from '@/ui/shared/loading-spinner';
7
+ import { getPasswordError } from '@/core/lib/validation';
8
+ import { useStoreInfo } from '@/core/providers/store-provider';
9
+ import { toBirthdayNumber } from '@/core/lib/birthday';
10
+ import { BirthdayPicker } from '@/components/shared/birthday-picker';
11
+ import { readReferralCookie } from '@/core/lib/referral';
12
+ import { useStoreCapabilities } from '@/core/providers/store-provider';
13
+ import { usePageExists } from '@/core/hooks/use-page-exists';
14
+
15
+ interface RegisterData {
16
+ firstName: string;
17
+ lastName: string;
18
+ email: string;
19
+ password: string;
20
+ acceptsMarketing: boolean;
21
+ /**
22
+ * Birthday month and day, never a year. Sent together or left out entirely:
23
+ * one without the other is rejected with HTTP 400. Omitted rather than
24
+ * nulled when the shopper leaves the field empty, because `registerCustomer()`
25
+ * takes numbers only.
26
+ */
27
+ birthMonth?: number;
28
+ birthDay?: number;
29
+ /**
30
+ * Loyalty referral share code, captured from a `?ref=` link that may have
31
+ * been clicked days ago. Omitted when nothing was captured; never invented.
32
+ */
33
+ referralCode?: string;
34
+ }
35
+
36
+ interface RegisterFormProps {
37
+ onSubmit: (data: RegisterData) => Promise<void>;
38
+ error?: string | null;
39
+ className?: string;
40
+ }
41
+
42
+ function getPasswordStrength(password: string): { label: string; color: string; width: string } {
43
+ if (password.length === 0) return { label: '', color: '', width: 'w-0' };
44
+ if (password.length < 8) return { label: 'tooShort', color: 'bg-destructive', width: 'w-1/4' };
45
+
46
+ let score = 0;
47
+ if (password.length >= 8) score++;
48
+ if (/[A-Z]/.test(password)) score++;
49
+ if (/[0-9]/.test(password)) score++;
50
+ if (/[^A-Za-z0-9]/.test(password)) score++;
51
+
52
+ if (score <= 1) return { label: 'weak', color: 'bg-orange-500', width: 'w-1/3' };
53
+ if (score <= 2) return { label: 'fair', color: 'bg-yellow-500', width: 'w-1/2' };
54
+ if (score <= 3) return { label: 'good', color: 'bg-primary', width: 'w-3/4' };
55
+ return { label: 'strong', color: 'bg-green-500', width: 'w-full' };
56
+ }
57
+
58
+ export function RegisterForm({ onSubmit, error, className }: RegisterFormProps) {
59
+ const t = useTranslations('auth');
60
+ const tf = useTranslations('checkoutForm');
61
+ const tc = useTranslations('common');
62
+ const { storeInfo } = useStoreInfo();
63
+ const [firstName, setFirstName] = useState('');
64
+ const [lastName, setLastName] = useState('');
65
+ const [email, setEmail] = useState('');
66
+ const [password, setPassword] = useState('');
67
+ const [privacyAccepted, setPrivacyAccepted] = useState(false);
68
+ const hasPrivacyPage = usePageExists('privacy');
69
+ const [privacyError, setPrivacyError] = useState(false);
70
+ const [passwordError, setPasswordError] = useState<string | null>(null);
71
+ const [acceptsMarketing, setAcceptsMarketing] = useState(false);
72
+ const { capabilities } = useStoreCapabilities();
73
+
74
+ /**
75
+ * Whether to promise a birthday gift beside the picker.
76
+ *
77
+ * The gate is inverted relative to `ReferralGreeting`, and deliberately so.
78
+ * There, an undecided capability must not suppress a real referral, so only
79
+ * an explicit `false` hides anything. Here the text makes a PROMISE, and a
80
+ * promise may only be printed on an explicit `true`: a store with birthday
81
+ * rewards switched off — which is the default — would otherwise tell every
82
+ * customer a gift is coming and send nothing, and `requireBirthday` can make
83
+ * handing over the date compulsory on top of that.
84
+ */
85
+ const birthdayGiftOn = capabilities?.features.hasBirthdayRewards === true;
86
+ const [birthMonth, setBirthMonth] = useState('');
87
+ const [birthDay, setBirthDay] = useState('');
88
+ const [birthdayError, setBirthdayError] = useState<string | null>(null);
89
+ const [loading, setLoading] = useState(false);
90
+ const [referralCode, setReferralCode] = useState<string | null>(null);
91
+
92
+ /**
93
+ * A referral code captured from a `?ref=` link, possibly on a different day.
94
+ * Read in an effect rather than during render because it comes from
95
+ * `document.cookie`, which does not exist during SSR: reading it inline would
96
+ * render one thing on the server and another on the client and trip a
97
+ * hydration mismatch.
98
+ */
99
+ useEffect(() => {
100
+ setReferralCode(readReferralCookie());
101
+ }, []);
102
+
103
+ /**
104
+ * The merchant can make the birthday mandatory at registration on this sales
105
+ * channel; the backend then rejects a register call without it. The flag is a
106
+ * rendering hint only, and absent means optional, so the fields stay optional
107
+ * unless it is explicitly true.
108
+ */
109
+ const birthdayRequired = storeInfo?.requireBirthday === true;
110
+
111
+ const strength = useMemo(() => getPasswordStrength(password), [password]);
112
+
113
+ /**
114
+ * The picker hands back both halves at once, or two nulls when the shopper
115
+ * clears the field. State stays as strings so the submit path below is
116
+ * unchanged.
117
+ */
118
+ function selectBirthday(month: number | null, day: number | null) {
119
+ setBirthMonth(month ? String(month) : '');
120
+ setBirthDay(day ? String(day) : '');
121
+ setBirthdayError(null);
122
+ }
123
+
124
+ async function handleSubmit(e: React.FormEvent) {
125
+ e.preventDefault();
126
+ if (loading) return;
127
+
128
+ const pwCode = getPasswordError(password);
129
+ if (pwCode) {
130
+ setPasswordError(t(pwCode));
131
+ return;
132
+ }
133
+ setPasswordError(null);
134
+
135
+ const month = toBirthdayNumber(birthMonth);
136
+ const day = toBirthdayNumber(birthDay);
137
+
138
+ if (birthdayRequired && (month === null || day === null)) {
139
+ setBirthdayError(tc('birthdayRequired'));
140
+ return;
141
+ }
142
+ // Half a birthday is an HTTP 400 at the API, so catch it here rather than
143
+ // letting the shopper find out from a failed registration.
144
+ if ((month === null) !== (day === null)) {
145
+ setBirthdayError(tc('birthdayIncomplete'));
146
+ return;
147
+ }
148
+ setBirthdayError(null);
149
+
150
+ if (!privacyAccepted) {
151
+ setPrivacyError(true);
152
+ return;
153
+ }
154
+
155
+ try {
156
+ setLoading(true);
157
+ await onSubmit({
158
+ firstName,
159
+ lastName,
160
+ email,
161
+ password,
162
+ acceptsMarketing,
163
+ // Omitted, never nulled, when the shopper left the field empty:
164
+ // `registerCustomer()` takes numbers only.
165
+ ...(month !== null && day !== null ? { birthMonth: month, birthDay: day } : {}),
166
+ // Same rule for the referral: send it only when one was captured. The
167
+ // API validates it after the fact, so a stale code cannot cost this
168
+ // shopper their registration.
169
+ ...(referralCode ? { referralCode } : {}),
170
+ });
171
+ } finally {
172
+ setLoading(false);
173
+ }
174
+ }
175
+
176
+ return (
177
+ <form onSubmit={handleSubmit} className={cn('space-y-4', className)}>
178
+ {error && (
179
+ <div className="bg-destructive/10 border-destructive/20 text-destructive rounded-lg border px-4 py-3 text-sm">
180
+ {error}
181
+ </div>
182
+ )}
183
+
184
+ {/* A referral captured earlier is about to be applied. Worth saying out
185
+ loud: the shopper was promised a welcome gift on a page they may have
186
+ left days ago, and silence here reads as the offer having expired.
187
+ Renders nothing when no code was captured. */}
188
+ {referralCode && (
189
+ <p className="border-primary/20 bg-primary/5 text-muted-foreground rounded-lg border px-4 py-3 text-sm">
190
+ {t('referralApplied')}
191
+ </p>
192
+ )}
193
+
194
+ <div className="grid grid-cols-2 gap-3">
195
+ <div>
196
+ <label
197
+ htmlFor="register-first-name"
198
+ className="text-foreground mb-1.5 block text-sm font-medium"
199
+ >
200
+ {tf('firstName')}
201
+ </label>
202
+ <input
203
+ id="register-first-name"
204
+ type="text"
205
+ required
206
+ value={firstName}
207
+ onChange={(e) => setFirstName(e.target.value)}
208
+ placeholder={t('firstNamePlaceholder')}
209
+ autoComplete="given-name"
210
+ className="border-border bg-background text-foreground placeholder:text-muted-foreground focus:ring-primary/20 focus:border-primary h-10 w-full rounded border px-3 text-sm focus:outline-none focus:ring-2"
211
+ />
212
+ </div>
213
+
214
+ <div>
215
+ <label
216
+ htmlFor="register-last-name"
217
+ className="text-foreground mb-1.5 block text-sm font-medium"
218
+ >
219
+ {tf('lastName')}
220
+ </label>
221
+ <input
222
+ id="register-last-name"
223
+ type="text"
224
+ required
225
+ value={lastName}
226
+ onChange={(e) => setLastName(e.target.value)}
227
+ placeholder={t('lastNamePlaceholder')}
228
+ autoComplete="family-name"
229
+ className="border-border bg-background text-foreground placeholder:text-muted-foreground focus:ring-primary/20 focus:border-primary h-10 w-full rounded border px-3 text-sm focus:outline-none focus:ring-2"
230
+ />
231
+ </div>
232
+ </div>
233
+
234
+ <div>
235
+ <label
236
+ htmlFor="register-email"
237
+ className="text-foreground mb-1.5 block text-sm font-medium"
238
+ >
239
+ {t('email')}
240
+ </label>
241
+ <input
242
+ id="register-email"
243
+ type="email"
244
+ required
245
+ value={email}
246
+ onChange={(e) => setEmail(e.target.value)}
247
+ placeholder={t('emailPlaceholder')}
248
+ autoComplete="email"
249
+ className="border-border bg-background text-foreground placeholder:text-muted-foreground focus:ring-primary/20 focus:border-primary h-10 w-full rounded border px-3 text-sm focus:outline-none focus:ring-2"
250
+ />
251
+ </div>
252
+
253
+ <div>
254
+ <label
255
+ htmlFor="register-password"
256
+ className="text-foreground mb-1.5 block text-sm font-medium"
257
+ >
258
+ {t('password')}
259
+ </label>
260
+ <input
261
+ id="register-password"
262
+ type="password"
263
+ required
264
+ minLength={8}
265
+ value={password}
266
+ onChange={(e) => {
267
+ setPassword(e.target.value);
268
+ if (passwordError) setPasswordError(null);
269
+ }}
270
+ placeholder={t('atLeastChars')}
271
+ autoComplete="new-password"
272
+ className="border-border bg-background text-foreground placeholder:text-muted-foreground focus:ring-primary/20 focus:border-primary h-10 w-full rounded border px-3 text-sm focus:outline-none focus:ring-2"
273
+ />
274
+ {password.length > 0 && (
275
+ <div className="mt-2">
276
+ <div className="bg-muted h-1.5 w-full overflow-hidden rounded-full">
277
+ <div
278
+ className={cn(
279
+ 'h-full rounded-full transition-all duration-300',
280
+ strength.color,
281
+ strength.width
282
+ )}
283
+ />
284
+ </div>
285
+ <p className="text-muted-foreground mt-1 text-xs">
286
+ {strength.label
287
+ ? t(strength.label as 'tooShort' | 'weak' | 'fair' | 'good' | 'strong')
288
+ : ''}
289
+ </p>
290
+ </div>
291
+ )}
292
+ {passwordError && <p className="text-destructive mt-1 text-xs">{passwordError}</p>}
293
+ </div>
294
+
295
+ {/* Birthday: month and day only, never a year. Optional unless the
296
+ merchant made it mandatory on this sales channel. */}
297
+ <div>
298
+ <label
299
+ htmlFor="register-birthday"
300
+ className="text-foreground mb-1.5 block text-sm font-medium"
301
+ >
302
+ {tc('birthday')}
303
+ {birthdayRequired && <span className="text-destructive"> *</span>}
304
+ </label>
305
+ <BirthdayPicker
306
+ id="register-birthday"
307
+ month={toBirthdayNumber(birthMonth)}
308
+ day={toBirthdayNumber(birthDay)}
309
+ onChange={selectBirthday}
310
+ required={birthdayRequired}
311
+ invalid={birthdayError !== null}
312
+ />
313
+ {birthdayGiftOn && (
314
+ <p className="text-muted-foreground mt-1.5 text-xs">{tc('birthdayGiftNote')}</p>
315
+ )}
316
+ {birthdayError && <p className="text-destructive mt-1 text-xs">{birthdayError}</p>}
317
+ </div>
318
+
319
+ {/* Privacy Policy (required) */}
320
+ <div>
321
+ <label className="flex cursor-pointer items-start gap-2">
322
+ <input
323
+ type="checkbox"
324
+ checked={privacyAccepted}
325
+ onChange={(e) => {
326
+ setPrivacyAccepted(e.target.checked);
327
+ setPrivacyError(false);
328
+ }}
329
+ className="accent-primary mt-0.5"
330
+ />
331
+ <span className="text-muted-foreground text-sm">
332
+ {t('privacyAcceptPrefix')}{' '}
333
+ {hasPrivacyPage === false ? (
334
+ <span className="text-foreground">{t('privacyPolicyLink')}</span>
335
+ ) : (
336
+ <a
337
+ href="/pages/privacy"
338
+ target="_blank"
339
+ rel="noopener noreferrer"
340
+ className="text-primary underline underline-offset-2"
341
+ >
342
+ {t('privacyPolicyLink')}
343
+ </a>
344
+ )}{' '}
345
+ <span className="text-destructive">*</span>
346
+ </span>
347
+ </label>
348
+ {privacyError && <p className="text-destructive mt-1 text-xs">{t('privacyRequired')}</p>}
349
+ </div>
350
+
351
+ {/* Marketing consent (optional) */}
352
+ <label className="flex cursor-pointer items-start gap-2">
353
+ <input
354
+ type="checkbox"
355
+ checked={acceptsMarketing}
356
+ onChange={(e) => setAcceptsMarketing(e.target.checked)}
357
+ className="accent-primary mt-0.5"
358
+ />
359
+ <span className="text-muted-foreground text-sm">{t('acceptsMarketing')}</span>
360
+ </label>
361
+
362
+ <button
363
+ type="submit"
364
+ disabled={loading}
365
+ className="bg-primary text-primary-foreground flex h-10 w-full items-center justify-center gap-2 rounded text-sm font-medium transition-opacity hover:opacity-90 disabled:cursor-not-allowed disabled:opacity-50"
366
+ >
367
+ {loading ? (
368
+ <>
369
+ <LoadingSpinner
370
+ size="sm"
371
+ className="border-primary-foreground/30 border-t-primary-foreground"
372
+ />
373
+ {t('creatingAccount')}
374
+ </>
375
+ ) : (
376
+ t('createAccount')
377
+ )}
378
+ </button>
379
+ </form>
380
+ );
381
+ }