create-brainerce-store 1.9.1 → 1.11.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,193 +1,243 @@
1
- 'use client';
2
-
3
- import { Suspense, useEffect, useState } from 'react';
4
- import { useSearchParams } from 'next/navigation';
5
- import Link from 'next/link';
6
- import type { WaitForOrderResult } from 'brainerce';
7
- import { getClient } from '@/lib/brainerce';
8
- import { useCart } from '@/providers/store-provider';
9
- import { LoadingSpinner } from '@/components/shared/loading-spinner';
10
- import { useTranslations } from '@/lib/translations';
11
-
12
- function OrderConfirmationContent() {
13
- const searchParams = useSearchParams();
14
- const checkoutId = searchParams.get('checkout_id');
15
-
16
- const { refreshCart } = useCart();
17
- const t = useTranslations('orderConfirmation');
18
- const tc = useTranslations('common');
19
- const [result, setResult] = useState<WaitForOrderResult | null>(null);
20
- const [loading, setLoading] = useState(true);
21
- const [error, setError] = useState<string | null>(null);
22
-
23
- useEffect(() => {
24
- if (!checkoutId) {
25
- setError(t('missingCheckoutInfo'));
26
- setLoading(false);
27
- return;
28
- }
29
-
30
- async function waitForOrder() {
31
- try {
32
- const client = getClient();
33
-
34
- // Clear cart state after successful payment
35
- client.handlePaymentSuccess(checkoutId!);
36
- await refreshCart();
37
-
38
- const orderResult = await client.waitForOrder(checkoutId!, {
39
- maxWaitMs: 30000,
40
- });
41
- setResult(orderResult);
42
- } catch (err) {
43
- const message = err instanceof Error ? err.message : 'Failed to confirm order';
44
- setError(message);
45
- } finally {
46
- setLoading(false);
47
- }
48
- }
49
-
50
- waitForOrder();
51
- }, [checkoutId, refreshCart]);
52
-
53
- if (loading) {
54
- return (
55
- <div className="flex min-h-[60vh] flex-col items-center justify-center">
56
- <LoadingSpinner size="lg" />
57
- <p className="text-muted-foreground mt-4">{t('confirming')}</p>
58
- <p className="text-muted-foreground mt-1 text-xs">{t('confirmingHint')}</p>
59
- </div>
60
- );
61
- }
62
-
63
- if (error) {
64
- return (
65
- <div className="mx-auto max-w-2xl px-4 py-16 text-center sm:px-6 lg:px-8">
66
- <svg
67
- className="text-destructive mx-auto mb-4 h-16 w-16"
68
- fill="none"
69
- viewBox="0 0 24 24"
70
- stroke="currentColor"
71
- >
72
- <path
73
- strokeLinecap="round"
74
- strokeLinejoin="round"
75
- strokeWidth={1.5}
76
- d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.834-2.694-.834-3.464 0L3.34 16.5c-.77.833.192 2.5 1.732 2.5z"
77
- />
78
- </svg>
79
- <h1 className="text-foreground text-2xl font-bold">{t('errorTitle')}</h1>
80
- <p className="text-muted-foreground mt-2">{error}</p>
81
- <p className="text-muted-foreground mt-1 text-sm">{t('errorChargedHint')}</p>
82
- <Link
83
- href="/"
84
- className="bg-primary text-primary-foreground mt-6 inline-flex items-center rounded px-6 py-3 font-medium transition-opacity hover:opacity-90"
85
- >
86
- {t('returnHome')}
87
- </Link>
88
- </div>
89
- );
90
- }
91
-
92
- // Order was created successfully
93
- if (result?.success) {
94
- const orderNumber = result.status.orderNumber;
95
-
96
- return (
97
- <div className="mx-auto max-w-2xl px-4 py-16 text-center sm:px-6 lg:px-8">
98
- <svg
99
- className="text-primary mx-auto mb-4 h-16 w-16"
100
- fill="none"
101
- viewBox="0 0 24 24"
102
- stroke="currentColor"
103
- >
104
- <path
105
- strokeLinecap="round"
106
- strokeLinejoin="round"
107
- strokeWidth={1.5}
108
- d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
109
- />
110
- </svg>
111
-
112
- <h1 className="text-foreground text-2xl font-bold">{t('thankYou')}</h1>
113
-
114
- {orderNumber && (
115
- <p className="text-foreground mt-3 text-lg">
116
- {t('orderNumber')} <span className="font-semibold">{orderNumber}</span>
117
- </p>
118
- )}
119
-
120
- <p className="text-muted-foreground mt-2">{t('confirmationEmail')}</p>
121
-
122
- <div className="mt-8 flex flex-col items-center justify-center gap-3 sm:flex-row">
123
- <Link
124
- href="/products"
125
- className="bg-primary text-primary-foreground inline-flex items-center rounded px-6 py-3 font-medium transition-opacity hover:opacity-90"
126
- >
127
- {tc('continueShopping')}
128
- </Link>
129
-
130
- <Link
131
- href="/account"
132
- className="border-border text-foreground hover:bg-muted inline-flex items-center rounded border px-6 py-3 font-medium transition-colors"
133
- >
134
- {t('viewOrders')}
135
- </Link>
136
- </div>
137
- </div>
138
- );
139
- }
140
-
141
- // Order not yet confirmed (polling timed out) - still show success
142
- return (
143
- <div className="mx-auto max-w-2xl px-4 py-16 text-center sm:px-6 lg:px-8">
144
- <svg
145
- className="text-primary mx-auto mb-4 h-16 w-16"
146
- fill="none"
147
- viewBox="0 0 24 24"
148
- stroke="currentColor"
149
- >
150
- <path
151
- strokeLinecap="round"
152
- strokeLinejoin="round"
153
- strokeWidth={1.5}
154
- d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
155
- />
156
- </svg>
157
-
158
- <h1 className="text-foreground text-2xl font-bold">{t('paymentReceived')}</h1>
159
-
160
- <p className="text-muted-foreground mt-2">{t('orderProcessing')}</p>
161
-
162
- <div className="mt-8 flex flex-col items-center justify-center gap-3 sm:flex-row">
163
- <Link
164
- href="/products"
165
- className="bg-primary text-primary-foreground inline-flex items-center rounded px-6 py-3 font-medium transition-opacity hover:opacity-90"
166
- >
167
- {tc('continueShopping')}
168
- </Link>
169
-
170
- <Link
171
- href="/account"
172
- className="border-border text-foreground hover:bg-muted inline-flex items-center rounded border px-6 py-3 font-medium transition-colors"
173
- >
174
- {t('viewOrders')}
175
- </Link>
176
- </div>
177
- </div>
178
- );
179
- }
180
-
181
- export default function OrderConfirmationPage() {
182
- return (
183
- <Suspense
184
- fallback={
185
- <div className="flex min-h-[60vh] items-center justify-center">
186
- <LoadingSpinner size="lg" />
187
- </div>
188
- }
189
- >
190
- <OrderConfirmationContent />
191
- </Suspense>
192
- );
193
- }
1
+ 'use client';
2
+
3
+ import { Suspense, useEffect, useState } from 'react';
4
+ import { useSearchParams } from 'next/navigation';
5
+ import Link from 'next/link';
6
+ import type { WaitForOrderResult, OrderDownloadLink } from 'brainerce';
7
+ import { getClient } from '@/lib/brainerce';
8
+ import { useCart } from '@/providers/store-provider';
9
+ import { LoadingSpinner } from '@/components/shared/loading-spinner';
10
+ import { useTranslations } from '@/lib/translations';
11
+
12
+ function OrderConfirmationContent() {
13
+ const searchParams = useSearchParams();
14
+ const checkoutId = searchParams.get('checkout_id');
15
+
16
+ const { refreshCart } = useCart();
17
+ const t = useTranslations('orderConfirmation');
18
+ const tc = useTranslations('common');
19
+ const [result, setResult] = useState<WaitForOrderResult | null>(null);
20
+ const [loading, setLoading] = useState(true);
21
+ const [error, setError] = useState<string | null>(null);
22
+
23
+ useEffect(() => {
24
+ if (!checkoutId) {
25
+ setError(t('missingCheckoutInfo'));
26
+ setLoading(false);
27
+ return;
28
+ }
29
+
30
+ async function waitForOrder() {
31
+ try {
32
+ const client = getClient();
33
+
34
+ // Clear cart state after successful payment
35
+ client.handlePaymentSuccess(checkoutId!);
36
+ await refreshCart();
37
+
38
+ const orderResult = await client.waitForOrder(checkoutId!, {
39
+ maxWaitMs: 30000,
40
+ });
41
+ setResult(orderResult);
42
+ } catch (err) {
43
+ const message = err instanceof Error ? err.message : 'Failed to confirm order';
44
+ setError(message);
45
+ } finally {
46
+ setLoading(false);
47
+ }
48
+ }
49
+
50
+ waitForOrder();
51
+ }, [checkoutId, refreshCart]);
52
+
53
+ if (loading) {
54
+ return (
55
+ <div className="flex min-h-[60vh] flex-col items-center justify-center">
56
+ <LoadingSpinner size="lg" />
57
+ <p className="text-muted-foreground mt-4">{t('confirming')}</p>
58
+ <p className="text-muted-foreground mt-1 text-xs">{t('confirmingHint')}</p>
59
+ </div>
60
+ );
61
+ }
62
+
63
+ if (error) {
64
+ return (
65
+ <div className="mx-auto max-w-2xl px-4 py-16 text-center sm:px-6 lg:px-8">
66
+ <svg
67
+ className="text-destructive mx-auto mb-4 h-16 w-16"
68
+ fill="none"
69
+ viewBox="0 0 24 24"
70
+ stroke="currentColor"
71
+ >
72
+ <path
73
+ strokeLinecap="round"
74
+ strokeLinejoin="round"
75
+ strokeWidth={1.5}
76
+ d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.834-2.694-.834-3.464 0L3.34 16.5c-.77.833.192 2.5 1.732 2.5z"
77
+ />
78
+ </svg>
79
+ <h1 className="text-foreground text-2xl font-bold">{t('errorTitle')}</h1>
80
+ <p className="text-muted-foreground mt-2">{error}</p>
81
+ <p className="text-muted-foreground mt-1 text-sm">{t('errorChargedHint')}</p>
82
+ <Link
83
+ href="/"
84
+ className="bg-primary text-primary-foreground mt-6 inline-flex items-center rounded px-6 py-3 font-medium transition-opacity hover:opacity-90"
85
+ >
86
+ {t('returnHome')}
87
+ </Link>
88
+ </div>
89
+ );
90
+ }
91
+
92
+ // Order was created successfully
93
+ if (result?.success) {
94
+ const orderNumber = result.status.orderNumber;
95
+
96
+ return (
97
+ <div className="mx-auto max-w-2xl px-4 py-16 text-center sm:px-6 lg:px-8">
98
+ <svg
99
+ className="text-primary mx-auto mb-4 h-16 w-16"
100
+ fill="none"
101
+ viewBox="0 0 24 24"
102
+ stroke="currentColor"
103
+ >
104
+ <path
105
+ strokeLinecap="round"
106
+ strokeLinejoin="round"
107
+ strokeWidth={1.5}
108
+ d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
109
+ />
110
+ </svg>
111
+
112
+ <h1 className="text-foreground text-2xl font-bold">{t('thankYou')}</h1>
113
+
114
+ {orderNumber && (
115
+ <p className="text-foreground mt-3 text-lg">
116
+ {t('orderNumber')} <span className="font-semibold">{orderNumber}</span>
117
+ </p>
118
+ )}
119
+
120
+ <p className="text-muted-foreground mt-2">{t('confirmationEmail')}</p>
121
+
122
+ {result.status.orderId && <ConfirmationDownloads orderId={result.status.orderId} />}
123
+
124
+ <div className="mt-8 flex flex-col items-center justify-center gap-3 sm:flex-row">
125
+ <Link
126
+ href="/products"
127
+ className="bg-primary text-primary-foreground inline-flex items-center rounded px-6 py-3 font-medium transition-opacity hover:opacity-90"
128
+ >
129
+ {tc('continueShopping')}
130
+ </Link>
131
+
132
+ <Link
133
+ href="/account"
134
+ className="border-border text-foreground hover:bg-muted inline-flex items-center rounded border px-6 py-3 font-medium transition-colors"
135
+ >
136
+ {t('viewOrders')}
137
+ </Link>
138
+ </div>
139
+ </div>
140
+ );
141
+ }
142
+
143
+ // Order not yet confirmed (polling timed out) - still show success
144
+ return (
145
+ <div className="mx-auto max-w-2xl px-4 py-16 text-center sm:px-6 lg:px-8">
146
+ <svg
147
+ className="text-primary mx-auto mb-4 h-16 w-16"
148
+ fill="none"
149
+ viewBox="0 0 24 24"
150
+ stroke="currentColor"
151
+ >
152
+ <path
153
+ strokeLinecap="round"
154
+ strokeLinejoin="round"
155
+ strokeWidth={1.5}
156
+ d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
157
+ />
158
+ </svg>
159
+
160
+ <h1 className="text-foreground text-2xl font-bold">{t('paymentReceived')}</h1>
161
+
162
+ <p className="text-muted-foreground mt-2">{t('orderProcessing')}</p>
163
+
164
+ <div className="mt-8 flex flex-col items-center justify-center gap-3 sm:flex-row">
165
+ <Link
166
+ href="/products"
167
+ className="bg-primary text-primary-foreground inline-flex items-center rounded px-6 py-3 font-medium transition-opacity hover:opacity-90"
168
+ >
169
+ {tc('continueShopping')}
170
+ </Link>
171
+
172
+ <Link
173
+ href="/account"
174
+ className="border-border text-foreground hover:bg-muted inline-flex items-center rounded border px-6 py-3 font-medium transition-colors"
175
+ >
176
+ {t('viewOrders')}
177
+ </Link>
178
+ </div>
179
+ </div>
180
+ );
181
+ }
182
+
183
+ function ConfirmationDownloads({ orderId }: { orderId: string }) {
184
+ const t = useTranslations('orderConfirmation');
185
+ const [downloads, setDownloads] = useState<OrderDownloadLink[] | null>(null);
186
+
187
+ useEffect(() => {
188
+ let cancelled = false;
189
+ async function fetchDownloads() {
190
+ try {
191
+ const client = getClient();
192
+ const links = await client.getOrderDownloads(orderId);
193
+ if (!cancelled && links.length > 0) setDownloads(links);
194
+ } catch {
195
+ // Not all orders have downloads - silently ignore
196
+ }
197
+ }
198
+ fetchDownloads();
199
+ return () => {
200
+ cancelled = true;
201
+ };
202
+ }, [orderId]);
203
+
204
+ if (!downloads || downloads.length === 0) return null;
205
+
206
+ return (
207
+ <div className="border-border bg-muted/30 mx-auto mt-8 max-w-md rounded-lg border p-6 text-start">
208
+ <h3 className="text-foreground mb-3 text-sm font-semibold">{t('yourDownloads')}</h3>
209
+ <div className="space-y-2">
210
+ {downloads.map((link, idx) => (
211
+ <div key={idx} className="flex items-center justify-between gap-3">
212
+ <div className="min-w-0 flex-1">
213
+ <p className="text-foreground truncate text-sm">{link.fileName}</p>
214
+ <p className="text-muted-foreground truncate text-xs">{link.productName}</p>
215
+ </div>
216
+ <a
217
+ href={link.downloadUrl}
218
+ target="_blank"
219
+ rel="noopener noreferrer"
220
+ className="bg-primary text-primary-foreground flex-shrink-0 rounded px-3 py-1.5 text-xs font-medium hover:opacity-90"
221
+ >
222
+ {t('download')}
223
+ </a>
224
+ </div>
225
+ ))}
226
+ </div>
227
+ </div>
228
+ );
229
+ }
230
+
231
+ export default function OrderConfirmationPage() {
232
+ return (
233
+ <Suspense
234
+ fallback={
235
+ <div className="flex min-h-[60vh] items-center justify-center">
236
+ <LoadingSpinner size="lg" />
237
+ </div>
238
+ }
239
+ >
240
+ <OrderConfirmationContent />
241
+ </Suspense>
242
+ );
243
+ }