create-brainerce-store 1.15.0 → 1.15.2
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/messages/en.json +4 -0
- package/messages/he.json +4 -0
- package/package.json +1 -1
- package/templates/nextjs/base/src/app/checkout/page.tsx +65 -62
- package/templates/nextjs/base/src/components/checkout/checkout-form.tsx +142 -130
- package/templates/nextjs/base/src/components/products/stock-badge.tsx +11 -8
- package/templates/nextjs/base/src/components/products/variant-selector.tsx +292 -147
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.15.
|
|
34
|
+
version: "1.15.2",
|
|
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/messages/en.json
CHANGED
|
@@ -76,6 +76,10 @@
|
|
|
76
76
|
"addingToCart": "Adding...",
|
|
77
77
|
"addedToCart": "Added to Cart!",
|
|
78
78
|
"outOfStock": "Out of Stock",
|
|
79
|
+
"inStock": "In Stock",
|
|
80
|
+
"unavailable": "Unavailable",
|
|
81
|
+
"onlyLeft": "Only {available} left",
|
|
82
|
+
"availableInStock": "{available} in stock",
|
|
79
83
|
"decreaseQuantity": "Decrease quantity",
|
|
80
84
|
"increaseQuantity": "Increase quantity",
|
|
81
85
|
"youMayAlsoLike": "You May Also Like",
|
package/messages/he.json
CHANGED
|
@@ -76,6 +76,10 @@
|
|
|
76
76
|
"addingToCart": "...מוסיף",
|
|
77
77
|
"addedToCart": "נוסף לעגלה!",
|
|
78
78
|
"outOfStock": "אזל מהמלאי",
|
|
79
|
+
"inStock": "במלאי",
|
|
80
|
+
"unavailable": "לא זמין",
|
|
81
|
+
"onlyLeft": "נותרו {available} בלבד",
|
|
82
|
+
"availableInStock": "{available} במלאי",
|
|
79
83
|
"decreaseQuantity": "הפחת כמות",
|
|
80
84
|
"increaseQuantity": "הגדל כמות",
|
|
81
85
|
"youMayAlsoLike": "אולי גם תאהבו",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import { Suspense, useEffect, useState, useCallback } from 'react';
|
|
3
|
+
import { Suspense, useEffect, useState, useCallback, useRef } from 'react';
|
|
4
4
|
import { useSearchParams } from 'next/navigation';
|
|
5
5
|
import Image from 'next/image';
|
|
6
6
|
import Link from 'next/link';
|
|
@@ -76,53 +76,62 @@ function CheckoutContent() {
|
|
|
76
76
|
.catch(() => {});
|
|
77
77
|
}, [isLoggedIn]);
|
|
78
78
|
|
|
79
|
-
// Initialize or resume checkout
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
setInitializing(true);
|
|
83
|
-
setError(null);
|
|
84
|
-
const client = getClient();
|
|
85
|
-
|
|
86
|
-
// Fetch shipping destinations and pickup locations in parallel
|
|
87
|
-
client
|
|
88
|
-
.getShippingDestinations()
|
|
89
|
-
.then(setDestinations)
|
|
90
|
-
.catch(() => {});
|
|
79
|
+
// Initialize or resume checkout (only once)
|
|
80
|
+
const checkoutInitRef = useRef(false);
|
|
81
|
+
const cartIdRef = useRef<string | null>(null);
|
|
91
82
|
|
|
92
|
-
|
|
93
|
-
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
// Only init once, or if cart ID actually changed (e.g. cart was replaced)
|
|
85
|
+
if (!cart?.id) return;
|
|
86
|
+
if (checkoutInitRef.current && cartIdRef.current === cart.id) return;
|
|
87
|
+
checkoutInitRef.current = true;
|
|
88
|
+
cartIdRef.current = cart.id;
|
|
94
89
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
90
|
+
const initCheckout = async () => {
|
|
91
|
+
try {
|
|
92
|
+
setInitializing(true);
|
|
93
|
+
setError(null);
|
|
94
|
+
const client = getClient();
|
|
99
95
|
|
|
100
|
-
//
|
|
101
|
-
|
|
102
|
-
(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
//
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
96
|
+
// Fetch shipping destinations and pickup locations in parallel
|
|
97
|
+
client
|
|
98
|
+
.getShippingDestinations()
|
|
99
|
+
.then(setDestinations)
|
|
100
|
+
.catch(() => {});
|
|
101
|
+
|
|
102
|
+
const locations = await client.getPickupLocations().catch(() => [] as PickupLocation[]);
|
|
103
|
+
setPickupLocations(locations);
|
|
104
|
+
|
|
105
|
+
// If returning with existing checkout ID, resume it
|
|
106
|
+
if (existingCheckoutId) {
|
|
107
|
+
const existing = await client.getCheckout(existingCheckoutId);
|
|
108
|
+
setCheckout(existing);
|
|
109
|
+
|
|
110
|
+
// Determine step based on checkout state
|
|
111
|
+
const allDigital = existing.lineItems.every(
|
|
112
|
+
(i) => (i.product as unknown as { isDownloadable?: boolean }).isDownloadable
|
|
113
|
+
);
|
|
114
|
+
setIsAllDigital(allDigital);
|
|
115
|
+
if (allDigital) {
|
|
116
|
+
// Digital products: show contact info step if email not set, else payment
|
|
117
|
+
setStep(existing.email ? 'payment' : 'address');
|
|
118
|
+
} else if (existing.deliveryType === 'pickup' && existing.pickupLocation) {
|
|
119
|
+
setDeliveryType('pickup');
|
|
120
|
+
setStep('payment');
|
|
121
|
+
} else if (existing.shippingAddress && existing.shippingRateId) {
|
|
122
|
+
setStep('payment');
|
|
123
|
+
} else if (existing.shippingAddress) {
|
|
124
|
+
// Fetch shipping rates
|
|
125
|
+
const rates = await client.getShippingRates(existing.id);
|
|
126
|
+
setShippingRates(rates);
|
|
127
|
+
setStep('shipping');
|
|
128
|
+
} else if (locations.length > 0) {
|
|
129
|
+
setStep('method');
|
|
130
|
+
}
|
|
131
|
+
return;
|
|
120
132
|
}
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
133
|
|
|
124
|
-
|
|
125
|
-
if (cart && cart.id) {
|
|
134
|
+
// Create new checkout — cart is always server-side now
|
|
126
135
|
const newCheckout = await client.createCheckout({ cartId: cart.id });
|
|
127
136
|
setCheckout(newCheckout);
|
|
128
137
|
|
|
@@ -135,28 +144,22 @@ function CheckoutContent() {
|
|
|
135
144
|
setStep('address');
|
|
136
145
|
return;
|
|
137
146
|
}
|
|
138
|
-
} else {
|
|
139
|
-
setError(t('cartIsEmpty'));
|
|
140
|
-
}
|
|
141
147
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
148
|
+
// If pickup locations exist, start with delivery method selection
|
|
149
|
+
if (locations.length > 0) {
|
|
150
|
+
setStep('method');
|
|
151
|
+
}
|
|
152
|
+
} catch (err) {
|
|
153
|
+
const message = err instanceof Error ? err.message : t('failedToInitCheckout');
|
|
154
|
+
setError(message);
|
|
155
|
+
} finally {
|
|
156
|
+
setInitializing(false);
|
|
145
157
|
}
|
|
146
|
-
}
|
|
147
|
-
const message = err instanceof Error ? err.message : t('failedToInitCheckout');
|
|
148
|
-
setError(message);
|
|
149
|
-
} finally {
|
|
150
|
-
setInitializing(false);
|
|
151
|
-
}
|
|
152
|
-
}, [existingCheckoutId, cart]);
|
|
158
|
+
};
|
|
153
159
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
initCheckout();
|
|
158
|
-
}
|
|
159
|
-
}, [cartLoaded, initCheckout]);
|
|
160
|
+
initCheckout();
|
|
161
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
162
|
+
}, [cart?.id, existingCheckoutId]);
|
|
160
163
|
|
|
161
164
|
// Handle shipping address submission
|
|
162
165
|
async function handleAddressSubmit(
|
|
@@ -189,147 +189,159 @@ export function CheckoutForm({
|
|
|
189
189
|
|
|
190
190
|
{!emailOnly && (
|
|
191
191
|
<>
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
{
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
192
|
+
{/* Country + Region row */}
|
|
193
|
+
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
|
194
|
+
<div>
|
|
195
|
+
<label htmlFor="country" className="text-foreground mb-1 block text-sm font-medium">
|
|
196
|
+
{t('country')} <span className="text-destructive">*</span>
|
|
197
|
+
</label>
|
|
198
|
+
{hasCountryOptions ? (
|
|
199
|
+
<select
|
|
200
|
+
id="country"
|
|
201
|
+
value={formData.country}
|
|
202
|
+
onChange={(e) => updateField('country', e.target.value)}
|
|
203
|
+
className={cn(
|
|
204
|
+
selectClass,
|
|
205
|
+
errors.country ? 'border-destructive' : 'border-border'
|
|
206
|
+
)}
|
|
207
|
+
>
|
|
208
|
+
<option value="">{t('selectCountry')}</option>
|
|
209
|
+
{destinations.countries.map((c) => (
|
|
210
|
+
<option key={c.code} value={c.code}>
|
|
211
|
+
{c.name}
|
|
212
|
+
</option>
|
|
213
|
+
))}
|
|
214
|
+
</select>
|
|
215
|
+
) : (
|
|
216
|
+
<input
|
|
217
|
+
id="country"
|
|
218
|
+
type="text"
|
|
219
|
+
value={formData.country}
|
|
220
|
+
onChange={(e) => updateField('country', e.target.value)}
|
|
221
|
+
className={cn(
|
|
222
|
+
inputClass,
|
|
223
|
+
errors.country ? 'border-destructive' : 'border-border'
|
|
224
|
+
)}
|
|
225
|
+
placeholder={t('countryPlaceholder')}
|
|
226
|
+
/>
|
|
227
|
+
)}
|
|
228
|
+
{errors.country && <p className="text-destructive mt-1 text-xs">{errors.country}</p>}
|
|
229
|
+
</div>
|
|
230
|
+
|
|
231
|
+
<div>
|
|
232
|
+
<label htmlFor="region" className="text-foreground mb-1 block text-sm font-medium">
|
|
233
|
+
{t('stateRegion')}
|
|
234
|
+
</label>
|
|
235
|
+
{hasRegionOptions ? (
|
|
236
|
+
<select
|
|
237
|
+
id="region"
|
|
238
|
+
value={formData.region || ''}
|
|
239
|
+
onChange={(e) => updateField('region', e.target.value)}
|
|
240
|
+
className={cn(selectClass, 'border-border')}
|
|
241
|
+
>
|
|
242
|
+
<option value="">{t('selectRegion')}</option>
|
|
243
|
+
{countryRegions.map((r) => (
|
|
244
|
+
<option key={r.code} value={r.code}>
|
|
245
|
+
{r.name}
|
|
246
|
+
</option>
|
|
247
|
+
))}
|
|
248
|
+
</select>
|
|
249
|
+
) : (
|
|
250
|
+
<input
|
|
251
|
+
id="region"
|
|
252
|
+
type="text"
|
|
253
|
+
value={formData.region || ''}
|
|
254
|
+
onChange={(e) => updateField('region', e.target.value)}
|
|
255
|
+
className={cn(inputClass, 'border-border')}
|
|
256
|
+
/>
|
|
257
|
+
)}
|
|
258
|
+
</div>
|
|
259
|
+
</div>
|
|
260
|
+
|
|
261
|
+
{/* Address line 1 */}
|
|
262
|
+
<div>
|
|
263
|
+
<label htmlFor="line1" className="text-foreground mb-1 block text-sm font-medium">
|
|
264
|
+
{t('address')} <span className="text-destructive">*</span>
|
|
265
|
+
</label>
|
|
213
266
|
<input
|
|
214
|
-
id="
|
|
267
|
+
id="line1"
|
|
215
268
|
type="text"
|
|
216
|
-
value={formData.
|
|
217
|
-
onChange={(e) => updateField('
|
|
218
|
-
className={cn(inputClass, errors.
|
|
219
|
-
placeholder={t('
|
|
269
|
+
value={formData.line1}
|
|
270
|
+
onChange={(e) => updateField('line1', e.target.value)}
|
|
271
|
+
className={cn(inputClass, errors.line1 ? 'border-destructive' : 'border-border')}
|
|
272
|
+
placeholder={t('streetAddress')}
|
|
220
273
|
/>
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
</div>
|
|
274
|
+
{errors.line1 && <p className="text-destructive mt-1 text-xs">{errors.line1}</p>}
|
|
275
|
+
</div>
|
|
224
276
|
|
|
225
|
-
|
|
226
|
-
<
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
<select
|
|
231
|
-
id="region"
|
|
232
|
-
value={formData.region || ''}
|
|
233
|
-
onChange={(e) => updateField('region', e.target.value)}
|
|
234
|
-
className={cn(selectClass, 'border-border')}
|
|
235
|
-
>
|
|
236
|
-
<option value="">{t('selectRegion')}</option>
|
|
237
|
-
{countryRegions.map((r) => (
|
|
238
|
-
<option key={r.code} value={r.code}>
|
|
239
|
-
{r.name}
|
|
240
|
-
</option>
|
|
241
|
-
))}
|
|
242
|
-
</select>
|
|
243
|
-
) : (
|
|
277
|
+
{/* Address line 2 */}
|
|
278
|
+
<div>
|
|
279
|
+
<label htmlFor="line2" className="text-foreground mb-1 block text-sm font-medium">
|
|
280
|
+
{t('apartmentSuite')}
|
|
281
|
+
</label>
|
|
244
282
|
<input
|
|
245
|
-
id="
|
|
283
|
+
id="line2"
|
|
246
284
|
type="text"
|
|
247
|
-
value={formData.
|
|
248
|
-
onChange={(e) => updateField('
|
|
285
|
+
value={formData.line2 || ''}
|
|
286
|
+
onChange={(e) => updateField('line2', e.target.value)}
|
|
249
287
|
className={cn(inputClass, 'border-border')}
|
|
288
|
+
placeholder={t('aptPlaceholder')}
|
|
250
289
|
/>
|
|
251
|
-
|
|
252
|
-
</div>
|
|
253
|
-
</div>
|
|
254
|
-
|
|
255
|
-
{/* Address line 1 */}
|
|
256
|
-
<div>
|
|
257
|
-
<label htmlFor="line1" className="text-foreground mb-1 block text-sm font-medium">
|
|
258
|
-
{t('address')} <span className="text-destructive">*</span>
|
|
259
|
-
</label>
|
|
260
|
-
<input
|
|
261
|
-
id="line1"
|
|
262
|
-
type="text"
|
|
263
|
-
value={formData.line1}
|
|
264
|
-
onChange={(e) => updateField('line1', e.target.value)}
|
|
265
|
-
className={cn(inputClass, errors.line1 ? 'border-destructive' : 'border-border')}
|
|
266
|
-
placeholder={t('streetAddress')}
|
|
267
|
-
/>
|
|
268
|
-
{errors.line1 && <p className="text-destructive mt-1 text-xs">{errors.line1}</p>}
|
|
269
|
-
</div>
|
|
270
|
-
|
|
271
|
-
{/* Address line 2 */}
|
|
272
|
-
<div>
|
|
273
|
-
<label htmlFor="line2" className="text-foreground mb-1 block text-sm font-medium">
|
|
274
|
-
{t('apartmentSuite')}
|
|
275
|
-
</label>
|
|
276
|
-
<input
|
|
277
|
-
id="line2"
|
|
278
|
-
type="text"
|
|
279
|
-
value={formData.line2 || ''}
|
|
280
|
-
onChange={(e) => updateField('line2', e.target.value)}
|
|
281
|
-
className={cn(inputClass, 'border-border')}
|
|
282
|
-
placeholder={t('aptPlaceholder')}
|
|
283
|
-
/>
|
|
284
|
-
</div>
|
|
290
|
+
</div>
|
|
285
291
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
292
|
+
{/* City + Postal code row */}
|
|
293
|
+
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
|
294
|
+
<div>
|
|
295
|
+
<label htmlFor="city" className="text-foreground mb-1 block text-sm font-medium">
|
|
296
|
+
{t('city')} <span className="text-destructive">*</span>
|
|
297
|
+
</label>
|
|
298
|
+
<input
|
|
299
|
+
id="city"
|
|
300
|
+
type="text"
|
|
301
|
+
value={formData.city}
|
|
302
|
+
onChange={(e) => updateField('city', e.target.value)}
|
|
303
|
+
className={cn(inputClass, errors.city ? 'border-destructive' : 'border-border')}
|
|
304
|
+
/>
|
|
305
|
+
{errors.city && <p className="text-destructive mt-1 text-xs">{errors.city}</p>}
|
|
306
|
+
</div>
|
|
301
307
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
308
|
+
<div>
|
|
309
|
+
<label
|
|
310
|
+
htmlFor="postalCode"
|
|
311
|
+
className="text-foreground mb-1 block text-sm font-medium"
|
|
312
|
+
>
|
|
313
|
+
{t('postalCode')} <span className="text-destructive">*</span>
|
|
314
|
+
</label>
|
|
315
|
+
<input
|
|
316
|
+
id="postalCode"
|
|
317
|
+
type="text"
|
|
318
|
+
value={formData.postalCode}
|
|
319
|
+
onChange={(e) => updateField('postalCode', e.target.value)}
|
|
320
|
+
className={cn(
|
|
321
|
+
inputClass,
|
|
322
|
+
errors.postalCode ? 'border-destructive' : 'border-border'
|
|
323
|
+
)}
|
|
324
|
+
/>
|
|
325
|
+
{errors.postalCode && (
|
|
326
|
+
<p className="text-destructive mt-1 text-xs">{errors.postalCode}</p>
|
|
327
|
+
)}
|
|
328
|
+
</div>
|
|
329
|
+
</div>
|
|
318
330
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
331
|
+
{/* Phone */}
|
|
332
|
+
<div>
|
|
333
|
+
<label htmlFor="phone" className="text-foreground mb-1 block text-sm font-medium">
|
|
334
|
+
{t('phone')}
|
|
335
|
+
</label>
|
|
336
|
+
<input
|
|
337
|
+
id="phone"
|
|
338
|
+
type="tel"
|
|
339
|
+
value={formData.phone || ''}
|
|
340
|
+
onChange={(e) => updateField('phone', e.target.value)}
|
|
341
|
+
className={cn(inputClass, 'border-border')}
|
|
342
|
+
placeholder={t('phonePlaceholder')}
|
|
343
|
+
/>
|
|
344
|
+
</div>
|
|
333
345
|
</>
|
|
334
346
|
)}
|
|
335
347
|
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import type { InventoryInfo } from 'brainerce';
|
|
4
4
|
import { cn } from '@/lib/utils';
|
|
5
|
+
import { useTranslations } from '@/lib/translations';
|
|
5
6
|
|
|
6
7
|
interface StockBadgeProps {
|
|
7
8
|
inventory: InventoryInfo | null | undefined;
|
|
@@ -10,7 +11,8 @@ interface StockBadgeProps {
|
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
export function StockBadge({ inventory, lowStockThreshold = 5, className }: StockBadgeProps) {
|
|
13
|
-
const
|
|
14
|
+
const t = useTranslations('productDetail');
|
|
15
|
+
const label = getStockLabel(inventory, lowStockThreshold, t);
|
|
14
16
|
const color = getStockColor(inventory, lowStockThreshold);
|
|
15
17
|
|
|
16
18
|
return (
|
|
@@ -28,21 +30,22 @@ export function StockBadge({ inventory, lowStockThreshold = 5, className }: Stoc
|
|
|
28
30
|
|
|
29
31
|
function getStockLabel(
|
|
30
32
|
inventory: InventoryInfo | null | undefined,
|
|
31
|
-
lowStockThreshold: number
|
|
33
|
+
lowStockThreshold: number,
|
|
34
|
+
t: (key: string) => string
|
|
32
35
|
): string {
|
|
33
|
-
if (!inventory) return '
|
|
36
|
+
if (!inventory) return t('outOfStock');
|
|
34
37
|
|
|
35
38
|
const { trackingMode, inStock, available } = inventory;
|
|
36
39
|
|
|
37
|
-
if (trackingMode === 'DISABLED') return '
|
|
38
|
-
if (!inStock) return '
|
|
39
|
-
if (trackingMode === 'UNLIMITED') return '
|
|
40
|
+
if (trackingMode === 'DISABLED') return t('unavailable');
|
|
41
|
+
if (!inStock) return t('outOfStock');
|
|
42
|
+
if (trackingMode === 'UNLIMITED') return t('inStock');
|
|
40
43
|
|
|
41
44
|
// TRACKED — show actual quantity
|
|
42
45
|
if (available <= lowStockThreshold) {
|
|
43
|
-
return
|
|
46
|
+
return t('onlyLeft').replace('{available}', String(available));
|
|
44
47
|
}
|
|
45
|
-
return
|
|
48
|
+
return t('availableInStock').replace('{available}', String(available));
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
function getStockColor(
|
|
@@ -1,147 +1,292 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import { useMemo } from 'react';
|
|
4
|
-
import type { Product, ProductVariant } from 'brainerce';
|
|
5
|
-
import { getVariantOptions,
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
return
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useMemo } from 'react';
|
|
4
|
+
import type { Product, ProductVariant } from 'brainerce';
|
|
5
|
+
import { getVariantOptions, getProductSwatches, formatPrice } from 'brainerce';
|
|
6
|
+
import type { InventoryInfo } from 'brainerce';
|
|
7
|
+
import { useStoreInfo } from '@/providers/store-provider';
|
|
8
|
+
import { useTranslations } from '@/lib/translations';
|
|
9
|
+
import { cn } from '@/lib/utils';
|
|
10
|
+
|
|
11
|
+
interface VariantSelectorProps {
|
|
12
|
+
product: Product;
|
|
13
|
+
selectedVariant: ProductVariant | null;
|
|
14
|
+
onVariantChange: (variant: ProductVariant) => void;
|
|
15
|
+
className?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface AttributeGroup {
|
|
19
|
+
name: string;
|
|
20
|
+
displayType: string;
|
|
21
|
+
values: Array<{
|
|
22
|
+
value: string;
|
|
23
|
+
swatchColor?: string | null;
|
|
24
|
+
swatchColor2?: string | null;
|
|
25
|
+
swatchImageUrl?: string | null;
|
|
26
|
+
variants: ProductVariant[];
|
|
27
|
+
}>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function VariantSelector({
|
|
31
|
+
product,
|
|
32
|
+
selectedVariant,
|
|
33
|
+
onVariantChange,
|
|
34
|
+
className,
|
|
35
|
+
}: VariantSelectorProps) {
|
|
36
|
+
const { storeInfo } = useStoreInfo();
|
|
37
|
+
const t = useTranslations('productDetail');
|
|
38
|
+
const currency = storeInfo?.currency || 'USD';
|
|
39
|
+
const variants = useMemo(() => product.variants || [], [product.variants]);
|
|
40
|
+
|
|
41
|
+
// Get swatch metadata from product attribute options
|
|
42
|
+
const swatchData = useMemo(() => getProductSwatches(product), [product]);
|
|
43
|
+
const swatchMap = useMemo(() => {
|
|
44
|
+
const map = new Map<
|
|
45
|
+
string,
|
|
46
|
+
{
|
|
47
|
+
displayType: string;
|
|
48
|
+
options: Map<
|
|
49
|
+
string,
|
|
50
|
+
{
|
|
51
|
+
swatchColor?: string | null;
|
|
52
|
+
swatchColor2?: string | null;
|
|
53
|
+
swatchImageUrl?: string | null;
|
|
54
|
+
}
|
|
55
|
+
>;
|
|
56
|
+
}
|
|
57
|
+
>();
|
|
58
|
+
for (const attr of swatchData) {
|
|
59
|
+
const optMap = new Map<
|
|
60
|
+
string,
|
|
61
|
+
{
|
|
62
|
+
swatchColor?: string | null;
|
|
63
|
+
swatchColor2?: string | null;
|
|
64
|
+
swatchImageUrl?: string | null;
|
|
65
|
+
}
|
|
66
|
+
>();
|
|
67
|
+
for (const opt of attr.options) {
|
|
68
|
+
optMap.set(opt.name, {
|
|
69
|
+
swatchColor: opt.swatchColor,
|
|
70
|
+
swatchColor2: opt.swatchColor2,
|
|
71
|
+
swatchImageUrl: opt.swatchImageUrl,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
map.set(attr.attributeName, { displayType: attr.displayType, options: optMap });
|
|
75
|
+
}
|
|
76
|
+
return map;
|
|
77
|
+
}, [swatchData]);
|
|
78
|
+
|
|
79
|
+
// Build attribute groups from variant data, enriched with swatch info
|
|
80
|
+
const attributeGroups = useMemo<AttributeGroup[]>(() => {
|
|
81
|
+
const groups = new Map<string, Map<string, ProductVariant[]>>();
|
|
82
|
+
|
|
83
|
+
for (const variant of variants) {
|
|
84
|
+
const options = getVariantOptions(variant);
|
|
85
|
+
for (const { name, value } of options) {
|
|
86
|
+
if (!groups.has(name)) {
|
|
87
|
+
groups.set(name, new Map());
|
|
88
|
+
}
|
|
89
|
+
const valuesMap = groups.get(name)!;
|
|
90
|
+
if (!valuesMap.has(value)) {
|
|
91
|
+
valuesMap.set(value, []);
|
|
92
|
+
}
|
|
93
|
+
valuesMap.get(value)!.push(variant);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return Array.from(groups.entries()).map(([name, valuesMap]) => {
|
|
98
|
+
const attrSwatch = swatchMap.get(name);
|
|
99
|
+
return {
|
|
100
|
+
name,
|
|
101
|
+
displayType: attrSwatch?.displayType || 'DROPDOWN',
|
|
102
|
+
values: Array.from(valuesMap.entries()).map(([value, variantList]) => {
|
|
103
|
+
const optSwatch = attrSwatch?.options.get(value);
|
|
104
|
+
return {
|
|
105
|
+
value,
|
|
106
|
+
swatchColor: optSwatch?.swatchColor,
|
|
107
|
+
swatchColor2: optSwatch?.swatchColor2,
|
|
108
|
+
swatchImageUrl: optSwatch?.swatchImageUrl,
|
|
109
|
+
variants: variantList,
|
|
110
|
+
};
|
|
111
|
+
}),
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
}, [variants, swatchMap]);
|
|
115
|
+
|
|
116
|
+
// Get currently selected attribute values
|
|
117
|
+
const selectedOptions = useMemo(() => {
|
|
118
|
+
if (!selectedVariant) return new Map<string, string>();
|
|
119
|
+
const opts = getVariantOptions(selectedVariant);
|
|
120
|
+
return new Map(opts.map(({ name, value }) => [name, value]));
|
|
121
|
+
}, [selectedVariant]);
|
|
122
|
+
|
|
123
|
+
// Find the variant that matches all selected attributes
|
|
124
|
+
function findMatchingVariant(
|
|
125
|
+
attributeName: string,
|
|
126
|
+
newValue: string
|
|
127
|
+
): ProductVariant | undefined {
|
|
128
|
+
const nextSelection = new Map(selectedOptions);
|
|
129
|
+
nextSelection.set(attributeName, newValue);
|
|
130
|
+
|
|
131
|
+
return variants.find((v) => {
|
|
132
|
+
const opts = getVariantOptions(v);
|
|
133
|
+
return Array.from(nextSelection.entries()).every(([name, value]) =>
|
|
134
|
+
opts.some((o) => o.name === name && o.value === value)
|
|
135
|
+
);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (attributeGroups.length === 0) return null;
|
|
140
|
+
|
|
141
|
+
return (
|
|
142
|
+
<div className={cn('space-y-4', className)}>
|
|
143
|
+
{attributeGroups.map((group) => (
|
|
144
|
+
<div key={group.name}>
|
|
145
|
+
<label className="text-foreground mb-2 block text-sm font-medium">
|
|
146
|
+
{group.name}
|
|
147
|
+
{selectedOptions.get(group.name) && (
|
|
148
|
+
<span className="text-muted-foreground ms-1 font-normal">
|
|
149
|
+
: {selectedOptions.get(group.name)}
|
|
150
|
+
</span>
|
|
151
|
+
)}
|
|
152
|
+
</label>
|
|
153
|
+
<div className="flex flex-wrap gap-2">
|
|
154
|
+
{group.values.map(
|
|
155
|
+
({
|
|
156
|
+
value,
|
|
157
|
+
swatchColor,
|
|
158
|
+
swatchColor2,
|
|
159
|
+
swatchImageUrl,
|
|
160
|
+
variants: matchingVariants,
|
|
161
|
+
}) => {
|
|
162
|
+
const isSelected = selectedOptions.get(group.name) === value;
|
|
163
|
+
const matchedVariant = findMatchingVariant(group.name, value);
|
|
164
|
+
const isAvailable = matchedVariant?.inventory?.canPurchase !== false;
|
|
165
|
+
|
|
166
|
+
// Color swatch rendering
|
|
167
|
+
if (group.displayType === 'COLOR_SWATCH' && swatchColor) {
|
|
168
|
+
return (
|
|
169
|
+
<button
|
|
170
|
+
key={value}
|
|
171
|
+
type="button"
|
|
172
|
+
disabled={!isAvailable}
|
|
173
|
+
title={value}
|
|
174
|
+
onClick={() => {
|
|
175
|
+
const variant = matchedVariant || matchingVariants[0];
|
|
176
|
+
if (variant) onVariantChange(variant);
|
|
177
|
+
}}
|
|
178
|
+
className={cn(
|
|
179
|
+
'h-9 w-9 rounded-full border-2 transition-all',
|
|
180
|
+
isSelected
|
|
181
|
+
? 'border-primary ring-primary/30 ring-2'
|
|
182
|
+
: isAvailable
|
|
183
|
+
? 'border-border hover:border-primary'
|
|
184
|
+
: 'cursor-not-allowed opacity-40'
|
|
185
|
+
)}
|
|
186
|
+
style={{
|
|
187
|
+
background: swatchColor2
|
|
188
|
+
? `linear-gradient(135deg, ${swatchColor} 50%, ${swatchColor2} 50%)`
|
|
189
|
+
: swatchColor,
|
|
190
|
+
}}
|
|
191
|
+
>
|
|
192
|
+
{!isAvailable && (
|
|
193
|
+
<span
|
|
194
|
+
className="bg-muted-foreground block h-full w-full rounded-full opacity-50"
|
|
195
|
+
style={{
|
|
196
|
+
backgroundImage:
|
|
197
|
+
'linear-gradient(135deg, transparent 45%, currentColor 45%, currentColor 55%, transparent 55%)',
|
|
198
|
+
}}
|
|
199
|
+
/>
|
|
200
|
+
)}
|
|
201
|
+
</button>
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Image swatch rendering
|
|
206
|
+
if (group.displayType === 'IMAGE_SWATCH' && swatchImageUrl) {
|
|
207
|
+
return (
|
|
208
|
+
<button
|
|
209
|
+
key={value}
|
|
210
|
+
type="button"
|
|
211
|
+
disabled={!isAvailable}
|
|
212
|
+
title={value}
|
|
213
|
+
onClick={() => {
|
|
214
|
+
const variant = matchedVariant || matchingVariants[0];
|
|
215
|
+
if (variant) onVariantChange(variant);
|
|
216
|
+
}}
|
|
217
|
+
className={cn(
|
|
218
|
+
'h-10 w-10 overflow-hidden rounded-lg border-2 transition-all',
|
|
219
|
+
isSelected
|
|
220
|
+
? 'border-primary ring-primary/30 ring-2'
|
|
221
|
+
: isAvailable
|
|
222
|
+
? 'border-border hover:border-primary'
|
|
223
|
+
: 'cursor-not-allowed opacity-40'
|
|
224
|
+
)}
|
|
225
|
+
>
|
|
226
|
+
<img
|
|
227
|
+
src={swatchImageUrl}
|
|
228
|
+
alt={value}
|
|
229
|
+
className="h-full w-full object-cover"
|
|
230
|
+
/>
|
|
231
|
+
</button>
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Default button rendering (BUTTON, DROPDOWN, or fallback)
|
|
236
|
+
return (
|
|
237
|
+
<button
|
|
238
|
+
key={value}
|
|
239
|
+
type="button"
|
|
240
|
+
disabled={!isAvailable}
|
|
241
|
+
onClick={() => {
|
|
242
|
+
const variant = matchedVariant || matchingVariants[0];
|
|
243
|
+
if (variant) onVariantChange(variant);
|
|
244
|
+
}}
|
|
245
|
+
className={cn(
|
|
246
|
+
'rounded border px-4 py-2 text-sm transition-colors',
|
|
247
|
+
isSelected
|
|
248
|
+
? 'border-primary bg-primary text-primary-foreground'
|
|
249
|
+
: isAvailable
|
|
250
|
+
? 'border-border bg-background text-foreground hover:border-primary'
|
|
251
|
+
: 'border-border bg-muted text-muted-foreground cursor-not-allowed line-through opacity-50'
|
|
252
|
+
)}
|
|
253
|
+
>
|
|
254
|
+
{value}
|
|
255
|
+
</button>
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
)}
|
|
259
|
+
</div>
|
|
260
|
+
</div>
|
|
261
|
+
))}
|
|
262
|
+
|
|
263
|
+
{/* Variant-specific info */}
|
|
264
|
+
{selectedVariant && (
|
|
265
|
+
<div className="text-muted-foreground flex items-center gap-3 pt-1 text-sm">
|
|
266
|
+
{selectedVariant.price && (
|
|
267
|
+
<span>
|
|
268
|
+
{
|
|
269
|
+
formatPrice(selectedVariant.salePrice || selectedVariant.price, {
|
|
270
|
+
currency,
|
|
271
|
+
}) as string
|
|
272
|
+
}
|
|
273
|
+
</span>
|
|
274
|
+
)}
|
|
275
|
+
<span>{getTranslatedStockStatus(selectedVariant.inventory, t)}</span>
|
|
276
|
+
</div>
|
|
277
|
+
)}
|
|
278
|
+
</div>
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function getTranslatedStockStatus(
|
|
283
|
+
inventory: InventoryInfo | null | undefined,
|
|
284
|
+
t: (key: string) => string
|
|
285
|
+
): string {
|
|
286
|
+
if (!inventory) return t('outOfStock');
|
|
287
|
+
const { trackingMode, inStock, available } = inventory;
|
|
288
|
+
if (trackingMode === 'DISABLED') return t('unavailable');
|
|
289
|
+
if (!inStock) return t('outOfStock');
|
|
290
|
+
if (trackingMode === 'UNLIMITED') return t('inStock');
|
|
291
|
+
return t('availableInStock').replace('{available}', String(available));
|
|
292
|
+
}
|