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.
- package/messages/en.json +266 -266
- package/messages/he.json +266 -266
- package/package.json +45 -45
- package/templates/nextjs/base/src/app/account/page.tsx +112 -108
- package/templates/nextjs/base/src/components/account/profile-section.tsx +224 -210
- package/templates/nextjs/base/src/providers/store-provider.tsx.ejs +5 -1
|
@@ -1,108 +1,112 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import { useEffect, useState } from 'react';
|
|
4
|
-
import { useRouter } from 'next/navigation';
|
|
5
|
-
import type { CustomerProfile, Order } from 'brainerce';
|
|
6
|
-
import { getClient } from '@/lib/brainerce';
|
|
7
|
-
import { useAuth } from '@/providers/store-provider';
|
|
8
|
-
import { LoadingSpinner } from '@/components/shared/loading-spinner';
|
|
9
|
-
import { ProfileSection } from '@/components/account/profile-section';
|
|
10
|
-
import { OrderHistory } from '@/components/account/order-history';
|
|
11
|
-
import { useTranslations } from '@/lib/translations';
|
|
12
|
-
|
|
13
|
-
export default function AccountPage() {
|
|
14
|
-
const router = useRouter();
|
|
15
|
-
const { isLoggedIn, logout } = useAuth();
|
|
16
|
-
const t = useTranslations('account');
|
|
17
|
-
const tc = useTranslations('common');
|
|
18
|
-
const [profile, setProfile] = useState<CustomerProfile | null>(null);
|
|
19
|
-
const [orders, setOrders] = useState<Order[]>([]);
|
|
20
|
-
const [loading, setLoading] = useState(true);
|
|
21
|
-
const [error, setError] = useState<string | null>(null);
|
|
22
|
-
|
|
23
|
-
useEffect(() => {
|
|
24
|
-
if (
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
<
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
{
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState } from 'react';
|
|
4
|
+
import { useRouter } from 'next/navigation';
|
|
5
|
+
import type { CustomerProfile, Order } from 'brainerce';
|
|
6
|
+
import { getClient } from '@/lib/brainerce';
|
|
7
|
+
import { useAuth } from '@/providers/store-provider';
|
|
8
|
+
import { LoadingSpinner } from '@/components/shared/loading-spinner';
|
|
9
|
+
import { ProfileSection } from '@/components/account/profile-section';
|
|
10
|
+
import { OrderHistory } from '@/components/account/order-history';
|
|
11
|
+
import { useTranslations } from '@/lib/translations';
|
|
12
|
+
|
|
13
|
+
export default function AccountPage() {
|
|
14
|
+
const router = useRouter();
|
|
15
|
+
const { isLoggedIn, authLoading, logout } = useAuth();
|
|
16
|
+
const t = useTranslations('account');
|
|
17
|
+
const tc = useTranslations('common');
|
|
18
|
+
const [profile, setProfile] = useState<CustomerProfile | null>(null);
|
|
19
|
+
const [orders, setOrders] = useState<Order[]>([]);
|
|
20
|
+
const [loading, setLoading] = useState(true);
|
|
21
|
+
const [error, setError] = useState<string | null>(null);
|
|
22
|
+
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
if (authLoading) return;
|
|
25
|
+
|
|
26
|
+
if (!isLoggedIn) {
|
|
27
|
+
router.push('/login');
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function loadAccountData() {
|
|
32
|
+
try {
|
|
33
|
+
const client = getClient();
|
|
34
|
+
const [profileResult, ordersResult] = await Promise.allSettled([
|
|
35
|
+
client.getMyProfile(),
|
|
36
|
+
client.getMyOrders({ limit: 20 }),
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
if (profileResult.status === 'fulfilled') {
|
|
40
|
+
setProfile(profileResult.value);
|
|
41
|
+
} else {
|
|
42
|
+
setError('Failed to load profile.');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (ordersResult.status === 'fulfilled') {
|
|
46
|
+
setOrders(ordersResult.value.data);
|
|
47
|
+
}
|
|
48
|
+
} catch (err) {
|
|
49
|
+
const message = err instanceof Error ? err.message : 'Failed to load account data.';
|
|
50
|
+
setError(message);
|
|
51
|
+
} finally {
|
|
52
|
+
setLoading(false);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
loadAccountData();
|
|
57
|
+
}, [isLoggedIn, authLoading, router]);
|
|
58
|
+
|
|
59
|
+
if (authLoading || !isLoggedIn) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (loading) {
|
|
64
|
+
return (
|
|
65
|
+
<div className="flex min-h-[60vh] items-center justify-center">
|
|
66
|
+
<LoadingSpinner size="lg" />
|
|
67
|
+
</div>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (error && !profile) {
|
|
72
|
+
return (
|
|
73
|
+
<div className="mx-auto max-w-3xl px-4 py-16 text-center sm:px-6 lg:px-8">
|
|
74
|
+
<h1 className="text-foreground text-2xl font-bold">{tc('error')}</h1>
|
|
75
|
+
<p className="text-muted-foreground mt-2">{error}</p>
|
|
76
|
+
<button
|
|
77
|
+
type="button"
|
|
78
|
+
onClick={() => window.location.reload()}
|
|
79
|
+
className="bg-primary text-primary-foreground mt-6 inline-flex items-center rounded px-6 py-3 font-medium transition-opacity hover:opacity-90"
|
|
80
|
+
>
|
|
81
|
+
{tc('tryAgain')}
|
|
82
|
+
</button>
|
|
83
|
+
</div>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<div className="mx-auto max-w-3xl px-4 py-8 sm:px-6 lg:px-8">
|
|
89
|
+
<div className="mb-6 flex items-center justify-between">
|
|
90
|
+
<h1 className="text-foreground text-2xl font-bold">{t('myAccount')}</h1>
|
|
91
|
+
<button
|
|
92
|
+
type="button"
|
|
93
|
+
onClick={logout}
|
|
94
|
+
className="text-muted-foreground hover:text-foreground text-sm transition-colors"
|
|
95
|
+
>
|
|
96
|
+
{t('signOut')}
|
|
97
|
+
</button>
|
|
98
|
+
</div>
|
|
99
|
+
|
|
100
|
+
{/* Profile Section */}
|
|
101
|
+
{profile && (
|
|
102
|
+
<ProfileSection profile={profile} onProfileUpdate={setProfile} className="mb-8" />
|
|
103
|
+
)}
|
|
104
|
+
|
|
105
|
+
{/* Order History */}
|
|
106
|
+
<div>
|
|
107
|
+
<h2 className="text-foreground mb-4 text-lg font-semibold">{t('orderHistory')}</h2>
|
|
108
|
+
<OrderHistory orders={orders} />
|
|
109
|
+
</div>
|
|
110
|
+
</div>
|
|
111
|
+
);
|
|
112
|
+
}
|