create-brainerce-store 1.35.0 → 1.37.0
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/dist/index.js +1 -1
- package/package.json +1 -1
- package/templates/nextjs/base/src/app/products/page.tsx +169 -6
- package/templates/nextjs/base/src/components/reviews/review-form.tsx +211 -51
- package/templates/nextjs/base/src/components/reviews/reviews-section.tsx +11 -13
- package/templates/nextjs/base/src/components/seo/product-json-ld.tsx +116 -116
package/dist/index.js
CHANGED
|
@@ -31,7 +31,7 @@ var require_package = __commonJS({
|
|
|
31
31
|
"package.json"(exports2, module2) {
|
|
32
32
|
module2.exports = {
|
|
33
33
|
name: "create-brainerce-store",
|
|
34
|
-
version: "1.
|
|
34
|
+
version: "1.37.0",
|
|
35
35
|
description: "Scaffold a production-ready e-commerce storefront connected to Brainerce",
|
|
36
36
|
bin: {
|
|
37
37
|
"create-brainerce-store": "dist/index.js"
|
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import { Suspense, useEffect, useState, useCallback, useRef } from 'react';
|
|
3
|
+
import { Suspense, useEffect, useMemo, useState, useCallback, useRef } from 'react';
|
|
4
4
|
import { useSearchParams } from 'next/navigation';
|
|
5
5
|
import { useRouter } from '@/lib/navigation';
|
|
6
|
-
import type { Product } from 'brainerce';
|
|
7
|
-
import type { ProductQueryParams } from 'brainerce';
|
|
6
|
+
import type { Product, ProductQueryParams, PublicMetafieldDefinition } from 'brainerce';
|
|
8
7
|
import { getClient } from '@/lib/brainerce';
|
|
9
8
|
import { ProductGrid } from '@/components/products/product-grid';
|
|
10
9
|
import { LoadingSpinner } from '@/components/shared/loading-spinner';
|
|
@@ -13,6 +12,18 @@ import { cn } from '@/lib/utils';
|
|
|
13
12
|
|
|
14
13
|
const PAGE_SIZE = 20;
|
|
15
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Metafield (custom-field) values are passed in the URL as `mf_<key>=v1,v2`
|
|
17
|
+
* for readability (no JSON encoding). The SDK assembles them back into
|
|
18
|
+
* `{ [key]: string[] }` before calling `client.getProducts`.
|
|
19
|
+
*
|
|
20
|
+
* Only definitions with `filterable: true` of type SELECT / MULTI_SELECT /
|
|
21
|
+
* BOOLEAN are honored server-side — the storefront renders facets only for
|
|
22
|
+
* those.
|
|
23
|
+
*/
|
|
24
|
+
const METAFIELD_URL_PREFIX = 'mf_';
|
|
25
|
+
const FILTERABLE_TYPES = new Set(['SELECT', 'MULTI_SELECT', 'BOOLEAN']);
|
|
26
|
+
|
|
16
27
|
type SortOption = {
|
|
17
28
|
labelKey: 'sortNewest' | 'sortNameAZ' | 'sortNameZA' | 'sortPriceLow' | 'sortPriceHigh';
|
|
18
29
|
sortBy: ProductQueryParams['sortBy'];
|
|
@@ -239,26 +250,52 @@ function ProductsContent() {
|
|
|
239
250
|
const [categories, setCategories] = useState<CategoryNode[]>([]);
|
|
240
251
|
const [brands, setBrands] = useState<Array<{ id: string; name: string }>>([]);
|
|
241
252
|
const [tags, setTags] = useState<Array<{ id: string; name: string }>>([]);
|
|
253
|
+
const [metafieldDefs, setMetafieldDefs] = useState<PublicMetafieldDefinition[]>([]);
|
|
242
254
|
|
|
243
255
|
const sortIndex = parseInt(sortParam, 10) || 0;
|
|
244
256
|
const currentSort = sortOptions[sortIndex] || sortOptions[0];
|
|
245
257
|
|
|
246
|
-
// Load categories, brands, and
|
|
258
|
+
// Load categories, brands, tags, and metafield definitions (for custom-field facets).
|
|
259
|
+
// The metafield-definitions endpoint already returns only definitions
|
|
260
|
+
// published to this sales channel; we filter client-side to the ones flagged
|
|
261
|
+
// `filterable: true` and of a UI-renderable type.
|
|
247
262
|
useEffect(() => {
|
|
248
263
|
async function loadFilters() {
|
|
249
264
|
const client = getClient();
|
|
250
|
-
const [catRes, brandRes, tagRes] = await Promise.allSettled([
|
|
265
|
+
const [catRes, brandRes, tagRes, mfRes] = await Promise.allSettled([
|
|
251
266
|
client.getCategories(),
|
|
252
267
|
client.getBrands(),
|
|
253
268
|
client.getTags(),
|
|
269
|
+
client.getPublicMetafieldDefinitions(),
|
|
254
270
|
]);
|
|
255
271
|
if (catRes.status === 'fulfilled') setCategories(catRes.value.categories as CategoryNode[]);
|
|
256
272
|
if (brandRes.status === 'fulfilled') setBrands(brandRes.value.brands);
|
|
257
273
|
if (tagRes.status === 'fulfilled') setTags(tagRes.value.tags);
|
|
274
|
+
if (mfRes.status === 'fulfilled') {
|
|
275
|
+
setMetafieldDefs(
|
|
276
|
+
mfRes.value.definitions.filter(
|
|
277
|
+
(d) => d.filterable === true && FILTERABLE_TYPES.has(d.type)
|
|
278
|
+
)
|
|
279
|
+
);
|
|
280
|
+
}
|
|
258
281
|
}
|
|
259
282
|
loadFilters();
|
|
260
283
|
}, []);
|
|
261
284
|
|
|
285
|
+
// Stable serialized signature of the selected metafield facets — used as
|
|
286
|
+
// the `loadProducts` dependency (and to derive the SDK shape). When no
|
|
287
|
+
// facets are selected, this is `''` so the SDK call omits `metafields`
|
|
288
|
+
// entirely and the backend short-circuits the filter branch.
|
|
289
|
+
const metafieldsKey = useMemo(() => {
|
|
290
|
+
if (metafieldDefs.length === 0) return '';
|
|
291
|
+
const parts: string[] = [];
|
|
292
|
+
for (const def of metafieldDefs) {
|
|
293
|
+
const raw = searchParams.get(`${METAFIELD_URL_PREFIX}${def.key}`);
|
|
294
|
+
if (raw) parts.push(`${def.key}=${raw}`);
|
|
295
|
+
}
|
|
296
|
+
return parts.sort().join('&');
|
|
297
|
+
}, [metafieldDefs, searchParams]);
|
|
298
|
+
|
|
262
299
|
// Load products when filters change
|
|
263
300
|
const loadProducts = useCallback(
|
|
264
301
|
async (pageNum: number, append: boolean) => {
|
|
@@ -281,6 +318,24 @@ function ProductsContent() {
|
|
|
281
318
|
if (categoryId) params.categories = categoryId;
|
|
282
319
|
if (brandId) params.brands = brandId;
|
|
283
320
|
if (tagId) params.tags = tagId;
|
|
321
|
+
// Build the `metafields` shape from `metafieldsKey` (each segment is
|
|
322
|
+
// `key=v1,v2`). Equivalent to reading the URL params directly but
|
|
323
|
+
// tied to the stable signature already in this callback's deps.
|
|
324
|
+
if (metafieldsKey) {
|
|
325
|
+
const mf: Record<string, string[]> = {};
|
|
326
|
+
for (const segment of metafieldsKey.split('&')) {
|
|
327
|
+
const eq = segment.indexOf('=');
|
|
328
|
+
if (eq < 0) continue;
|
|
329
|
+
const k = segment.slice(0, eq);
|
|
330
|
+
const v = segment
|
|
331
|
+
.slice(eq + 1)
|
|
332
|
+
.split(',')
|
|
333
|
+
.map((s) => s.trim())
|
|
334
|
+
.filter(Boolean);
|
|
335
|
+
if (v.length > 0) mf[k] = v;
|
|
336
|
+
}
|
|
337
|
+
if (Object.keys(mf).length > 0) params.metafields = mf;
|
|
338
|
+
}
|
|
284
339
|
|
|
285
340
|
const result = await client.getProducts(params);
|
|
286
341
|
|
|
@@ -299,7 +354,15 @@ function ProductsContent() {
|
|
|
299
354
|
setLoadingMore(false);
|
|
300
355
|
}
|
|
301
356
|
},
|
|
302
|
-
[
|
|
357
|
+
[
|
|
358
|
+
searchQuery,
|
|
359
|
+
categoryId,
|
|
360
|
+
brandId,
|
|
361
|
+
tagId,
|
|
362
|
+
currentSort.sortBy,
|
|
363
|
+
currentSort.sortOrder,
|
|
364
|
+
metafieldsKey,
|
|
365
|
+
]
|
|
303
366
|
);
|
|
304
367
|
|
|
305
368
|
useEffect(() => {
|
|
@@ -326,6 +389,24 @@ function ProductsContent() {
|
|
|
326
389
|
updateParam('category', id);
|
|
327
390
|
}
|
|
328
391
|
|
|
392
|
+
/** Read the currently-selected values for a given metafield definition. */
|
|
393
|
+
function getSelectedMetafieldValues(key: string): string[] {
|
|
394
|
+
const raw = searchParams.get(`${METAFIELD_URL_PREFIX}${key}`);
|
|
395
|
+
if (!raw) return [];
|
|
396
|
+
return raw
|
|
397
|
+
.split(',')
|
|
398
|
+
.map((v) => v.trim())
|
|
399
|
+
.filter(Boolean);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Toggle one value on/off in a metafield facet's URL param. Multi-select
|
|
404
|
+
* adds/removes from the list; single-select / boolean replace the value.
|
|
405
|
+
*/
|
|
406
|
+
function setMetafieldValue(key: string, values: string[]) {
|
|
407
|
+
updateParam(`${METAFIELD_URL_PREFIX}${key}`, values.join(','));
|
|
408
|
+
}
|
|
409
|
+
|
|
329
410
|
return (
|
|
330
411
|
<div className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
|
|
331
412
|
{/* Page Header */}
|
|
@@ -402,6 +483,88 @@ function ProductsContent() {
|
|
|
402
483
|
</select>
|
|
403
484
|
)}
|
|
404
485
|
|
|
486
|
+
{/* Custom-field (metafield) facets — rendered per definition type.
|
|
487
|
+
Only definitions flagged `filterable: true` and published to
|
|
488
|
+
this sales channel reach this list (see `metafieldDefs` filter
|
|
489
|
+
above). The merchant's display name (`def.name`) is shown as-is
|
|
490
|
+
— `enumValues` come from the merchant's dashboard config. */}
|
|
491
|
+
{metafieldDefs.map((def) => {
|
|
492
|
+
const selected = getSelectedMetafieldValues(def.key);
|
|
493
|
+
|
|
494
|
+
if (def.type === 'BOOLEAN') {
|
|
495
|
+
const isOn = selected.includes('true');
|
|
496
|
+
return (
|
|
497
|
+
<label
|
|
498
|
+
key={def.id}
|
|
499
|
+
className="border-border text-foreground hover:border-primary inline-flex h-9 cursor-pointer items-center gap-2 rounded border px-3 text-sm transition-colors"
|
|
500
|
+
>
|
|
501
|
+
<input
|
|
502
|
+
type="checkbox"
|
|
503
|
+
checked={isOn}
|
|
504
|
+
onChange={(e) => setMetafieldValue(def.key, e.target.checked ? ['true'] : [])}
|
|
505
|
+
className="accent-primary h-4 w-4"
|
|
506
|
+
/>
|
|
507
|
+
{def.name}
|
|
508
|
+
</label>
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
if (def.type === 'SELECT') {
|
|
513
|
+
return (
|
|
514
|
+
<select
|
|
515
|
+
key={def.id}
|
|
516
|
+
value={selected[0] || ''}
|
|
517
|
+
onChange={(e) =>
|
|
518
|
+
setMetafieldValue(def.key, e.target.value ? [e.target.value] : [])
|
|
519
|
+
}
|
|
520
|
+
aria-label={def.name}
|
|
521
|
+
className="border-border bg-background text-foreground focus:ring-primary/20 focus:border-primary h-9 rounded border px-3 text-sm focus:outline-none focus:ring-2"
|
|
522
|
+
>
|
|
523
|
+
<option value="">{`${tc('all')} ${def.name}`}</option>
|
|
524
|
+
{(def.enumValues || []).map((v) => (
|
|
525
|
+
<option key={v} value={v}>
|
|
526
|
+
{v}
|
|
527
|
+
</option>
|
|
528
|
+
))}
|
|
529
|
+
</select>
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// MULTI_SELECT — chip toggles. Each click adds/removes one value.
|
|
534
|
+
return (
|
|
535
|
+
<div
|
|
536
|
+
key={def.id}
|
|
537
|
+
className="border-border inline-flex items-center gap-1 rounded border px-2 py-1"
|
|
538
|
+
aria-label={def.name}
|
|
539
|
+
>
|
|
540
|
+
<span className="text-muted-foreground px-1 text-xs">{def.name}:</span>
|
|
541
|
+
{(def.enumValues || []).map((v) => {
|
|
542
|
+
const active = selected.includes(v);
|
|
543
|
+
return (
|
|
544
|
+
<button
|
|
545
|
+
key={v}
|
|
546
|
+
type="button"
|
|
547
|
+
onClick={() =>
|
|
548
|
+
setMetafieldValue(
|
|
549
|
+
def.key,
|
|
550
|
+
active ? selected.filter((s) => s !== v) : [...selected, v]
|
|
551
|
+
)
|
|
552
|
+
}
|
|
553
|
+
className={cn(
|
|
554
|
+
'rounded-full border px-2 py-0.5 text-xs transition-colors',
|
|
555
|
+
active
|
|
556
|
+
? 'bg-primary text-primary-foreground border-primary'
|
|
557
|
+
: 'border-border text-muted-foreground hover:border-primary hover:text-foreground'
|
|
558
|
+
)}
|
|
559
|
+
>
|
|
560
|
+
{v}
|
|
561
|
+
</button>
|
|
562
|
+
);
|
|
563
|
+
})}
|
|
564
|
+
</div>
|
|
565
|
+
);
|
|
566
|
+
})}
|
|
567
|
+
|
|
405
568
|
{/* Sort */}
|
|
406
569
|
<div className="flex items-center gap-2 sm:ms-auto">
|
|
407
570
|
<label htmlFor="sort" className="text-muted-foreground whitespace-nowrap text-sm">
|
|
@@ -1,17 +1,193 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import { useState } from 'react';
|
|
4
|
-
import {
|
|
3
|
+
import { useEffect, useState } from 'react';
|
|
4
|
+
import { getClient } from '@/lib/brainerce';
|
|
5
|
+
import { checkAuthStatus } from '@/lib/auth';
|
|
6
|
+
import type { MyProductReview, ProductReview } from 'brainerce';
|
|
5
7
|
|
|
6
8
|
interface ReviewFormProps {
|
|
7
9
|
productId: string;
|
|
8
10
|
}
|
|
9
11
|
|
|
12
|
+
type Stage =
|
|
13
|
+
| { kind: 'loading' }
|
|
14
|
+
| { kind: 'signed_out' }
|
|
15
|
+
| { kind: 'not_eligible'; reason: MyProductReview['reason'] }
|
|
16
|
+
| { kind: 'submit' }
|
|
17
|
+
| { kind: 'edit'; review: ProductReview };
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Single component that decides which UI to render for a customer on a PDP:
|
|
21
|
+
* - Loading while we check auth state and eligibility
|
|
22
|
+
* - Signed out -> sign-in CTA
|
|
23
|
+
* - Signed in but not eligible -> "purchase this product to review"
|
|
24
|
+
* - Eligible without review -> submit form
|
|
25
|
+
* - Eligible with existing review -> edit/delete form (rating + body prefilled)
|
|
26
|
+
*/
|
|
10
27
|
export function ReviewForm({ productId }: ReviewFormProps) {
|
|
11
|
-
const [
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
28
|
+
const [stage, setStage] = useState<Stage>({ kind: 'loading' });
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
let cancelled = false;
|
|
32
|
+
async function load() {
|
|
33
|
+
const auth = await checkAuthStatus();
|
|
34
|
+
if (cancelled) return;
|
|
35
|
+
if (!auth.isLoggedIn) {
|
|
36
|
+
setStage({ kind: 'signed_out' });
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const me = await getClient().getMyProductReview(productId);
|
|
41
|
+
if (cancelled) return;
|
|
42
|
+
if (!me.eligible) {
|
|
43
|
+
setStage({ kind: 'not_eligible', reason: me.reason });
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (me.myReview) {
|
|
47
|
+
setStage({ kind: 'edit', review: me.myReview });
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
setStage({ kind: 'submit' });
|
|
51
|
+
} catch {
|
|
52
|
+
if (!cancelled) setStage({ kind: 'not_eligible', reason: 'product_not_found' });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
void load();
|
|
56
|
+
return () => {
|
|
57
|
+
cancelled = true;
|
|
58
|
+
};
|
|
59
|
+
}, [productId]);
|
|
60
|
+
|
|
61
|
+
if (stage.kind === 'loading') {
|
|
62
|
+
return <div className="text-muted-foreground text-sm">Loading…</div>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (stage.kind === 'signed_out') {
|
|
66
|
+
return (
|
|
67
|
+
<div className="rounded-md border p-4">
|
|
68
|
+
<p className="text-sm">
|
|
69
|
+
<a href="/account/login" className="font-medium underline">
|
|
70
|
+
Sign in
|
|
71
|
+
</a>{' '}
|
|
72
|
+
to leave a review for this product.
|
|
73
|
+
</p>
|
|
74
|
+
</div>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (stage.kind === 'not_eligible') {
|
|
79
|
+
return <NotEligibleMessage reason={stage.reason} />;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (stage.kind === 'edit') {
|
|
83
|
+
return (
|
|
84
|
+
<EditReviewBlock
|
|
85
|
+
productId={productId}
|
|
86
|
+
initial={stage.review}
|
|
87
|
+
onDeleted={() => setStage({ kind: 'submit' })}
|
|
88
|
+
/>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return <SubmitReviewBlock productId={productId} />;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function NotEligibleMessage({ reason }: { reason: MyProductReview['reason'] }) {
|
|
96
|
+
let message: string;
|
|
97
|
+
switch (reason) {
|
|
98
|
+
case 'no_eligible_order':
|
|
99
|
+
message = 'Only customers who purchased this product can leave a review.';
|
|
100
|
+
break;
|
|
101
|
+
case 'reviews_disabled':
|
|
102
|
+
message = 'Reviews are not enabled for this store.';
|
|
103
|
+
break;
|
|
104
|
+
default:
|
|
105
|
+
message = 'You cannot review this product right now.';
|
|
106
|
+
}
|
|
107
|
+
return (
|
|
108
|
+
<div className="bg-muted/30 rounded-md border p-4">
|
|
109
|
+
<p className="text-muted-foreground text-sm">{message}</p>
|
|
110
|
+
</div>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function SubmitReviewBlock({ productId }: { productId: string }) {
|
|
115
|
+
return <ReviewEditor productId={productId} mode="create" initialRating={5} initialBody="" />;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function EditReviewBlock({
|
|
119
|
+
productId,
|
|
120
|
+
initial,
|
|
121
|
+
onDeleted,
|
|
122
|
+
}: {
|
|
123
|
+
productId: string;
|
|
124
|
+
initial: ProductReview;
|
|
125
|
+
onDeleted: () => void;
|
|
126
|
+
}) {
|
|
127
|
+
const [deleting, setDeleting] = useState(false);
|
|
128
|
+
const [deleteError, setDeleteError] = useState<string | null>(null);
|
|
129
|
+
|
|
130
|
+
async function handleDelete() {
|
|
131
|
+
if (!window.confirm('Delete your review for this product?')) return;
|
|
132
|
+
setDeleting(true);
|
|
133
|
+
setDeleteError(null);
|
|
134
|
+
try {
|
|
135
|
+
await getClient().deleteMyProductReview(productId);
|
|
136
|
+
onDeleted();
|
|
137
|
+
window.location.reload();
|
|
138
|
+
} catch {
|
|
139
|
+
setDeleteError('Could not delete your review. Please try again.');
|
|
140
|
+
} finally {
|
|
141
|
+
setDeleting(false);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return (
|
|
146
|
+
<div className="space-y-3">
|
|
147
|
+
<ReviewEditor
|
|
148
|
+
productId={productId}
|
|
149
|
+
mode="edit"
|
|
150
|
+
initialRating={initial.rating}
|
|
151
|
+
initialBody={initial.body ?? ''}
|
|
152
|
+
/>
|
|
153
|
+
<div className="flex items-center justify-between rounded-md border border-red-100 bg-red-50/50 p-3">
|
|
154
|
+
<p className="text-xs text-red-700">Want to start over? You can delete your review.</p>
|
|
155
|
+
<button
|
|
156
|
+
type="button"
|
|
157
|
+
onClick={handleDelete}
|
|
158
|
+
disabled={deleting}
|
|
159
|
+
className="text-xs font-medium text-red-700 underline disabled:opacity-50"
|
|
160
|
+
>
|
|
161
|
+
{deleting ? 'Deleting…' : 'Delete review'}
|
|
162
|
+
</button>
|
|
163
|
+
</div>
|
|
164
|
+
{deleteError && (
|
|
165
|
+
<p role="alert" className="text-sm text-red-700">
|
|
166
|
+
{deleteError}
|
|
167
|
+
</p>
|
|
168
|
+
)}
|
|
169
|
+
</div>
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The actual form. Same shape for create and edit — only the API method
|
|
175
|
+
* and the heading differ. Optimistic UX: success state + page reload to
|
|
176
|
+
* let the server-rendered reviews list pick up the change.
|
|
177
|
+
*/
|
|
178
|
+
function ReviewEditor({
|
|
179
|
+
productId,
|
|
180
|
+
mode,
|
|
181
|
+
initialRating,
|
|
182
|
+
initialBody,
|
|
183
|
+
}: {
|
|
184
|
+
productId: string;
|
|
185
|
+
mode: 'create' | 'edit';
|
|
186
|
+
initialRating: number;
|
|
187
|
+
initialBody: string;
|
|
188
|
+
}) {
|
|
189
|
+
const [rating, setRating] = useState(initialRating);
|
|
190
|
+
const [body, setBody] = useState(initialBody);
|
|
15
191
|
const [submitting, setSubmitting] = useState(false);
|
|
16
192
|
const [error, setError] = useState<string | null>(null);
|
|
17
193
|
const [success, setSuccess] = useState(false);
|
|
@@ -21,29 +197,29 @@ export function ReviewForm({ productId }: ReviewFormProps) {
|
|
|
21
197
|
setError(null);
|
|
22
198
|
setSubmitting(true);
|
|
23
199
|
try {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
200
|
+
const client = getClient();
|
|
201
|
+
const input = { rating, body: body.trim() || undefined };
|
|
202
|
+
if (mode === 'edit') {
|
|
203
|
+
await client.updateMyProductReview(productId, input);
|
|
204
|
+
} else {
|
|
205
|
+
await client.submitProductReview(productId, input);
|
|
206
|
+
}
|
|
30
207
|
setSuccess(true);
|
|
31
|
-
setAuthorName('');
|
|
32
|
-
setAuthorEmail('');
|
|
33
|
-
setBody('');
|
|
34
|
-
setRating(5);
|
|
35
|
-
// The reviews list is server-rendered — refresh to pick up the new review.
|
|
36
208
|
setTimeout(() => window.location.reload(), 1200);
|
|
37
209
|
} catch (err) {
|
|
38
210
|
const status = (err as { status?: number })?.status;
|
|
39
211
|
if (status === 409) {
|
|
40
212
|
setError("You've already submitted a review for this product.");
|
|
213
|
+
} else if (status === 403) {
|
|
214
|
+
setError('Only customers who purchased this product can leave a review.');
|
|
41
215
|
} else if (status === 429) {
|
|
42
216
|
setError('Too many submissions. Please try again in a few minutes.');
|
|
43
|
-
} else if (status === 403) {
|
|
44
|
-
setError('Reviews are not accepted for this product right now.');
|
|
45
217
|
} else {
|
|
46
|
-
setError(
|
|
218
|
+
setError(
|
|
219
|
+
mode === 'edit'
|
|
220
|
+
? 'Could not update your review. Please try again.'
|
|
221
|
+
: 'Could not submit your review. Please try again.'
|
|
222
|
+
);
|
|
47
223
|
}
|
|
48
224
|
} finally {
|
|
49
225
|
setSubmitting(false);
|
|
@@ -52,18 +228,20 @@ export function ReviewForm({ productId }: ReviewFormProps) {
|
|
|
52
228
|
|
|
53
229
|
if (success) {
|
|
54
230
|
return (
|
|
55
|
-
<p className="
|
|
56
|
-
|
|
231
|
+
<p className="rounded-md border border-emerald-200 bg-emerald-50 p-3 text-sm text-emerald-700">
|
|
232
|
+
{mode === 'edit'
|
|
233
|
+
? 'Thanks! Your review has been updated.'
|
|
234
|
+
: 'Thanks! Your review has been submitted.'}
|
|
57
235
|
</p>
|
|
58
236
|
);
|
|
59
237
|
}
|
|
60
238
|
|
|
61
239
|
return (
|
|
62
|
-
<form onSubmit={handleSubmit} className="
|
|
63
|
-
<h3 className="font-medium">Write a review</h3>
|
|
240
|
+
<form onSubmit={handleSubmit} className="space-y-3 rounded-md border p-4">
|
|
241
|
+
<h3 className="font-medium">{mode === 'edit' ? 'Edit your review' : 'Write a review'}</h3>
|
|
64
242
|
|
|
65
243
|
<fieldset>
|
|
66
|
-
<legend className="text-sm
|
|
244
|
+
<legend className="mb-1 text-sm">Your rating</legend>
|
|
67
245
|
<div className="inline-flex gap-1" role="radiogroup" aria-label="Star rating">
|
|
68
246
|
{[1, 2, 3, 4, 5].map((n) => (
|
|
69
247
|
<button
|
|
@@ -80,30 +258,6 @@ export function ReviewForm({ productId }: ReviewFormProps) {
|
|
|
80
258
|
</div>
|
|
81
259
|
</fieldset>
|
|
82
260
|
|
|
83
|
-
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
|
84
|
-
<label className="block text-sm">
|
|
85
|
-
Name
|
|
86
|
-
<input
|
|
87
|
-
type="text"
|
|
88
|
-
value={authorName}
|
|
89
|
-
onChange={(event) => setAuthorName(event.target.value)}
|
|
90
|
-
required
|
|
91
|
-
maxLength={100}
|
|
92
|
-
className="mt-1 block w-full rounded border px-2 py-1.5 text-sm"
|
|
93
|
-
/>
|
|
94
|
-
</label>
|
|
95
|
-
<label className="block text-sm">
|
|
96
|
-
Email <span className="text-muted-foreground text-xs">(optional)</span>
|
|
97
|
-
<input
|
|
98
|
-
type="email"
|
|
99
|
-
value={authorEmail}
|
|
100
|
-
onChange={(event) => setAuthorEmail(event.target.value)}
|
|
101
|
-
maxLength={200}
|
|
102
|
-
className="mt-1 block w-full rounded border px-2 py-1.5 text-sm"
|
|
103
|
-
/>
|
|
104
|
-
</label>
|
|
105
|
-
</div>
|
|
106
|
-
|
|
107
261
|
<label className="block text-sm">
|
|
108
262
|
Your review <span className="text-muted-foreground text-xs">(optional)</span>
|
|
109
263
|
<textarea
|
|
@@ -123,10 +277,16 @@ export function ReviewForm({ productId }: ReviewFormProps) {
|
|
|
123
277
|
|
|
124
278
|
<button
|
|
125
279
|
type="submit"
|
|
126
|
-
disabled={submitting
|
|
280
|
+
disabled={submitting}
|
|
127
281
|
className="inline-flex items-center rounded bg-black px-4 py-2 text-sm text-white disabled:opacity-50"
|
|
128
282
|
>
|
|
129
|
-
{submitting
|
|
283
|
+
{submitting
|
|
284
|
+
? mode === 'edit'
|
|
285
|
+
? 'Saving…'
|
|
286
|
+
: 'Submitting…'
|
|
287
|
+
: mode === 'edit'
|
|
288
|
+
? 'Save changes'
|
|
289
|
+
: 'Submit review'}
|
|
130
290
|
</button>
|
|
131
291
|
</form>
|
|
132
292
|
);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ProductReview } from 'brainerce';
|
|
2
|
-
import {
|
|
2
|
+
import { getServerClient } from '@/lib/brainerce';
|
|
3
3
|
import { ReviewForm } from './review-form';
|
|
4
4
|
|
|
5
5
|
interface ReviewsSectionProps {
|
|
@@ -21,7 +21,7 @@ export async function ReviewsSection({ productId, initialReviews }: ReviewsSecti
|
|
|
21
21
|
|
|
22
22
|
if (!initialReviews) {
|
|
23
23
|
try {
|
|
24
|
-
const result = await
|
|
24
|
+
const result = await getServerClient().listProductReviews(productId, { page: 1, limit: 20 });
|
|
25
25
|
reviews = result.data;
|
|
26
26
|
} catch {
|
|
27
27
|
// Reviews disabled at store level or network error — render the form anyway.
|
|
@@ -31,16 +31,16 @@ export async function ReviewsSection({ productId, initialReviews }: ReviewsSecti
|
|
|
31
31
|
|
|
32
32
|
return (
|
|
33
33
|
<section className="mt-8" aria-labelledby="reviews-heading">
|
|
34
|
-
<h2 id="reviews-heading" className="text-xl font-semibold
|
|
34
|
+
<h2 id="reviews-heading" className="mb-4 text-xl font-semibold">
|
|
35
35
|
Reviews
|
|
36
36
|
</h2>
|
|
37
37
|
|
|
38
38
|
{reviews.length === 0 ? (
|
|
39
|
-
<p className="text-
|
|
39
|
+
<p className="text-muted-foreground mb-6 text-sm">
|
|
40
40
|
No reviews yet — be the first to share your experience.
|
|
41
41
|
</p>
|
|
42
42
|
) : (
|
|
43
|
-
<ul className="space-y-4
|
|
43
|
+
<ul className="mb-6 space-y-4">
|
|
44
44
|
{reviews.map((review) => (
|
|
45
45
|
<ReviewCard key={review.id} review={review} />
|
|
46
46
|
))}
|
|
@@ -54,22 +54,20 @@ export async function ReviewsSection({ productId, initialReviews }: ReviewsSecti
|
|
|
54
54
|
|
|
55
55
|
function ReviewCard({ review }: { review: ProductReview }) {
|
|
56
56
|
return (
|
|
57
|
-
<li className="
|
|
58
|
-
<header className="flex items-center gap-2
|
|
57
|
+
<li className="rounded-md border p-4">
|
|
58
|
+
<header className="flex flex-wrap items-center gap-2">
|
|
59
59
|
<Stars rating={review.rating} />
|
|
60
|
-
<span className="font-medium
|
|
60
|
+
<span className="text-sm font-medium">{review.authorName}</span>
|
|
61
61
|
{review.verifiedPurchase && (
|
|
62
|
-
<span className="
|
|
62
|
+
<span className="rounded bg-emerald-50 px-2 py-0.5 text-xs text-emerald-700">
|
|
63
63
|
Verified purchase
|
|
64
64
|
</span>
|
|
65
65
|
)}
|
|
66
|
-
<time className="text-
|
|
66
|
+
<time className="text-muted-foreground ms-auto text-xs">
|
|
67
67
|
{new Date(review.createdAt).toLocaleDateString()}
|
|
68
68
|
</time>
|
|
69
69
|
</header>
|
|
70
|
-
{review.body &&
|
|
71
|
-
<p className="mt-2 text-sm whitespace-pre-line break-words">{review.body}</p>
|
|
72
|
-
)}
|
|
70
|
+
{review.body && <p className="mt-2 whitespace-pre-line break-words text-sm">{review.body}</p>}
|
|
73
71
|
</li>
|
|
74
72
|
);
|
|
75
73
|
}
|
|
@@ -1,116 +1,116 @@
|
|
|
1
|
-
import type { Product } from 'brainerce';
|
|
2
|
-
import { getProductPriceInfo } from 'brainerce';
|
|
3
|
-
import { getNonce } from '@/lib/nonce';
|
|
4
|
-
|
|
5
|
-
interface ProductJsonLdProps {
|
|
6
|
-
product: Product;
|
|
7
|
-
url: string;
|
|
8
|
-
currency?: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export async function ProductJsonLd({ product, url, currency = 'USD' }: ProductJsonLdProps) {
|
|
12
|
-
const nonce = await getNonce();
|
|
13
|
-
const priceInfo = getProductPriceInfo(product);
|
|
14
|
-
const imageUrl = product.images?.[0]?.url;
|
|
15
|
-
const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || '';
|
|
16
|
-
|
|
17
|
-
const availability =
|
|
18
|
-
product.inventory?.canPurchase !== false
|
|
19
|
-
? 'https://schema.org/InStock'
|
|
20
|
-
: 'https://schema.org/OutOfStock';
|
|
21
|
-
|
|
22
|
-
const isVariable = product.type === 'VARIABLE';
|
|
23
|
-
const offers =
|
|
24
|
-
isVariable && product.priceMin
|
|
25
|
-
? {
|
|
26
|
-
'@type': 'AggregateOffer',
|
|
27
|
-
lowPrice: product.priceMin,
|
|
28
|
-
highPrice: product.priceMax ?? product.priceMin,
|
|
29
|
-
offerCount: product.variants?.length ?? 1,
|
|
30
|
-
priceCurrency: currency,
|
|
31
|
-
availability,
|
|
32
|
-
url,
|
|
33
|
-
}
|
|
34
|
-
: {
|
|
35
|
-
'@type': 'Offer',
|
|
36
|
-
price: priceInfo.price,
|
|
37
|
-
priceCurrency: currency,
|
|
38
|
-
availability,
|
|
39
|
-
url,
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const productJsonLd: Record<string, unknown> = {
|
|
43
|
-
'@context': 'https://schema.org',
|
|
44
|
-
'@type': 'Product',
|
|
45
|
-
name: product.name,
|
|
46
|
-
description: product.description || product.name,
|
|
47
|
-
image: imageUrl,
|
|
48
|
-
url,
|
|
49
|
-
sku: product.sku || product.id,
|
|
50
|
-
offers,
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// Emit aggregateRating only when there is real review data — Google rejects
|
|
54
|
-
// self-serving / faked ratings and stores with 0 reviews shouldn't claim any.
|
|
55
|
-
if (product.reviewCount && product.reviewCount > 0) {
|
|
56
|
-
productJsonLd.aggregateRating = {
|
|
57
|
-
'@type': 'AggregateRating',
|
|
58
|
-
ratingValue: product.avgRating,
|
|
59
|
-
reviewCount: product.reviewCount,
|
|
60
|
-
bestRating: 5,
|
|
61
|
-
worstRating: 1,
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const breadcrumbJsonLd = {
|
|
66
|
-
'@context': 'https://schema.org',
|
|
67
|
-
'@type': 'BreadcrumbList',
|
|
68
|
-
itemListElement: [
|
|
69
|
-
{
|
|
70
|
-
'@type': 'ListItem',
|
|
71
|
-
position: 1,
|
|
72
|
-
name: 'Home',
|
|
73
|
-
item: baseUrl || '/',
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
'@type': 'ListItem',
|
|
77
|
-
position: 2,
|
|
78
|
-
name: 'Products',
|
|
79
|
-
item: `${baseUrl}/products`,
|
|
80
|
-
},
|
|
81
|
-
{
|
|
82
|
-
'@type': 'ListItem',
|
|
83
|
-
position: 3,
|
|
84
|
-
name: product.name,
|
|
85
|
-
item: url,
|
|
86
|
-
},
|
|
87
|
-
],
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
return (
|
|
91
|
-
<>
|
|
92
|
-
<script
|
|
93
|
-
type="application/ld+json"
|
|
94
|
-
nonce={nonce}
|
|
95
|
-
suppressHydrationWarning
|
|
96
|
-
dangerouslySetInnerHTML={{ __html: serializeJsonLd(productJsonLd) }}
|
|
97
|
-
/>
|
|
98
|
-
<script
|
|
99
|
-
type="application/ld+json"
|
|
100
|
-
nonce={nonce}
|
|
101
|
-
suppressHydrationWarning
|
|
102
|
-
dangerouslySetInnerHTML={{ __html: serializeJsonLd(breadcrumbJsonLd) }}
|
|
103
|
-
/>
|
|
104
|
-
</>
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// Serialize a JSON-LD object for embedding in a <script> tag. Escapes
|
|
109
|
-
// `<`, `>`, and `&` to \uXXXX so seller-controlled product fields cannot
|
|
110
|
-
// break out of the script element with `</script>` or inject HTML.
|
|
111
|
-
function serializeJsonLd(value: unknown): string {
|
|
112
|
-
return JSON.stringify(value)
|
|
113
|
-
.replace(/</g, '\\u003c')
|
|
114
|
-
.replace(/>/g, '\\u003e')
|
|
115
|
-
.replace(/&/g, '\\u0026');
|
|
116
|
-
}
|
|
1
|
+
import type { Product } from 'brainerce';
|
|
2
|
+
import { getProductPriceInfo } from 'brainerce';
|
|
3
|
+
import { getNonce } from '@/lib/nonce';
|
|
4
|
+
|
|
5
|
+
interface ProductJsonLdProps {
|
|
6
|
+
product: Product;
|
|
7
|
+
url: string;
|
|
8
|
+
currency?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function ProductJsonLd({ product, url, currency = 'USD' }: ProductJsonLdProps) {
|
|
12
|
+
const nonce = await getNonce();
|
|
13
|
+
const priceInfo = getProductPriceInfo(product);
|
|
14
|
+
const imageUrl = product.images?.[0]?.url;
|
|
15
|
+
const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || '';
|
|
16
|
+
|
|
17
|
+
const availability =
|
|
18
|
+
product.inventory?.canPurchase !== false
|
|
19
|
+
? 'https://schema.org/InStock'
|
|
20
|
+
: 'https://schema.org/OutOfStock';
|
|
21
|
+
|
|
22
|
+
const isVariable = product.type === 'VARIABLE';
|
|
23
|
+
const offers =
|
|
24
|
+
isVariable && product.priceMin
|
|
25
|
+
? {
|
|
26
|
+
'@type': 'AggregateOffer',
|
|
27
|
+
lowPrice: product.priceMin,
|
|
28
|
+
highPrice: product.priceMax ?? product.priceMin,
|
|
29
|
+
offerCount: product.variants?.length ?? 1,
|
|
30
|
+
priceCurrency: currency,
|
|
31
|
+
availability,
|
|
32
|
+
url,
|
|
33
|
+
}
|
|
34
|
+
: {
|
|
35
|
+
'@type': 'Offer',
|
|
36
|
+
price: priceInfo.price,
|
|
37
|
+
priceCurrency: currency,
|
|
38
|
+
availability,
|
|
39
|
+
url,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const productJsonLd: Record<string, unknown> = {
|
|
43
|
+
'@context': 'https://schema.org',
|
|
44
|
+
'@type': 'Product',
|
|
45
|
+
name: product.name,
|
|
46
|
+
description: product.description || product.name,
|
|
47
|
+
image: imageUrl,
|
|
48
|
+
url,
|
|
49
|
+
sku: product.sku || product.id,
|
|
50
|
+
offers,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Emit aggregateRating only when there is real review data — Google rejects
|
|
54
|
+
// self-serving / faked ratings and stores with 0 reviews shouldn't claim any.
|
|
55
|
+
if (product.reviewCount && product.reviewCount > 0) {
|
|
56
|
+
productJsonLd.aggregateRating = {
|
|
57
|
+
'@type': 'AggregateRating',
|
|
58
|
+
ratingValue: product.avgRating,
|
|
59
|
+
reviewCount: product.reviewCount,
|
|
60
|
+
bestRating: 5,
|
|
61
|
+
worstRating: 1,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const breadcrumbJsonLd = {
|
|
66
|
+
'@context': 'https://schema.org',
|
|
67
|
+
'@type': 'BreadcrumbList',
|
|
68
|
+
itemListElement: [
|
|
69
|
+
{
|
|
70
|
+
'@type': 'ListItem',
|
|
71
|
+
position: 1,
|
|
72
|
+
name: 'Home',
|
|
73
|
+
item: baseUrl || '/',
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
'@type': 'ListItem',
|
|
77
|
+
position: 2,
|
|
78
|
+
name: 'Products',
|
|
79
|
+
item: `${baseUrl}/products`,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
'@type': 'ListItem',
|
|
83
|
+
position: 3,
|
|
84
|
+
name: product.name,
|
|
85
|
+
item: url,
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<>
|
|
92
|
+
<script
|
|
93
|
+
type="application/ld+json"
|
|
94
|
+
nonce={nonce}
|
|
95
|
+
suppressHydrationWarning
|
|
96
|
+
dangerouslySetInnerHTML={{ __html: serializeJsonLd(productJsonLd) }}
|
|
97
|
+
/>
|
|
98
|
+
<script
|
|
99
|
+
type="application/ld+json"
|
|
100
|
+
nonce={nonce}
|
|
101
|
+
suppressHydrationWarning
|
|
102
|
+
dangerouslySetInnerHTML={{ __html: serializeJsonLd(breadcrumbJsonLd) }}
|
|
103
|
+
/>
|
|
104
|
+
</>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Serialize a JSON-LD object for embedding in a <script> tag. Escapes
|
|
109
|
+
// `<`, `>`, and `&` to \uXXXX so seller-controlled product fields cannot
|
|
110
|
+
// break out of the script element with `</script>` or inject HTML.
|
|
111
|
+
function serializeJsonLd(value: unknown): string {
|
|
112
|
+
return JSON.stringify(value)
|
|
113
|
+
.replace(/</g, '\\u003c')
|
|
114
|
+
.replace(/>/g, '\\u003e')
|
|
115
|
+
.replace(/&/g, '\\u0026');
|
|
116
|
+
}
|