create-brainerce-store 1.15.1 → 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/products/stock-badge.tsx +11 -8
- package/templates/nextjs/base/src/components/products/variant-selector.tsx +17 -2
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(
|
|
@@ -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(
|
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
import { useMemo } from 'react';
|
|
4
4
|
import type { Product, ProductVariant } from 'brainerce';
|
|
5
|
-
import { getVariantOptions, getProductSwatches,
|
|
5
|
+
import { getVariantOptions, getProductSwatches, formatPrice } from 'brainerce';
|
|
6
|
+
import type { InventoryInfo } from 'brainerce';
|
|
6
7
|
import { useStoreInfo } from '@/providers/store-provider';
|
|
8
|
+
import { useTranslations } from '@/lib/translations';
|
|
7
9
|
import { cn } from '@/lib/utils';
|
|
8
10
|
|
|
9
11
|
interface VariantSelectorProps {
|
|
@@ -32,6 +34,7 @@ export function VariantSelector({
|
|
|
32
34
|
className,
|
|
33
35
|
}: VariantSelectorProps) {
|
|
34
36
|
const { storeInfo } = useStoreInfo();
|
|
37
|
+
const t = useTranslations('productDetail');
|
|
35
38
|
const currency = storeInfo?.currency || 'USD';
|
|
36
39
|
const variants = useMemo(() => product.variants || [], [product.variants]);
|
|
37
40
|
|
|
@@ -269,9 +272,21 @@ export function VariantSelector({
|
|
|
269
272
|
}
|
|
270
273
|
</span>
|
|
271
274
|
)}
|
|
272
|
-
<span>{
|
|
275
|
+
<span>{getTranslatedStockStatus(selectedVariant.inventory, t)}</span>
|
|
273
276
|
</div>
|
|
274
277
|
)}
|
|
275
278
|
</div>
|
|
276
279
|
);
|
|
277
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
|
+
}
|