create-brainerce-store 1.14.3 → 1.14.5

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.
@@ -1,184 +1,232 @@
1
- 'use client';
2
-
3
- import { useState, useMemo } from 'react';
4
- import { useTranslations } from '@/lib/translations';
5
- import { cn } from '@/lib/utils';
6
- import { LoadingSpinner } from '@/components/shared/loading-spinner';
7
-
8
- interface RegisterData {
9
- firstName: string;
10
- lastName: string;
11
- email: string;
12
- password: string;
13
- }
14
-
15
- interface RegisterFormProps {
16
- onSubmit: (data: RegisterData) => Promise<void>;
17
- error?: string | null;
18
- className?: string;
19
- }
20
-
21
- function getPasswordStrength(password: string): { label: string; color: string; width: string } {
22
- if (password.length === 0) return { label: '', color: '', width: 'w-0' };
23
- if (password.length < 6) return { label: 'tooShort', color: 'bg-destructive', width: 'w-1/4' };
24
-
25
- let score = 0;
26
- if (password.length >= 8) score++;
27
- if (/[A-Z]/.test(password)) score++;
28
- if (/[0-9]/.test(password)) score++;
29
- if (/[^A-Za-z0-9]/.test(password)) score++;
30
-
31
- if (score <= 1) return { label: 'weak', color: 'bg-orange-500', width: 'w-1/3' };
32
- if (score <= 2) return { label: 'fair', color: 'bg-yellow-500', width: 'w-1/2' };
33
- if (score <= 3) return { label: 'good', color: 'bg-primary', width: 'w-3/4' };
34
- return { label: 'strong', color: 'bg-green-500', width: 'w-full' };
35
- }
36
-
37
- export function RegisterForm({ onSubmit, error, className }: RegisterFormProps) {
38
- const t = useTranslations('auth');
39
- const tf = useTranslations('checkoutForm');
40
- const [firstName, setFirstName] = useState('');
41
- const [lastName, setLastName] = useState('');
42
- const [email, setEmail] = useState('');
43
- const [password, setPassword] = useState('');
44
- const [loading, setLoading] = useState(false);
45
-
46
- const strength = useMemo(() => getPasswordStrength(password), [password]);
47
-
48
- async function handleSubmit(e: React.FormEvent) {
49
- e.preventDefault();
50
- if (loading) return;
51
-
52
- try {
53
- setLoading(true);
54
- await onSubmit({ firstName, lastName, email, password });
55
- } finally {
56
- setLoading(false);
57
- }
58
- }
59
-
60
- return (
61
- <form onSubmit={handleSubmit} className={cn('space-y-4', className)}>
62
- {error && (
63
- <div className="bg-destructive/10 border-destructive/20 text-destructive rounded-lg border px-4 py-3 text-sm">
64
- {error}
65
- </div>
66
- )}
67
-
68
- <div className="grid grid-cols-2 gap-3">
69
- <div>
70
- <label
71
- htmlFor="register-first-name"
72
- className="text-foreground mb-1.5 block text-sm font-medium"
73
- >
74
- {tf('firstName')}
75
- </label>
76
- <input
77
- id="register-first-name"
78
- type="text"
79
- required
80
- value={firstName}
81
- onChange={(e) => setFirstName(e.target.value)}
82
- placeholder={t('firstNamePlaceholder')}
83
- autoComplete="given-name"
84
- 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"
85
- />
86
- </div>
87
-
88
- <div>
89
- <label
90
- htmlFor="register-last-name"
91
- className="text-foreground mb-1.5 block text-sm font-medium"
92
- >
93
- {tf('lastName')}
94
- </label>
95
- <input
96
- id="register-last-name"
97
- type="text"
98
- required
99
- value={lastName}
100
- onChange={(e) => setLastName(e.target.value)}
101
- placeholder={t('lastNamePlaceholder')}
102
- autoComplete="family-name"
103
- 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"
104
- />
105
- </div>
106
- </div>
107
-
108
- <div>
109
- <label
110
- htmlFor="register-email"
111
- className="text-foreground mb-1.5 block text-sm font-medium"
112
- >
113
- {t('email')}
114
- </label>
115
- <input
116
- id="register-email"
117
- type="email"
118
- required
119
- value={email}
120
- onChange={(e) => setEmail(e.target.value)}
121
- placeholder={t('emailPlaceholder')}
122
- autoComplete="email"
123
- 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"
124
- />
125
- </div>
126
-
127
- <div>
128
- <label
129
- htmlFor="register-password"
130
- className="text-foreground mb-1.5 block text-sm font-medium"
131
- >
132
- {t('password')}
133
- </label>
134
- <input
135
- id="register-password"
136
- type="password"
137
- required
138
- minLength={6}
139
- value={password}
140
- onChange={(e) => setPassword(e.target.value)}
141
- placeholder={t('atLeastChars')}
142
- autoComplete="new-password"
143
- 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"
144
- />
145
- {password.length > 0 && (
146
- <div className="mt-2">
147
- <div className="bg-muted h-1.5 w-full overflow-hidden rounded-full">
148
- <div
149
- className={cn(
150
- 'h-full rounded-full transition-all duration-300',
151
- strength.color,
152
- strength.width
153
- )}
154
- />
155
- </div>
156
- <p className="text-muted-foreground mt-1 text-xs">
157
- {strength.label
158
- ? t(strength.label as 'tooShort' | 'weak' | 'fair' | 'good' | 'strong')
159
- : ''}
160
- </p>
161
- </div>
162
- )}
163
- </div>
164
-
165
- <button
166
- type="submit"
167
- disabled={loading}
168
- 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"
169
- >
170
- {loading ? (
171
- <>
172
- <LoadingSpinner
173
- size="sm"
174
- className="border-primary-foreground/30 border-t-primary-foreground"
175
- />
176
- {t('creatingAccount')}
177
- </>
178
- ) : (
179
- t('createAccount')
180
- )}
181
- </button>
182
- </form>
183
- );
184
- }
1
+ 'use client';
2
+
3
+ import { useState, useMemo } from 'react';
4
+ import { useTranslations } from '@/lib/translations';
5
+ import { cn } from '@/lib/utils';
6
+ import { LoadingSpinner } from '@/components/shared/loading-spinner';
7
+
8
+ interface RegisterData {
9
+ firstName: string;
10
+ lastName: string;
11
+ email: string;
12
+ password: string;
13
+ acceptsMarketing: boolean;
14
+ }
15
+
16
+ interface RegisterFormProps {
17
+ onSubmit: (data: RegisterData) => Promise<void>;
18
+ error?: string | null;
19
+ className?: string;
20
+ }
21
+
22
+ function getPasswordStrength(password: string): { label: string; color: string; width: string } {
23
+ if (password.length === 0) return { label: '', color: '', width: 'w-0' };
24
+ if (password.length < 6) return { label: 'tooShort', color: 'bg-destructive', width: 'w-1/4' };
25
+
26
+ let score = 0;
27
+ if (password.length >= 8) score++;
28
+ if (/[A-Z]/.test(password)) score++;
29
+ if (/[0-9]/.test(password)) score++;
30
+ if (/[^A-Za-z0-9]/.test(password)) score++;
31
+
32
+ if (score <= 1) return { label: 'weak', color: 'bg-orange-500', width: 'w-1/3' };
33
+ if (score <= 2) return { label: 'fair', color: 'bg-yellow-500', width: 'w-1/2' };
34
+ if (score <= 3) return { label: 'good', color: 'bg-primary', width: 'w-3/4' };
35
+ return { label: 'strong', color: 'bg-green-500', width: 'w-full' };
36
+ }
37
+
38
+ export function RegisterForm({ onSubmit, error, className }: RegisterFormProps) {
39
+ const t = useTranslations('auth');
40
+ const tf = useTranslations('checkoutForm');
41
+ const [firstName, setFirstName] = useState('');
42
+ const [lastName, setLastName] = useState('');
43
+ const [email, setEmail] = useState('');
44
+ const [password, setPassword] = useState('');
45
+ const [privacyAccepted, setPrivacyAccepted] = useState(false);
46
+ const [privacyError, setPrivacyError] = useState(false);
47
+ const [acceptsMarketing, setAcceptsMarketing] = useState(false);
48
+ const [loading, setLoading] = useState(false);
49
+
50
+ const strength = useMemo(() => getPasswordStrength(password), [password]);
51
+
52
+ async function handleSubmit(e: React.FormEvent) {
53
+ e.preventDefault();
54
+ if (loading) return;
55
+
56
+ if (!privacyAccepted) {
57
+ setPrivacyError(true);
58
+ return;
59
+ }
60
+
61
+ try {
62
+ setLoading(true);
63
+ await onSubmit({ firstName, lastName, email, password, acceptsMarketing });
64
+ } finally {
65
+ setLoading(false);
66
+ }
67
+ }
68
+
69
+ return (
70
+ <form onSubmit={handleSubmit} className={cn('space-y-4', className)}>
71
+ {error && (
72
+ <div className="bg-destructive/10 border-destructive/20 text-destructive rounded-lg border px-4 py-3 text-sm">
73
+ {error}
74
+ </div>
75
+ )}
76
+
77
+ <div className="grid grid-cols-2 gap-3">
78
+ <div>
79
+ <label
80
+ htmlFor="register-first-name"
81
+ className="text-foreground mb-1.5 block text-sm font-medium"
82
+ >
83
+ {tf('firstName')}
84
+ </label>
85
+ <input
86
+ id="register-first-name"
87
+ type="text"
88
+ required
89
+ value={firstName}
90
+ onChange={(e) => setFirstName(e.target.value)}
91
+ placeholder={t('firstNamePlaceholder')}
92
+ autoComplete="given-name"
93
+ 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"
94
+ />
95
+ </div>
96
+
97
+ <div>
98
+ <label
99
+ htmlFor="register-last-name"
100
+ className="text-foreground mb-1.5 block text-sm font-medium"
101
+ >
102
+ {tf('lastName')}
103
+ </label>
104
+ <input
105
+ id="register-last-name"
106
+ type="text"
107
+ required
108
+ value={lastName}
109
+ onChange={(e) => setLastName(e.target.value)}
110
+ placeholder={t('lastNamePlaceholder')}
111
+ autoComplete="family-name"
112
+ 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"
113
+ />
114
+ </div>
115
+ </div>
116
+
117
+ <div>
118
+ <label
119
+ htmlFor="register-email"
120
+ className="text-foreground mb-1.5 block text-sm font-medium"
121
+ >
122
+ {t('email')}
123
+ </label>
124
+ <input
125
+ id="register-email"
126
+ type="email"
127
+ required
128
+ value={email}
129
+ onChange={(e) => setEmail(e.target.value)}
130
+ placeholder={t('emailPlaceholder')}
131
+ autoComplete="email"
132
+ 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"
133
+ />
134
+ </div>
135
+
136
+ <div>
137
+ <label
138
+ htmlFor="register-password"
139
+ className="text-foreground mb-1.5 block text-sm font-medium"
140
+ >
141
+ {t('password')}
142
+ </label>
143
+ <input
144
+ id="register-password"
145
+ type="password"
146
+ required
147
+ minLength={6}
148
+ value={password}
149
+ onChange={(e) => setPassword(e.target.value)}
150
+ placeholder={t('atLeastChars')}
151
+ autoComplete="new-password"
152
+ 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"
153
+ />
154
+ {password.length > 0 && (
155
+ <div className="mt-2">
156
+ <div className="bg-muted h-1.5 w-full overflow-hidden rounded-full">
157
+ <div
158
+ className={cn(
159
+ 'h-full rounded-full transition-all duration-300',
160
+ strength.color,
161
+ strength.width
162
+ )}
163
+ />
164
+ </div>
165
+ <p className="text-muted-foreground mt-1 text-xs">
166
+ {strength.label
167
+ ? t(strength.label as 'tooShort' | 'weak' | 'fair' | 'good' | 'strong')
168
+ : ''}
169
+ </p>
170
+ </div>
171
+ )}
172
+ </div>
173
+
174
+ {/* Privacy Policy (required) */}
175
+ <div>
176
+ <label className="flex cursor-pointer items-start gap-2">
177
+ <input
178
+ type="checkbox"
179
+ checked={privacyAccepted}
180
+ onChange={(e) => {
181
+ setPrivacyAccepted(e.target.checked);
182
+ setPrivacyError(false);
183
+ }}
184
+ className="accent-primary mt-0.5"
185
+ />
186
+ <span className="text-muted-foreground text-sm">
187
+ {t('privacyAcceptPrefix')}{' '}
188
+ <a
189
+ href="/privacy"
190
+ target="_blank"
191
+ rel="noopener noreferrer"
192
+ className="text-primary underline underline-offset-2"
193
+ >
194
+ {t('privacyPolicyLink')}
195
+ </a>{' '}
196
+ <span className="text-destructive">*</span>
197
+ </span>
198
+ </label>
199
+ {privacyError && <p className="text-destructive mt-1 text-xs">{t('privacyRequired')}</p>}
200
+ </div>
201
+
202
+ {/* Marketing consent (optional) */}
203
+ <label className="flex cursor-pointer items-start gap-2">
204
+ <input
205
+ type="checkbox"
206
+ checked={acceptsMarketing}
207
+ onChange={(e) => setAcceptsMarketing(e.target.checked)}
208
+ className="accent-primary mt-0.5"
209
+ />
210
+ <span className="text-muted-foreground text-sm">{t('acceptsMarketing')}</span>
211
+ </label>
212
+
213
+ <button
214
+ type="submit"
215
+ disabled={loading}
216
+ 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"
217
+ >
218
+ {loading ? (
219
+ <>
220
+ <LoadingSpinner
221
+ size="sm"
222
+ className="border-primary-foreground/30 border-t-primary-foreground"
223
+ />
224
+ {t('creatingAccount')}
225
+ </>
226
+ ) : (
227
+ t('createAccount')
228
+ )}
229
+ </button>
230
+ </form>
231
+ );
232
+ }