create-brainerce-store 1.5.0 → 1.5.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.
@@ -1,210 +1,224 @@
1
- 'use client';
2
-
3
- import { useState } from 'react';
4
- import type { CustomerProfile } from 'brainerce';
5
- import { getClient } from '@/lib/brainerce';
6
- import { useTranslations } from '@/lib/translations';
7
- import { cn } from '@/lib/utils';
8
-
9
- interface ProfileSectionProps {
10
- profile: CustomerProfile;
11
- onProfileUpdate?: (updated: CustomerProfile) => void;
12
- className?: string;
13
- }
14
-
15
- export function ProfileSection({ profile, onProfileUpdate, className }: ProfileSectionProps) {
16
- const t = useTranslations('account');
17
- const [editing, setEditing] = useState(false);
18
- const [saving, setSaving] = useState(false);
19
- const [message, setMessage] = useState<{ type: 'success' | 'error'; text: string } | null>(null);
20
- const [form, setForm] = useState({
21
- firstName: profile.firstName || '',
22
- lastName: profile.lastName || '',
23
- phone: profile.phone || '',
24
- });
25
-
26
- const fullName = [profile.firstName, profile.lastName].filter(Boolean).join(' ');
27
- const initials =
28
- [profile.firstName?.[0], profile.lastName?.[0]].filter(Boolean).join('').toUpperCase() ||
29
- profile.email[0].toUpperCase();
30
-
31
- function startEditing() {
32
- setForm({
33
- firstName: profile.firstName || '',
34
- lastName: profile.lastName || '',
35
- phone: profile.phone || '',
36
- });
37
- setMessage(null);
38
- setEditing(true);
39
- }
40
-
41
- function cancelEditing() {
42
- setEditing(false);
43
- setMessage(null);
44
- }
45
-
46
- async function handleSave(e: React.FormEvent) {
47
- e.preventDefault();
48
- setSaving(true);
49
- setMessage(null);
50
-
51
- try {
52
- const client = getClient();
53
- const updated = await client.updateMyProfile({
54
- firstName: form.firstName || undefined,
55
- lastName: form.lastName || undefined,
56
- phone: form.phone || undefined,
57
- });
58
- onProfileUpdate?.(updated);
59
- setEditing(false);
60
- setMessage({ type: 'success', text: t('profileUpdated') });
61
- setTimeout(() => setMessage(null), 3000);
62
- } catch {
63
- setMessage({ type: 'error', text: t('profileUpdateFailed') });
64
- } finally {
65
- setSaving(false);
66
- }
67
- }
68
-
69
- return (
70
- <div className={cn('border-border rounded-lg border p-6', className)}>
71
- <div className="flex items-start gap-4">
72
- {/* Avatar */}
73
- <div className="bg-primary/10 text-primary flex h-14 w-14 flex-shrink-0 items-center justify-center rounded-full text-lg font-semibold">
74
- {initials}
75
- </div>
76
-
77
- <div className="min-w-0 flex-1">
78
- {editing ? (
79
- <form onSubmit={handleSave} className="space-y-3">
80
- <div className="grid grid-cols-2 gap-3">
81
- <div>
82
- <label className="text-muted-foreground mb-1 block text-xs font-medium">
83
- {t('firstName')}
84
- </label>
85
- <input
86
- type="text"
87
- value={form.firstName}
88
- onChange={(e) => setForm((f) => ({ ...f, firstName: e.target.value }))}
89
- className="border-border bg-background text-foreground w-full rounded-md border px-3 py-1.5 text-sm outline-none focus:border-primary"
90
- autoFocus
91
- />
92
- </div>
93
- <div>
94
- <label className="text-muted-foreground mb-1 block text-xs font-medium">
95
- {t('lastName')}
96
- </label>
97
- <input
98
- type="text"
99
- value={form.lastName}
100
- onChange={(e) => setForm((f) => ({ ...f, lastName: e.target.value }))}
101
- className="border-border bg-background text-foreground w-full rounded-md border px-3 py-1.5 text-sm outline-none focus:border-primary"
102
- />
103
- </div>
104
- </div>
105
- <div>
106
- <label className="text-muted-foreground mb-1 block text-xs font-medium">
107
- {t('phone')}
108
- </label>
109
- <input
110
- type="tel"
111
- value={form.phone}
112
- onChange={(e) => setForm((f) => ({ ...f, phone: e.target.value }))}
113
- className="border-border bg-background text-foreground w-full rounded-md border px-3 py-1.5 text-sm outline-none focus:border-primary"
114
- />
115
- </div>
116
- <p className="text-muted-foreground truncate text-sm">{profile.email}</p>
117
- <div className="flex items-center gap-2">
118
- <button
119
- type="submit"
120
- disabled={saving}
121
- className="bg-primary text-primary-foreground rounded-md px-4 py-1.5 text-sm font-medium transition-opacity hover:opacity-90 disabled:opacity-50"
122
- >
123
- {saving ? '...' : t('save')}
124
- </button>
125
- <button
126
- type="button"
127
- onClick={cancelEditing}
128
- disabled={saving}
129
- className="text-muted-foreground hover:text-foreground rounded-md px-4 py-1.5 text-sm transition-colors disabled:opacity-50"
130
- >
131
- {t('cancel')}
132
- </button>
133
- </div>
134
- </form>
135
- ) : (
136
- <>
137
- <div className="flex items-center gap-2">
138
- {fullName && (
139
- <h2 className="text-foreground truncate text-lg font-semibold">{fullName}</h2>
140
- )}
141
- <button
142
- type="button"
143
- onClick={startEditing}
144
- className="text-muted-foreground hover:text-foreground flex-shrink-0 rounded p-1 transition-colors"
145
- title={t('editProfile')}
146
- >
147
- <svg className="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
148
- <path strokeLinecap="round" strokeLinejoin="round" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z" />
149
- </svg>
150
- </button>
151
- </div>
152
- <p className="text-muted-foreground truncate text-sm">{profile.email}</p>
153
-
154
- <div className="mt-2 flex items-center gap-2">
155
- {profile.emailVerified ? (
156
- <span className="inline-flex items-center gap-1 rounded-full bg-green-100 px-2 py-0.5 text-xs font-medium text-green-700 dark:bg-green-950/30 dark:text-green-400">
157
- <svg className="h-3 w-3" fill="none" viewBox="0 0 24 24" stroke="currentColor">
158
- <path
159
- strokeLinecap="round"
160
- strokeLinejoin="round"
161
- strokeWidth={2}
162
- d="M5 13l4 4L19 7"
163
- />
164
- </svg>
165
- {t('verified')}
166
- </span>
167
- ) : (
168
- <span className="inline-flex items-center gap-1 rounded-full bg-orange-100 px-2 py-0.5 text-xs font-medium text-orange-700 dark:bg-orange-950/30 dark:text-orange-400">
169
- <svg className="h-3 w-3" fill="none" viewBox="0 0 24 24" stroke="currentColor">
170
- <path
171
- strokeLinecap="round"
172
- strokeLinejoin="round"
173
- strokeWidth={2}
174
- d="M12 9v2m0 4h.01"
175
- />
176
- </svg>
177
- {t('unverified')}
178
- </span>
179
- )}
180
- </div>
181
-
182
- {profile.phone && <p className="text-muted-foreground mt-2 text-sm">{profile.phone}</p>}
183
-
184
- <p className="text-muted-foreground mt-3 text-xs">
185
- {t('memberSince')}{' '}
186
- {new Date(profile.createdAt).toLocaleDateString(undefined, {
187
- year: 'numeric',
188
- month: 'long',
189
- day: 'numeric',
190
- })}
191
- </p>
192
- </>
193
- )}
194
-
195
- {/* Success/Error message */}
196
- {message && (
197
- <p
198
- className={cn(
199
- 'mt-2 text-sm',
200
- message.type === 'success' ? 'text-green-600 dark:text-green-400' : 'text-red-600 dark:text-red-400'
201
- )}
202
- >
203
- {message.text}
204
- </p>
205
- )}
206
- </div>
207
- </div>
208
- </div>
209
- );
210
- }
1
+ 'use client';
2
+
3
+ import { useState } from 'react';
4
+ import type { CustomerProfile } from 'brainerce';
5
+ import { getClient } from '@/lib/brainerce';
6
+ import { useTranslations } from '@/lib/translations';
7
+ import { cn } from '@/lib/utils';
8
+
9
+ interface ProfileSectionProps {
10
+ profile: CustomerProfile;
11
+ onProfileUpdate?: (updated: CustomerProfile) => void;
12
+ className?: string;
13
+ }
14
+
15
+ export function ProfileSection({ profile, onProfileUpdate, className }: ProfileSectionProps) {
16
+ const t = useTranslations('account');
17
+ const [editing, setEditing] = useState(false);
18
+ const [saving, setSaving] = useState(false);
19
+ const [message, setMessage] = useState<{ type: 'success' | 'error'; text: string } | null>(null);
20
+ const [form, setForm] = useState({
21
+ firstName: profile.firstName || '',
22
+ lastName: profile.lastName || '',
23
+ phone: profile.phone || '',
24
+ });
25
+
26
+ const fullName = [profile.firstName, profile.lastName].filter(Boolean).join(' ');
27
+ const initials =
28
+ [profile.firstName?.[0], profile.lastName?.[0]].filter(Boolean).join('').toUpperCase() ||
29
+ profile.email[0].toUpperCase();
30
+
31
+ function startEditing() {
32
+ setForm({
33
+ firstName: profile.firstName || '',
34
+ lastName: profile.lastName || '',
35
+ phone: profile.phone || '',
36
+ });
37
+ setMessage(null);
38
+ setEditing(true);
39
+ }
40
+
41
+ function cancelEditing() {
42
+ setEditing(false);
43
+ setMessage(null);
44
+ }
45
+
46
+ async function handleSave(e: React.FormEvent) {
47
+ e.preventDefault();
48
+ setSaving(true);
49
+ setMessage(null);
50
+
51
+ try {
52
+ const client = getClient();
53
+ const updated = await client.updateMyProfile({
54
+ firstName: form.firstName || undefined,
55
+ lastName: form.lastName || undefined,
56
+ phone: form.phone || undefined,
57
+ });
58
+ onProfileUpdate?.(updated);
59
+ setEditing(false);
60
+ setMessage({ type: 'success', text: t('profileUpdated') });
61
+ setTimeout(() => setMessage(null), 3000);
62
+ } catch {
63
+ setMessage({ type: 'error', text: t('profileUpdateFailed') });
64
+ } finally {
65
+ setSaving(false);
66
+ }
67
+ }
68
+
69
+ return (
70
+ <div className={cn('border-border rounded-lg border p-6', className)}>
71
+ <div className="flex items-start gap-4">
72
+ {/* Avatar */}
73
+ <div className="bg-primary/10 text-primary flex h-14 w-14 flex-shrink-0 items-center justify-center rounded-full text-lg font-semibold">
74
+ {initials}
75
+ </div>
76
+
77
+ <div className="min-w-0 flex-1">
78
+ {editing ? (
79
+ <form onSubmit={handleSave} className="space-y-3">
80
+ <div className="grid grid-cols-2 gap-3">
81
+ <div>
82
+ <label className="text-muted-foreground mb-1 block text-xs font-medium">
83
+ {t('firstName')}
84
+ </label>
85
+ <input
86
+ type="text"
87
+ value={form.firstName}
88
+ onChange={(e) => setForm((f) => ({ ...f, firstName: e.target.value }))}
89
+ className="border-border bg-background text-foreground focus:border-primary w-full rounded-md border px-3 py-1.5 text-sm outline-none"
90
+ autoFocus
91
+ />
92
+ </div>
93
+ <div>
94
+ <label className="text-muted-foreground mb-1 block text-xs font-medium">
95
+ {t('lastName')}
96
+ </label>
97
+ <input
98
+ type="text"
99
+ value={form.lastName}
100
+ onChange={(e) => setForm((f) => ({ ...f, lastName: e.target.value }))}
101
+ className="border-border bg-background text-foreground focus:border-primary w-full rounded-md border px-3 py-1.5 text-sm outline-none"
102
+ />
103
+ </div>
104
+ </div>
105
+ <div>
106
+ <label className="text-muted-foreground mb-1 block text-xs font-medium">
107
+ {t('phone')}
108
+ </label>
109
+ <input
110
+ type="tel"
111
+ value={form.phone}
112
+ onChange={(e) => setForm((f) => ({ ...f, phone: e.target.value }))}
113
+ className="border-border bg-background text-foreground focus:border-primary w-full rounded-md border px-3 py-1.5 text-sm outline-none"
114
+ />
115
+ </div>
116
+ <p className="text-muted-foreground truncate text-sm">{profile.email}</p>
117
+ <div className="flex items-center gap-2">
118
+ <button
119
+ type="submit"
120
+ disabled={saving}
121
+ className="bg-primary text-primary-foreground rounded-md px-4 py-1.5 text-sm font-medium transition-opacity hover:opacity-90 disabled:opacity-50"
122
+ >
123
+ {saving ? '...' : t('save')}
124
+ </button>
125
+ <button
126
+ type="button"
127
+ onClick={cancelEditing}
128
+ disabled={saving}
129
+ className="text-muted-foreground hover:text-foreground rounded-md px-4 py-1.5 text-sm transition-colors disabled:opacity-50"
130
+ >
131
+ {t('cancel')}
132
+ </button>
133
+ </div>
134
+ </form>
135
+ ) : (
136
+ <>
137
+ <div className="flex items-center gap-2">
138
+ {fullName && (
139
+ <h2 className="text-foreground truncate text-lg font-semibold">{fullName}</h2>
140
+ )}
141
+ <button
142
+ type="button"
143
+ onClick={startEditing}
144
+ className="text-muted-foreground hover:text-foreground flex-shrink-0 rounded p-1 transition-colors"
145
+ title={t('editProfile')}
146
+ >
147
+ <svg
148
+ className="h-4 w-4"
149
+ fill="none"
150
+ viewBox="0 0 24 24"
151
+ stroke="currentColor"
152
+ strokeWidth={2}
153
+ >
154
+ <path
155
+ strokeLinecap="round"
156
+ strokeLinejoin="round"
157
+ d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"
158
+ />
159
+ </svg>
160
+ </button>
161
+ </div>
162
+ <p className="text-muted-foreground truncate text-sm">{profile.email}</p>
163
+
164
+ <div className="mt-2 flex items-center gap-2">
165
+ {profile.emailVerified ? (
166
+ <span className="inline-flex items-center gap-1 rounded-full bg-green-100 px-2 py-0.5 text-xs font-medium text-green-700 dark:bg-green-950/30 dark:text-green-400">
167
+ <svg className="h-3 w-3" fill="none" viewBox="0 0 24 24" stroke="currentColor">
168
+ <path
169
+ strokeLinecap="round"
170
+ strokeLinejoin="round"
171
+ strokeWidth={2}
172
+ d="M5 13l4 4L19 7"
173
+ />
174
+ </svg>
175
+ {t('verified')}
176
+ </span>
177
+ ) : (
178
+ <span className="inline-flex items-center gap-1 rounded-full bg-orange-100 px-2 py-0.5 text-xs font-medium text-orange-700 dark:bg-orange-950/30 dark:text-orange-400">
179
+ <svg className="h-3 w-3" fill="none" viewBox="0 0 24 24" stroke="currentColor">
180
+ <path
181
+ strokeLinecap="round"
182
+ strokeLinejoin="round"
183
+ strokeWidth={2}
184
+ d="M12 9v2m0 4h.01"
185
+ />
186
+ </svg>
187
+ {t('unverified')}
188
+ </span>
189
+ )}
190
+ </div>
191
+
192
+ {profile.phone && (
193
+ <p className="text-muted-foreground mt-2 text-sm">{profile.phone}</p>
194
+ )}
195
+
196
+ <p className="text-muted-foreground mt-3 text-xs">
197
+ {t('memberSince')}{' '}
198
+ {new Date(profile.createdAt).toLocaleDateString(undefined, {
199
+ year: 'numeric',
200
+ month: 'long',
201
+ day: 'numeric',
202
+ })}
203
+ </p>
204
+ </>
205
+ )}
206
+
207
+ {/* Success/Error message */}
208
+ {message && (
209
+ <p
210
+ className={cn(
211
+ 'mt-2 text-sm',
212
+ message.type === 'success'
213
+ ? 'text-green-600 dark:text-green-400'
214
+ : 'text-red-600 dark:text-red-400'
215
+ )}
216
+ >
217
+ {message.text}
218
+ </p>
219
+ )}
220
+ </div>
221
+ </div>
222
+ </div>
223
+ );
224
+ }
@@ -23,6 +23,7 @@ export function useStoreInfo() {
23
23
  // ---- Auth Context ----
24
24
  interface AuthContextValue {
25
25
  isLoggedIn: boolean;
26
+ authLoading: boolean;
26
27
  token: string | null;
27
28
  login: (token: string) => void;
28
29
  logout: () => void;
@@ -30,6 +31,7 @@ interface AuthContextValue {
30
31
 
31
32
  const AuthContext = createContext<AuthContextValue>({
32
33
  isLoggedIn: false,
34
+ authLoading: true,
33
35
  token: null,
34
36
  login: () => {},
35
37
  logout: () => {},
@@ -65,6 +67,7 @@ export function StoreProvider({ children }: { children: React.ReactNode }) {
65
67
  const [storeInfo, setStoreInfo] = useState<StoreInfo | null>(null);
66
68
  const [storeLoading, setStoreLoading] = useState(true);
67
69
  const [token, setToken] = useState<string | null>(null);
70
+ const [authLoading, setAuthLoading] = useState(true);
68
71
  const [cart, setCart] = useState<Cart | null>(null);
69
72
  const [cartLoading, setCartLoading] = useState(true);
70
73
 
@@ -75,6 +78,7 @@ export function StoreProvider({ children }: { children: React.ReactNode }) {
75
78
  if (stored) {
76
79
  setToken(stored);
77
80
  }
81
+ setAuthLoading(false);
78
82
 
79
83
  client
80
84
  .getStoreInfo()
@@ -134,7 +138,7 @@ export function StoreProvider({ children }: { children: React.ReactNode }) {
134
138
 
135
139
  return (
136
140
  <StoreInfoContext.Provider value={{ storeInfo, loading: storeLoading }}>
137
- <AuthContext.Provider value={{ isLoggedIn: !!token, token, login, logout }}>
141
+ <AuthContext.Provider value={{ isLoggedIn: !!token, authLoading, token, login, logout }}>
138
142
  <CartContext.Provider
139
143
  value={{ cart, cartLoading, refreshCart, itemCount, totals }}
140
144
  >