create-brainerce-store 1.51.0 → 1.52.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.
Files changed (170) hide show
  1. package/README.md +72 -0
  2. package/dist/index.js +9 -68
  3. package/messages/en.json +32 -0
  4. package/messages/he.json +32 -0
  5. package/package.json +1 -1
  6. package/templates/nextjs/base/.claude/commands/design.md +9 -0
  7. package/templates/nextjs/base/AI-GUIDE.md +52 -3
  8. package/templates/nextjs/base/CLAUDE.md +10 -0
  9. package/templates/nextjs/base/package.json.ejs +52 -39
  10. package/templates/nextjs/base/src/app/globals.css +36 -30
  11. package/templates/nextjs/base/src/components/checkout/payment-step.tsx +14 -10
  12. package/templates/nextjs/base/src/components/ui/accordion.tsx +53 -0
  13. package/templates/nextjs/base/src/components/ui/badge.tsx +32 -0
  14. package/templates/nextjs/base/src/components/ui/button.tsx +49 -0
  15. package/templates/nextjs/base/src/components/ui/card.tsx +55 -0
  16. package/templates/nextjs/base/src/components/ui/checkbox.tsx +28 -0
  17. package/templates/nextjs/base/src/components/ui/dialog.tsx +107 -0
  18. package/templates/nextjs/base/src/components/ui/input.tsx +22 -0
  19. package/templates/nextjs/base/src/components/ui/label.tsx +21 -0
  20. package/templates/nextjs/base/src/components/ui/radio-group.tsx +38 -0
  21. package/templates/nextjs/base/src/components/ui/select.tsx +153 -0
  22. package/templates/nextjs/base/src/components/ui/separator.tsx +26 -0
  23. package/templates/nextjs/base/src/components/ui/sheet.tsx +125 -0
  24. package/templates/nextjs/base/src/components/ui/skeleton.tsx +7 -0
  25. package/templates/nextjs/base/src/components/ui/tabs.tsx +55 -0
  26. package/templates/nextjs/base/src/components/ui/textarea.tsx +21 -0
  27. package/templates/nextjs/base/src/components/ui/tooltip.tsx +32 -0
  28. package/templates/nextjs/base/src/ui/cart/cart-bundle-offer.tsx +17 -11
  29. package/templates/nextjs/base/src/ui/cart/cart-item.tsx +2 -8
  30. package/templates/nextjs/base/src/ui/cart/cart-nudges.tsx +28 -39
  31. package/templates/nextjs/base/src/ui/cart/cart-summary.tsx +3 -1
  32. package/templates/nextjs/base/src/ui/cart/cart-upgrade-banner.tsx +8 -23
  33. package/templates/nextjs/base/src/ui/cart/cart-view.tsx +18 -27
  34. package/templates/nextjs/base/src/ui/cart/coupon-input.tsx +10 -21
  35. package/templates/nextjs/base/src/ui/cart/free-shipping-bar.tsx +2 -3
  36. package/templates/nextjs/base/src/ui/cart/reservation-countdown.tsx +95 -103
  37. package/templates/nextjs/base/src/ui/home/hero-section.tsx +4 -6
  38. package/templates/nextjs/base/src/ui/layout/announcement-bar.tsx.ejs +116 -125
  39. package/templates/nextjs/base/src/ui/layout/faq-section.tsx.ejs +90 -103
  40. package/templates/nextjs/base/src/ui/layout/header-account.tsx +2 -17
  41. package/templates/nextjs/base/src/ui/layout/language-switcher.tsx.ejs +80 -70
  42. package/templates/nextjs/base/src/ui/layout/site-footer.tsx.ejs +144 -164
  43. package/templates/nextjs/base/src/ui/layout/site-header.tsx.ejs +152 -182
  44. package/templates/nextjs/base/src/ui/product/customization-fields.tsx +456 -444
  45. package/templates/nextjs/base/src/ui/product/discount-badge.tsx +18 -22
  46. package/templates/nextjs/base/src/ui/product/frequently-bought-together.tsx +15 -27
  47. package/templates/nextjs/base/src/ui/product/modifier-group-selector.tsx +206 -175
  48. package/templates/nextjs/base/src/ui/product/price-display.tsx +3 -2
  49. package/templates/nextjs/base/src/ui/product/product-card.tsx +238 -271
  50. package/templates/nextjs/base/src/ui/product/product-client-section.tsx +455 -470
  51. package/templates/nextjs/base/src/ui/product/product-listing.tsx +96 -79
  52. package/templates/nextjs/base/src/ui/product/recommendation-section.tsx +102 -107
  53. package/templates/nextjs/base/src/ui/product/review-form.tsx +44 -43
  54. package/templates/nextjs/{designs/passepartout → base/src}/ui/product/reviews-section.tsx.ejs +108 -96
  55. package/templates/nextjs/base/src/ui/product/stock-badge.tsx +61 -63
  56. package/templates/nextjs/base/tailwind.config.ts +69 -42
  57. package/templates/nextjs/designs/{passepartout → atelier}/app-overlay/layout.tsx.ejs +23 -25
  58. package/templates/nextjs/designs/atelier/globals.css +396 -0
  59. package/templates/nextjs/designs/atelier/messages-patch/en.json +99 -0
  60. package/templates/nextjs/designs/atelier/messages-patch/he.json +99 -0
  61. package/templates/nextjs/designs/{passepartout → atelier}/ui/cart/cart-bundle-offer.tsx +110 -111
  62. package/templates/nextjs/designs/atelier/ui/cart/cart-drawer.tsx +163 -0
  63. package/templates/nextjs/designs/atelier/ui/cart/cart-item.tsx +140 -0
  64. package/templates/nextjs/designs/atelier/ui/cart/cart-nudges.tsx +34 -0
  65. package/templates/nextjs/designs/atelier/ui/cart/cart-summary.tsx +107 -0
  66. package/templates/nextjs/designs/{passepartout → atelier}/ui/cart/cart-upgrade-banner.tsx +123 -141
  67. package/templates/nextjs/designs/atelier/ui/cart/cart-view.tsx +147 -0
  68. package/templates/nextjs/designs/atelier/ui/cart/coupon-input.tsx +142 -0
  69. package/templates/nextjs/designs/{passepartout → atelier}/ui/cart/free-shipping-bar.tsx +77 -62
  70. package/templates/nextjs/designs/{passepartout → atelier}/ui/cart/reservation-countdown.tsx +89 -103
  71. package/templates/nextjs/designs/atelier/ui/home/benefits-band.tsx +34 -0
  72. package/templates/nextjs/designs/atelier/ui/home/category-tiles.tsx +148 -0
  73. package/templates/nextjs/designs/atelier/ui/home/discount-banner-strip.tsx +27 -0
  74. package/templates/nextjs/designs/atelier/ui/home/editorial-band.tsx +73 -0
  75. package/templates/nextjs/designs/atelier/ui/home/featured-products-section.tsx +36 -0
  76. package/templates/nextjs/designs/atelier/ui/home/hero-section.tsx +148 -0
  77. package/templates/nextjs/designs/atelier/ui/home/home-client.tsx +54 -0
  78. package/templates/nextjs/designs/atelier/ui/home/newsletter-section.tsx +78 -0
  79. package/templates/nextjs/designs/atelier/ui/home/testimonials-band.tsx +46 -0
  80. package/templates/nextjs/designs/atelier/ui/layout/announcement-bar.tsx +118 -0
  81. package/templates/nextjs/designs/atelier/ui/layout/faq-section.tsx +96 -0
  82. package/templates/nextjs/designs/atelier/ui/layout/header-account.tsx +36 -0
  83. package/templates/nextjs/designs/atelier/ui/layout/header-cart.tsx +36 -0
  84. package/templates/nextjs/designs/atelier/ui/layout/language-switcher.tsx.ejs +137 -0
  85. package/templates/nextjs/designs/atelier/ui/layout/nav-url.ts +14 -0
  86. package/templates/nextjs/designs/atelier/ui/layout/rich-text-block.tsx +34 -0
  87. package/templates/nextjs/designs/atelier/ui/layout/site-footer.tsx.ejs +141 -0
  88. package/templates/nextjs/designs/atelier/ui/layout/site-header.tsx.ejs +138 -0
  89. package/templates/nextjs/designs/atelier/ui/product/customization-fields.tsx +466 -0
  90. package/templates/nextjs/designs/{passepartout → atelier}/ui/product/discount-badge.tsx +17 -22
  91. package/templates/nextjs/designs/{passepartout → atelier}/ui/product/frequently-bought-together.tsx +196 -205
  92. package/templates/nextjs/designs/atelier/ui/product/modifier-group-selector.tsx +193 -0
  93. package/templates/nextjs/designs/atelier/ui/product/price-display.tsx +69 -0
  94. package/templates/nextjs/designs/atelier/ui/product/product-card.tsx +260 -0
  95. package/templates/nextjs/designs/atelier/ui/product/product-client-section.tsx +471 -0
  96. package/templates/nextjs/designs/atelier/ui/product/product-grid.tsx +47 -0
  97. package/templates/nextjs/designs/atelier/ui/product/product-listing.tsx +260 -0
  98. package/templates/nextjs/designs/atelier/ui/product/recommendation-section.tsx +95 -0
  99. package/templates/nextjs/designs/{passepartout → atelier}/ui/product/review-form.tsx +267 -294
  100. package/templates/nextjs/designs/atelier/ui/product/reviews-section.tsx.ejs +148 -0
  101. package/templates/nextjs/designs/atelier/ui/product/stock-badge.tsx +66 -0
  102. package/templates/nextjs/designs/atelier/ui/product/variant-selector.tsx +206 -0
  103. package/templates/nextjs/designs/atelier/ui/shared/cdn-image.tsx +29 -0
  104. package/templates/nextjs/designs/{passepartout → atelier}/ui/shared/fly-to-cart.ts +86 -85
  105. package/templates/nextjs/designs/atelier/ui/shared/icons.tsx +269 -0
  106. package/templates/nextjs/designs/atelier/ui/shared/loading-spinner.tsx +64 -0
  107. package/templates/nextjs/designs/atelier/ui/shared/reveal.tsx +71 -0
  108. package/templates/nextjs/designs/atelier/ui/shared/select-menu.tsx +125 -0
  109. package/templates/nextjs/themes/luxury/globals.css +405 -399
  110. package/templates/nextjs/themes/minimal/globals.css +36 -30
  111. package/templates/nextjs/themes/playful/globals.css +406 -400
  112. package/templates/nextjs/ui-canvas/cart/cart-bundle-offer.tsx +2 -0
  113. package/templates/nextjs/ui-canvas/cart/cart-item.tsx +2 -0
  114. package/templates/nextjs/ui-canvas/cart/cart-nudges.tsx +2 -0
  115. package/templates/nextjs/ui-canvas/cart/cart-summary.tsx +2 -0
  116. package/templates/nextjs/ui-canvas/cart/cart-upgrade-banner.tsx +2 -0
  117. package/templates/nextjs/ui-canvas/cart/cart-view.tsx +2 -2
  118. package/templates/nextjs/ui-canvas/cart/coupon-input.tsx +2 -0
  119. package/templates/nextjs/ui-canvas/cart/free-shipping-bar.tsx +2 -0
  120. package/templates/nextjs/ui-canvas/cart/reservation-countdown.tsx +2 -0
  121. package/templates/nextjs/ui-canvas/home/discount-banner-strip.tsx +1 -1
  122. package/templates/nextjs/ui-canvas/home/featured-products-section.tsx +1 -1
  123. package/templates/nextjs/ui-canvas/home/hero-section.tsx +1 -1
  124. package/templates/nextjs/ui-canvas/home/home-client.tsx +1 -1
  125. package/templates/nextjs/ui-canvas/layout/announcement-bar.tsx.ejs +1 -1
  126. package/templates/nextjs/ui-canvas/layout/faq-section.tsx.ejs +2 -2
  127. package/templates/nextjs/ui-canvas/layout/header-account.tsx +1 -1
  128. package/templates/nextjs/ui-canvas/layout/language-switcher.tsx.ejs +2 -0
  129. package/templates/nextjs/ui-canvas/layout/rich-text-block.tsx.ejs +2 -0
  130. package/templates/nextjs/ui-canvas/layout/site-footer.tsx.ejs +2 -2
  131. package/templates/nextjs/ui-canvas/layout/site-header.tsx.ejs +2 -2
  132. package/templates/nextjs/ui-canvas/product/customization-fields.tsx +2 -0
  133. package/templates/nextjs/ui-canvas/product/discount-badge.tsx +2 -0
  134. package/templates/nextjs/ui-canvas/product/frequently-bought-together.tsx +2 -0
  135. package/templates/nextjs/ui-canvas/product/modifier-group-selector.tsx +2 -0
  136. package/templates/nextjs/ui-canvas/product/price-display.tsx +2 -0
  137. package/templates/nextjs/ui-canvas/product/product-card.tsx +1 -1
  138. package/templates/nextjs/ui-canvas/product/product-client-section.tsx +1 -1
  139. package/templates/nextjs/ui-canvas/product/product-grid.tsx +1 -1
  140. package/templates/nextjs/ui-canvas/product/product-listing.tsx +1 -1
  141. package/templates/nextjs/ui-canvas/product/recommendation-section.tsx +4 -0
  142. package/templates/nextjs/ui-canvas/product/review-form.tsx +29 -32
  143. package/templates/nextjs/ui-canvas/product/{reviews-section.tsx → reviews-section.tsx.ejs} +26 -8
  144. package/templates/nextjs/ui-canvas/product/stock-badge.tsx +2 -0
  145. package/templates/nextjs/ui-canvas/product/variant-selector.tsx +2 -0
  146. package/templates/nextjs/ui-canvas/shared/cdn-image.tsx +2 -0
  147. package/templates/nextjs/ui-canvas/shared/loading-spinner.tsx +2 -0
  148. package/templates/nextjs/base/src/ui/product/reviews-section.tsx +0 -85
  149. package/templates/nextjs/designs/passepartout/globals.css +0 -412
  150. package/templates/nextjs/designs/passepartout/messages-patch/en.json +0 -87
  151. package/templates/nextjs/designs/passepartout/messages-patch/he.json +0 -87
  152. package/templates/nextjs/designs/passepartout/ui/cart/cart-nudges.tsx +0 -39
  153. package/templates/nextjs/designs/passepartout/ui/cart/cart-view.tsx +0 -149
  154. package/templates/nextjs/designs/passepartout/ui/home/home-client.tsx +0 -401
  155. package/templates/nextjs/designs/passepartout/ui/layout/announcement-bar.tsx +0 -139
  156. package/templates/nextjs/designs/passepartout/ui/layout/cart-link.tsx +0 -45
  157. package/templates/nextjs/designs/passepartout/ui/layout/faq-section.tsx +0 -103
  158. package/templates/nextjs/designs/passepartout/ui/layout/site-footer.tsx +0 -159
  159. package/templates/nextjs/designs/passepartout/ui/layout/site-header.tsx.ejs +0 -155
  160. package/templates/nextjs/designs/passepartout/ui/layout/trust-icons.tsx +0 -60
  161. package/templates/nextjs/designs/passepartout/ui/layout/whatsapp-button.tsx +0 -34
  162. package/templates/nextjs/designs/passepartout/ui/product/price-display.tsx +0 -111
  163. package/templates/nextjs/designs/passepartout/ui/product/product-card.tsx +0 -265
  164. package/templates/nextjs/designs/passepartout/ui/product/product-client-section.tsx +0 -667
  165. package/templates/nextjs/designs/passepartout/ui/product/product-grid.tsx +0 -35
  166. package/templates/nextjs/designs/passepartout/ui/product/product-listing.tsx +0 -427
  167. package/templates/nextjs/designs/passepartout/ui/product/recommendation-section.tsx +0 -113
  168. package/templates/nextjs/designs/passepartout/ui/product/stock-badge.tsx +0 -52
  169. package/templates/nextjs/designs/passepartout/ui/product/variant-selector.tsx +0 -307
  170. package/templates/nextjs/designs/passepartout/ui/shared/loading-spinner.tsx +0 -33
@@ -1,294 +1,267 @@
1
- 'use client';
2
-
3
- import { useEffect, useState } from 'react';
4
- import { getClient } from '@/core/lib/brainerce';
5
- import { checkAuthStatus } from '@/core/lib/auth';
6
- import { useTranslations } from '@/core/lib/translations';
7
- import type { MyProductReview, ProductReview } from 'brainerce';
8
-
9
- interface ReviewFormProps {
10
- productId: string;
11
- }
12
-
13
- type Stage =
14
- | { kind: 'loading' }
15
- | { kind: 'signed_out' }
16
- | { kind: 'not_eligible'; reason: MyProductReview['reason'] }
17
- | { kind: 'submit' }
18
- | { kind: 'edit'; review: ProductReview };
19
-
20
- /**
21
- * Single component that decides which UI to render for a customer on a PDP:
22
- * - Loading while we check auth state and eligibility
23
- * - Signed out -> sign-in CTA
24
- * - Signed in but not eligible -> "purchase this product to review"
25
- * - Eligible without review -> submit form
26
- * - Eligible with existing review -> edit/delete form (rating + body prefilled)
27
- */
28
- export function ReviewForm({ productId }: ReviewFormProps) {
29
- const [stage, setStage] = useState<Stage>({ kind: 'loading' });
30
- const t = useTranslations('reviews');
31
-
32
- useEffect(() => {
33
- let cancelled = false;
34
- async function load() {
35
- const auth = await checkAuthStatus();
36
- if (cancelled) return;
37
- if (!auth.isLoggedIn) {
38
- setStage({ kind: 'signed_out' });
39
- return;
40
- }
41
- try {
42
- const me = await getClient().getMyProductReview(productId);
43
- if (cancelled) return;
44
- if (!me.eligible) {
45
- setStage({ kind: 'not_eligible', reason: me.reason });
46
- return;
47
- }
48
- if (me.myReview) {
49
- setStage({ kind: 'edit', review: me.myReview });
50
- return;
51
- }
52
- setStage({ kind: 'submit' });
53
- } catch {
54
- if (!cancelled) setStage({ kind: 'not_eligible', reason: 'product_not_found' });
55
- }
56
- }
57
- void load();
58
- return () => {
59
- cancelled = true;
60
- };
61
- }, [productId]);
62
-
63
- if (stage.kind === 'loading') {
64
- return <div className="text-muted-foreground text-sm">{t('loading')}</div>;
65
- }
66
-
67
- if (stage.kind === 'signed_out') {
68
- return (
69
- <div className="border p-4">
70
- <p className="text-sm">
71
- <a href="/account/login" className="font-medium underline">
72
- {t('signIn')}
73
- </a>{' '}
74
- {t('signInSuffix')}
75
- </p>
76
- </div>
77
- );
78
- }
79
-
80
- if (stage.kind === 'not_eligible') {
81
- return <NotEligibleMessage reason={stage.reason} />;
82
- }
83
-
84
- if (stage.kind === 'edit') {
85
- return (
86
- <EditReviewBlock
87
- productId={productId}
88
- initial={stage.review}
89
- onDeleted={() => setStage({ kind: 'submit' })}
90
- />
91
- );
92
- }
93
-
94
- return <SubmitReviewBlock productId={productId} />;
95
- }
96
-
97
- function NotEligibleMessage({ reason }: { reason: MyProductReview['reason'] }) {
98
- const t = useTranslations('reviews');
99
- let message: string;
100
- switch (reason) {
101
- case 'no_eligible_order':
102
- message = t('onlyPurchasers');
103
- break;
104
- case 'reviews_disabled':
105
- message = t('reviewsDisabled');
106
- break;
107
- default:
108
- message = t('cannotReview');
109
- }
110
- return (
111
- <div className="bg-muted/30 border p-4">
112
- <p className="text-muted-foreground text-sm">{message}</p>
113
- </div>
114
- );
115
- }
116
-
117
- function SubmitReviewBlock({ productId }: { productId: string }) {
118
- return <ReviewEditor productId={productId} mode="create" initialRating={5} initialBody="" />;
119
- }
120
-
121
- function EditReviewBlock({
122
- productId,
123
- initial,
124
- onDeleted,
125
- }: {
126
- productId: string;
127
- initial: ProductReview;
128
- onDeleted: () => void;
129
- }) {
130
- const [deleting, setDeleting] = useState(false);
131
- const t = useTranslations('reviews');
132
- const [deleteError, setDeleteError] = useState<string | null>(null);
133
-
134
- async function handleDelete() {
135
- if (!window.confirm(t('confirmDelete'))) return;
136
- setDeleting(true);
137
- setDeleteError(null);
138
- try {
139
- await getClient().deleteMyProductReview(productId);
140
- onDeleted();
141
- window.location.reload();
142
- } catch {
143
- setDeleteError(t('deleteFailed'));
144
- } finally {
145
- setDeleting(false);
146
- }
147
- }
148
-
149
- return (
150
- <div className="space-y-3">
151
- <ReviewEditor
152
- productId={productId}
153
- mode="edit"
154
- initialRating={initial.rating}
155
- initialBody={initial.body ?? ''}
156
- />
157
- <div className="border-border bg-muted/40 flex items-center justify-between border p-3">
158
- <p className="text-destructive text-xs">{t('startOver')}</p>
159
- <button
160
- type="button"
161
- onClick={handleDelete}
162
- disabled={deleting}
163
- className="text-destructive text-xs font-medium underline disabled:opacity-50"
164
- >
165
- {deleting ? t('deleting') : t('deleteReview')}
166
- </button>
167
- </div>
168
- {deleteError && (
169
- <p role="alert" className="text-sm text-red-700">
170
- {deleteError}
171
- </p>
172
- )}
173
- </div>
174
- );
175
- }
176
-
177
- /**
178
- * The actual form. Same shape for create and edit — only the API method
179
- * and the heading differ. Optimistic UX: success state + page reload to
180
- * let the server-rendered reviews list pick up the change.
181
- */
182
- function ReviewEditor({
183
- productId,
184
- mode,
185
- initialRating,
186
- initialBody,
187
- }: {
188
- productId: string;
189
- mode: 'create' | 'edit';
190
- initialRating: number;
191
- initialBody: string;
192
- }) {
193
- const [rating, setRating] = useState(initialRating);
194
- const t = useTranslations('reviews');
195
- const [body, setBody] = useState(initialBody);
196
- const [submitting, setSubmitting] = useState(false);
197
- const [error, setError] = useState<string | null>(null);
198
- const [success, setSuccess] = useState(false);
199
-
200
- async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
201
- event.preventDefault();
202
- setError(null);
203
- setSubmitting(true);
204
- try {
205
- const client = getClient();
206
- const input = { rating, body: body.trim() || undefined };
207
- if (mode === 'edit') {
208
- await client.updateMyProductReview(productId, input);
209
- } else {
210
- await client.submitProductReview(productId, input);
211
- }
212
- setSuccess(true);
213
- setTimeout(() => window.location.reload(), 1200);
214
- } catch (err) {
215
- const status = (err as { status?: number })?.status;
216
- if (status === 409) {
217
- setError(t('alreadySubmitted'));
218
- } else if (status === 403) {
219
- setError(t('onlyPurchasers'));
220
- } else if (status === 429) {
221
- setError(t('tooMany'));
222
- } else {
223
- setError(
224
- mode === 'edit' ? t('updateFailed') : t('submitFailed')
225
- );
226
- }
227
- } finally {
228
- setSubmitting(false);
229
- }
230
- }
231
-
232
- if (success) {
233
- return (
234
- <p className="border-border bg-surface text-foreground border p-3 text-sm">
235
- {mode === 'edit' ? t('thanksUpdated') : t('thanksSubmitted')}
236
- </p>
237
- );
238
- }
239
-
240
- return (
241
- <form onSubmit={handleSubmit} className="space-y-3 border p-4">
242
- <h3 className="font-medium">{mode === 'edit' ? t('editYourReview') : t('writeReview')}</h3>
243
-
244
- <fieldset>
245
- <legend className="mb-1 text-sm">{t('yourRating')}</legend>
246
- <div className="inline-flex gap-1" role="radiogroup" aria-label={t('starRating')}>
247
- {[1, 2, 3, 4, 5].map((n) => (
248
- <button
249
- key={n}
250
- type="button"
251
- role="radio"
252
- aria-checked={rating === n}
253
- onClick={() => setRating(n)}
254
- className={`text-2xl leading-none ${n <= rating ? 'text-accent' : 'text-border'}`}
255
- >
256
- ★
257
- </button>
258
- ))}
259
- </div>
260
- </fieldset>
261
-
262
- <label className="block text-sm">
263
- {t('yourReview')} <span className="text-muted-foreground text-xs">({t('optionalField')})</span>
264
- <textarea
265
- value={body}
266
- onChange={(event) => setBody(event.target.value)}
267
- maxLength={5000}
268
- rows={4}
269
- className="mt-1 block w-full rounded border px-2 py-1.5 text-sm"
270
- />
271
- </label>
272
-
273
- {error && (
274
- <p role="alert" className="text-sm text-red-700">
275
- {error}
276
- </p>
277
- )}
278
-
279
- <button
280
- type="submit"
281
- disabled={submitting}
282
- className="bg-primary text-primary-foreground inline-flex items-center px-4 py-2 text-sm font-semibold disabled:opacity-50"
283
- >
284
- {submitting
285
- ? mode === 'edit'
286
- ? t('saving')
287
- : t('submittingBtn')
288
- : mode === 'edit'
289
- ? t('saveChanges')
290
- : t('submitReview')}
291
- </button>
292
- </form>
293
- );
294
- }
1
+ 'use client';
2
+
3
+ import { useEffect, useState } from 'react';
4
+ import { getClient } from '@/core/lib/brainerce';
5
+ import { checkAuthStatus } from '@/core/lib/auth';
6
+ import { useTranslations } from '@/core/lib/translations';
7
+ import type { MyProductReview, ProductReview } from 'brainerce';
8
+ import { IconStar } from '@/ui/shared/icons';
9
+
10
+ interface ReviewFormProps {
11
+ productId: string;
12
+ }
13
+
14
+ type Stage =
15
+ | { kind: 'loading' }
16
+ | { kind: 'signed_out' }
17
+ | { kind: 'not_eligible'; reason: MyProductReview['reason'] }
18
+ | { kind: 'submit' }
19
+ | { kind: 'edit'; review: ProductReview };
20
+
21
+ /**
22
+ * Single component that decides which UI to render for a customer on a PDP:
23
+ * - Loading while we check auth state and eligibility
24
+ * - Signed out -> sign-in CTA
25
+ * - Signed in but not eligible -> "purchase this product to review"
26
+ * - Eligible without review -> submit form
27
+ * - Eligible with existing review -> edit/delete form (rating + body prefilled)
28
+ *
29
+ * DESIGN ME — review submit/edit form; auth via checkAuthStatus(), eligibility
30
+ * + existing review via getClient().getMyProductReview(productId).
31
+ */
32
+ export function ReviewForm({ productId }: ReviewFormProps) {
33
+ const [stage, setStage] = useState<Stage>({ kind: 'loading' });
34
+ const t = useTranslations('reviews');
35
+
36
+ useEffect(() => {
37
+ let cancelled = false;
38
+ async function load() {
39
+ const auth = await checkAuthStatus();
40
+ if (cancelled) return;
41
+ if (!auth.isLoggedIn) {
42
+ setStage({ kind: 'signed_out' });
43
+ return;
44
+ }
45
+ try {
46
+ const me = await getClient().getMyProductReview(productId);
47
+ if (cancelled) return;
48
+ if (!me.eligible) {
49
+ setStage({ kind: 'not_eligible', reason: me.reason });
50
+ return;
51
+ }
52
+ if (me.myReview) {
53
+ setStage({ kind: 'edit', review: me.myReview });
54
+ return;
55
+ }
56
+ setStage({ kind: 'submit' });
57
+ } catch {
58
+ if (!cancelled) setStage({ kind: 'not_eligible', reason: 'product_not_found' });
59
+ }
60
+ }
61
+ void load();
62
+ return () => {
63
+ cancelled = true;
64
+ };
65
+ }, [productId]);
66
+
67
+ if (stage.kind === 'loading') {
68
+ return <p role="status" className="text-sm text-muted-foreground">{t('loading')}</p>;
69
+ }
70
+
71
+ if (stage.kind === 'signed_out') {
72
+ return (
73
+ <p className="text-sm text-muted-foreground">
74
+ <a href="/account/login" className="font-semibold text-primary underline underline-offset-2">{t('signIn')}</a> {t('signInSuffix')}
75
+ </p>
76
+ );
77
+ }
78
+
79
+ if (stage.kind === 'not_eligible') {
80
+ return <NotEligibleMessage reason={stage.reason} />;
81
+ }
82
+
83
+ if (stage.kind === 'edit') {
84
+ return (
85
+ <EditReviewBlock
86
+ productId={productId}
87
+ initial={stage.review}
88
+ onDeleted={() => setStage({ kind: 'submit' })}
89
+ />
90
+ );
91
+ }
92
+
93
+ return <SubmitReviewBlock productId={productId} />;
94
+ }
95
+
96
+ function NotEligibleMessage({ reason }: { reason: MyProductReview['reason'] }) {
97
+ const t = useTranslations('reviews');
98
+ let message: string;
99
+ switch (reason) {
100
+ case 'no_eligible_order':
101
+ message = t('onlyPurchasers');
102
+ break;
103
+ case 'reviews_disabled':
104
+ message = t('reviewsDisabled');
105
+ break;
106
+ default:
107
+ message = t('cannotReview');
108
+ }
109
+ return <p className="text-sm text-muted-foreground">{message}</p>;
110
+ }
111
+
112
+ function SubmitReviewBlock({ productId }: { productId: string }) {
113
+ return <ReviewEditor productId={productId} mode="create" initialRating={5} initialBody="" />;
114
+ }
115
+
116
+ function EditReviewBlock({
117
+ productId,
118
+ initial,
119
+ onDeleted,
120
+ }: {
121
+ productId: string;
122
+ initial: ProductReview;
123
+ onDeleted: () => void;
124
+ }) {
125
+ const [deleting, setDeleting] = useState(false);
126
+ const t = useTranslations('reviews');
127
+ const [deleteError, setDeleteError] = useState<string | null>(null);
128
+
129
+ async function handleDelete() {
130
+ if (!window.confirm(t('confirmDelete'))) return;
131
+ setDeleting(true);
132
+ setDeleteError(null);
133
+ try {
134
+ await getClient().deleteMyProductReview(productId);
135
+ onDeleted();
136
+ window.location.reload();
137
+ } catch {
138
+ setDeleteError(t('deleteFailed'));
139
+ } finally {
140
+ setDeleting(false);
141
+ }
142
+ }
143
+
144
+ return (
145
+ <div>
146
+ <ReviewEditor
147
+ productId={productId}
148
+ mode="edit"
149
+ initialRating={initial.rating}
150
+ initialBody={initial.body ?? ''}
151
+ />
152
+ <div className="mt-4 flex flex-wrap items-center gap-2 border-t pt-4">
153
+ <p className="text-xs text-muted-foreground">{t('startOver')}</p>
154
+ <button type="button" onClick={handleDelete} disabled={deleting} className="text-xs font-semibold text-destructive underline underline-offset-2 disabled:opacity-50">
155
+ {deleting ? t('deleting') : t('deleteReview')}
156
+ </button>
157
+ </div>
158
+ {deleteError && <p role="alert" className="mt-2 text-sm font-medium text-destructive">{deleteError}</p>}
159
+ </div>
160
+ );
161
+ }
162
+
163
+ /**
164
+ * The actual form. Same shape for create and edit — only the API method
165
+ * and the heading differ. Optimistic UX: success state + page reload to
166
+ * let the server-rendered reviews list pick up the change.
167
+ */
168
+ function ReviewEditor({
169
+ productId,
170
+ mode,
171
+ initialRating,
172
+ initialBody,
173
+ }: {
174
+ productId: string;
175
+ mode: 'create' | 'edit';
176
+ initialRating: number;
177
+ initialBody: string;
178
+ }) {
179
+ const [rating, setRating] = useState(initialRating);
180
+ const t = useTranslations('reviews');
181
+ const [body, setBody] = useState(initialBody);
182
+ const [submitting, setSubmitting] = useState(false);
183
+ const [error, setError] = useState<string | null>(null);
184
+ const [success, setSuccess] = useState(false);
185
+
186
+ async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
187
+ event.preventDefault();
188
+ setError(null);
189
+ setSubmitting(true);
190
+ try {
191
+ const client = getClient();
192
+ const input = { rating, body: body.trim() || undefined };
193
+ if (mode === 'edit') {
194
+ await client.updateMyProductReview(productId, input);
195
+ } else {
196
+ await client.submitProductReview(productId, input);
197
+ }
198
+ setSuccess(true);
199
+ setTimeout(() => window.location.reload(), 1200);
200
+ } catch (err) {
201
+ const status = (err as { status?: number })?.status;
202
+ if (status === 409) {
203
+ setError(t('alreadySubmitted'));
204
+ } else if (status === 403) {
205
+ setError(t('onlyPurchasers'));
206
+ } else if (status === 429) {
207
+ setError(t('tooMany'));
208
+ } else {
209
+ setError(mode === 'edit' ? t('updateFailed') : t('submitFailed'));
210
+ }
211
+ } finally {
212
+ setSubmitting(false);
213
+ }
214
+ }
215
+
216
+ if (success) {
217
+ return <p role="status" className="text-sm font-semibold text-primary">{mode === 'edit' ? t('thanksUpdated') : t('thanksSubmitted')}</p>;
218
+ }
219
+
220
+ return (
221
+ <form onSubmit={handleSubmit} className="space-y-4">
222
+ <h3 className="font-sans text-base font-semibold">{mode === 'edit' ? t('editYourReview') : t('writeReview')}</h3>
223
+
224
+ <fieldset>
225
+ <legend className="mb-1.5 text-sm font-medium">{t('yourRating')}</legend>
226
+ <div className="inline-flex gap-1" role="radiogroup" aria-label={t('starRating')}>
227
+ {[1, 2, 3, 4, 5].map((n) => (
228
+ <button
229
+ key={n}
230
+ type="button"
231
+ role="radio"
232
+ aria-checked={rating === n}
233
+ onClick={() => setRating(n)}
234
+ className={n <= rating ? 'text-primary' : 'text-border hover:text-primary/50'}
235
+ >
236
+ <IconStar size={24} filled={n <= rating} />
237
+ <span className="sr-only">{n}</span>
238
+ </button>
239
+ ))}
240
+ </div>
241
+ </fieldset>
242
+
243
+ <label className="block text-sm font-medium">
244
+ {t('yourReview')} <span className="font-normal text-muted-foreground">({t('optionalField')})</span>
245
+ <textarea
246
+ value={body}
247
+ onChange={(event) => setBody(event.target.value)}
248
+ maxLength={5000}
249
+ rows={4}
250
+ className="input mt-1.5 h-auto min-h-24 py-2.5"
251
+ />
252
+ </label>
253
+
254
+ {error && <p role="alert" className="text-sm font-medium text-destructive">{error}</p>}
255
+
256
+ <button type="submit" disabled={submitting} className="btn-primary btn-sm">
257
+ {submitting
258
+ ? mode === 'edit'
259
+ ? t('saving')
260
+ : t('submittingBtn')
261
+ : mode === 'edit'
262
+ ? t('saveChanges')
263
+ : t('submitReview')}
264
+ </button>
265
+ </form>
266
+ );
267
+ }