create-brainerce-store 1.78.0 → 1.80.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 +27 -2
- package/messages/en.json +12 -3
- package/messages/he.json +12 -3
- package/package.json +10 -1
- package/templates/nextjs/base/.eslintrc.json +2 -0
- package/templates/nextjs/base/AGENTS.md.ejs +23 -5
- package/templates/nextjs/base/CLAUDE.md.ejs +23 -5
- package/templates/nextjs/base/src/app/checkout/page.tsx +19 -2
- package/templates/nextjs/base/src/app/order-status/page.tsx +18 -3
- package/templates/nextjs/base/src/app/products/[slug]/page.tsx +220 -214
- package/templates/nextjs/base/src/components/account/order-history.tsx +11 -4
- package/templates/nextjs/base/src/components/account/profile-section.tsx +7 -1
- package/templates/nextjs/base/src/components/auth/register-form.tsx +18 -1
- package/templates/nextjs/base/src/components/checkout/payment-step.tsx +14 -2
- package/templates/nextjs/base/src/components/tracking-bootstrap.tsx +1 -1
- package/templates/nextjs/base/src/core/hooks/use-product-page.ts +343 -328
- package/templates/nextjs/base/src/core/lib/kit.ts +88 -0
- package/templates/nextjs/base/src/ui/cart/gift-card-input.tsx +87 -12
- package/templates/nextjs/base/src/ui/layout/newsletter-signup.tsx +59 -12
- package/templates/nextjs/base/src/ui/product/frequently-bought-together.tsx +205 -197
- package/templates/nextjs/base/src/ui/product/product-card.tsx +230 -221
- package/templates/nextjs/base/src/ui/product/product-client-section.tsx +524 -493
- package/templates/nextjs/base/src/ui/product/recommendation-section.tsx +117 -108
- package/templates/nextjs/base/src/ui/product/stock-badge.tsx +23 -3
- package/templates/nextjs/designs/atelier/ui/product/frequently-bought-together.tsx +210 -202
- package/templates/nextjs/designs/atelier/ui/product/product-card.tsx +251 -242
- package/templates/nextjs/designs/atelier/ui/product/product-client-section.tsx +540 -509
- package/templates/nextjs/designs/atelier/ui/product/recommendation-section.tsx +110 -101
- package/templates/nextjs/designs/atelier/ui/product/stock-badge.tsx +22 -2
- package/templates/nextjs/ui-canvas/cart/gift-card-input.tsx +41 -11
- package/templates/nextjs/ui-canvas/layout/newsletter-signup.tsx +50 -8
- package/templates/nextjs/ui-canvas/product/frequently-bought-together.tsx +182 -174
- package/templates/nextjs/ui-canvas/product/product-card.tsx +174 -165
- package/templates/nextjs/ui-canvas/product/product-client-section.tsx +31 -0
- package/templates/nextjs/ui-canvas/product/recommendation-section.tsx +114 -105
- package/templates/nextjs/ui-canvas/product/stock-badge.tsx +22 -2
|
@@ -1,174 +1,182 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import { useState } from 'react';
|
|
4
|
-
import { CdnImage as Image } from '@/ui/shared/cdn-image';
|
|
5
|
-
import type { Product, ProductRecommendation } from 'brainerce';
|
|
6
|
-
import { formatPrice } from 'brainerce';
|
|
7
|
-
import { useCart, useStoreInfo } from '@/core/providers/store-provider';
|
|
8
|
-
import { useCurrency } from '@/core/lib/use-currency';
|
|
9
|
-
import { useTranslations } from '@/core/lib/translations';
|
|
10
|
-
import { cn } from '@/core/lib/utils';
|
|
11
|
-
|
|
12
|
-
interface FrequentlyBoughtTogetherProps {
|
|
13
|
-
items: ProductRecommendation[];
|
|
14
|
-
currentProduct: Product;
|
|
15
|
-
className?: string;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// ⛔ STORE CURRENCY THROUGHOUT, deliberately. The cross-sells are
|
|
19
|
-
// `ProductRecommendation`, a trimmed shape with no displayPrice/displayCurrency,
|
|
20
|
-
// so there is nothing to convert them to. Converting only `currentProduct`
|
|
21
|
-
// (which is a full Product and does carry FX fields) would add a euro amount to
|
|
22
|
-
// two dollar amounts and print the result as one total. One currency wins, and
|
|
23
|
-
// it has to be the one the cart actually charges.
|
|
24
|
-
function getEffectivePrice(item: { basePrice: string; salePrice?: string | null }): number {
|
|
25
|
-
const sale = item.salePrice ? parseFloat(item.salePrice) : null;
|
|
26
|
-
const base = parseFloat(item.basePrice);
|
|
27
|
-
return sale != null && sale < base ? sale : base;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function ProductThumb({
|
|
31
|
-
name,
|
|
32
|
-
imageUrl,
|
|
33
|
-
price,
|
|
34
|
-
currency,
|
|
35
|
-
checked,
|
|
36
|
-
onToggle,
|
|
37
|
-
disabled,
|
|
38
|
-
}: {
|
|
39
|
-
name: string;
|
|
40
|
-
imageUrl: string | null;
|
|
41
|
-
price: number;
|
|
42
|
-
currency: string;
|
|
43
|
-
checked: boolean;
|
|
44
|
-
onToggle?: () => void;
|
|
45
|
-
disabled?: boolean;
|
|
46
|
-
}) {
|
|
47
|
-
return (
|
|
48
|
-
<label className="inline-flex items-center gap-2">
|
|
49
|
-
{onToggle && <input type="checkbox" checked={checked} onChange={onToggle} disabled={disabled} />}
|
|
50
|
-
{/* `relative` + fixed box are layout-critical for next/image fill */}
|
|
51
|
-
<span className="relative block h-20 w-20">
|
|
52
|
-
{imageUrl ? <Image src={imageUrl} alt={name} fill sizes="80px" /> : null}
|
|
53
|
-
</span>
|
|
54
|
-
<span>{name}</span>
|
|
55
|
-
<span>{formatPrice(price, { currency }) as string}</span>
|
|
56
|
-
</label>
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* DESIGN ME — "frequently bought together" bundle on the product page:
|
|
62
|
-
* current product + selectable cross-sells + combined total + add-all button;
|
|
63
|
-
* items come from useProductPage().recommendations.crossSells, the feature
|
|
64
|
-
* gate from useStoreInfo().upsell.
|
|
65
|
-
*
|
|
66
|
-
* Building blocks: shadcn/ui primitives in src/components/ui (Button, Card, Badge, Input, Select, Accordion, Dialog, Skeleton, ...) + lucide-react icons are installed and ready to compose with.
|
|
67
|
-
*/
|
|
68
|
-
export function FrequentlyBoughtTogether({
|
|
69
|
-
items,
|
|
70
|
-
currentProduct,
|
|
71
|
-
className,
|
|
72
|
-
}: FrequentlyBoughtTogetherProps) {
|
|
73
|
-
// Hooks must be called unconditionally and in the same order on every
|
|
74
|
-
// render — keep all of them above any early `return null` branch.
|
|
75
|
-
const { storeInfo } = useStoreInfo();
|
|
76
|
-
const { refreshCart } = useCart();
|
|
77
|
-
const t = useTranslations('productDetail');
|
|
78
|
-
const currency = useCurrency();
|
|
79
|
-
|
|
80
|
-
//
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const
|
|
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
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
</div>
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useState } from 'react';
|
|
4
|
+
import { CdnImage as Image } from '@/ui/shared/cdn-image';
|
|
5
|
+
import type { Product, ProductRecommendation } from 'brainerce';
|
|
6
|
+
import { formatPrice } from 'brainerce';
|
|
7
|
+
import { useCart, useStoreInfo } from '@/core/providers/store-provider';
|
|
8
|
+
import { useCurrency } from '@/core/lib/use-currency';
|
|
9
|
+
import { useTranslations } from '@/core/lib/translations';
|
|
10
|
+
import { cn } from '@/core/lib/utils';
|
|
11
|
+
|
|
12
|
+
interface FrequentlyBoughtTogetherProps {
|
|
13
|
+
items: ProductRecommendation[];
|
|
14
|
+
currentProduct: Product;
|
|
15
|
+
className?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// ⛔ STORE CURRENCY THROUGHOUT, deliberately. The cross-sells are
|
|
19
|
+
// `ProductRecommendation`, a trimmed shape with no displayPrice/displayCurrency,
|
|
20
|
+
// so there is nothing to convert them to. Converting only `currentProduct`
|
|
21
|
+
// (which is a full Product and does carry FX fields) would add a euro amount to
|
|
22
|
+
// two dollar amounts and print the result as one total. One currency wins, and
|
|
23
|
+
// it has to be the one the cart actually charges.
|
|
24
|
+
function getEffectivePrice(item: { basePrice: string; salePrice?: string | null }): number {
|
|
25
|
+
const sale = item.salePrice ? parseFloat(item.salePrice) : null;
|
|
26
|
+
const base = parseFloat(item.basePrice);
|
|
27
|
+
return sale != null && sale < base ? sale : base;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function ProductThumb({
|
|
31
|
+
name,
|
|
32
|
+
imageUrl,
|
|
33
|
+
price,
|
|
34
|
+
currency,
|
|
35
|
+
checked,
|
|
36
|
+
onToggle,
|
|
37
|
+
disabled,
|
|
38
|
+
}: {
|
|
39
|
+
name: string;
|
|
40
|
+
imageUrl: string | null;
|
|
41
|
+
price: number;
|
|
42
|
+
currency: string;
|
|
43
|
+
checked: boolean;
|
|
44
|
+
onToggle?: () => void;
|
|
45
|
+
disabled?: boolean;
|
|
46
|
+
}) {
|
|
47
|
+
return (
|
|
48
|
+
<label className="inline-flex items-center gap-2">
|
|
49
|
+
{onToggle && <input type="checkbox" checked={checked} onChange={onToggle} disabled={disabled} />}
|
|
50
|
+
{/* `relative` + fixed box are layout-critical for next/image fill */}
|
|
51
|
+
<span className="relative block h-20 w-20">
|
|
52
|
+
{imageUrl ? <Image src={imageUrl} alt={name} fill sizes="80px" /> : null}
|
|
53
|
+
</span>
|
|
54
|
+
<span>{name}</span>
|
|
55
|
+
<span>{formatPrice(price, { currency }) as string}</span>
|
|
56
|
+
</label>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* DESIGN ME — "frequently bought together" bundle on the product page:
|
|
62
|
+
* current product + selectable cross-sells + combined total + add-all button;
|
|
63
|
+
* items come from useProductPage().recommendations.crossSells, the feature
|
|
64
|
+
* gate from useStoreInfo().upsell.
|
|
65
|
+
*
|
|
66
|
+
* Building blocks: shadcn/ui primitives in src/components/ui (Button, Card, Badge, Input, Select, Accordion, Dialog, Skeleton, ...) + lucide-react icons are installed and ready to compose with.
|
|
67
|
+
*/
|
|
68
|
+
export function FrequentlyBoughtTogether({
|
|
69
|
+
items,
|
|
70
|
+
currentProduct,
|
|
71
|
+
className,
|
|
72
|
+
}: FrequentlyBoughtTogetherProps) {
|
|
73
|
+
// Hooks must be called unconditionally and in the same order on every
|
|
74
|
+
// render — keep all of them above any early `return null` branch.
|
|
75
|
+
const { storeInfo } = useStoreInfo();
|
|
76
|
+
const { refreshCart } = useCart();
|
|
77
|
+
const t = useTranslations('productDetail');
|
|
78
|
+
const currency = useCurrency();
|
|
79
|
+
|
|
80
|
+
// ⛔ KITs are dropped, not priced. A recommendation is a `ProductRecommendation`
|
|
81
|
+
// — an UNRESOLVED list shape, and the recommendations endpoint does not resolve
|
|
82
|
+
// kits, so `item.basePrice` on a kit is the stored PLACEHOLDER, wrong for every
|
|
83
|
+
// kit priced SUM / SUM_MINUS_PERCENT. This block sums those numbers into one
|
|
84
|
+
// "Total" beside a button that adds them all, so a wrong figure here is a
|
|
85
|
+
// number the shopper commits to. Adding a kit by its own `productId` would in
|
|
86
|
+
// fact be correct — it is the displayed price that cannot be trusted — so a
|
|
87
|
+
// kit still sells fine from its own product page, where the API resolves it.
|
|
88
|
+
// Only show up to 3 cross-sells.
|
|
89
|
+
const crossSells = items.filter((item) => item.type !== 'KIT').slice(0, 3);
|
|
90
|
+
|
|
91
|
+
const [selected, setSelected] = useState<Set<string>>(() => new Set(crossSells.map((i) => i.id)));
|
|
92
|
+
const [adding, setAdding] = useState(false);
|
|
93
|
+
|
|
94
|
+
if (!storeInfo?.upsell?.frequentlyBoughtTogetherEnabled) return null;
|
|
95
|
+
if (crossSells.length === 0) return null;
|
|
96
|
+
|
|
97
|
+
const currentPrice = getEffectivePrice(currentProduct);
|
|
98
|
+
const currentImage = currentProduct.images?.[0];
|
|
99
|
+
const currentImageUrl = currentImage
|
|
100
|
+
? typeof currentImage === 'string'
|
|
101
|
+
? currentImage
|
|
102
|
+
: currentImage.url
|
|
103
|
+
: null;
|
|
104
|
+
|
|
105
|
+
const totalPrice = crossSells
|
|
106
|
+
.filter((item) => selected.has(item.id))
|
|
107
|
+
.reduce((sum, item) => sum + getEffectivePrice(item), currentPrice);
|
|
108
|
+
|
|
109
|
+
const toggleItem = (id: string) => {
|
|
110
|
+
setSelected((prev) => {
|
|
111
|
+
const next = new Set(prev);
|
|
112
|
+
if (next.has(id)) {
|
|
113
|
+
next.delete(id);
|
|
114
|
+
} else {
|
|
115
|
+
next.add(id);
|
|
116
|
+
}
|
|
117
|
+
return next;
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
async function handleAddAll() {
|
|
122
|
+
if (adding || selected.size === 0) return;
|
|
123
|
+
try {
|
|
124
|
+
setAdding(true);
|
|
125
|
+
const { getClient } = await import('@/core/lib/brainerce');
|
|
126
|
+
const client = getClient();
|
|
127
|
+
const selectedItems = crossSells.filter((item) => selected.has(item.id));
|
|
128
|
+
for (const item of selectedItems) {
|
|
129
|
+
await client.smartAddToCart({ productId: item.id, quantity: 1 });
|
|
130
|
+
}
|
|
131
|
+
await refreshCart();
|
|
132
|
+
} catch (err) {
|
|
133
|
+
console.error('Failed to add items to cart:', err);
|
|
134
|
+
} finally {
|
|
135
|
+
setAdding(false);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return (
|
|
140
|
+
<section className={cn(className)}>
|
|
141
|
+
<h2>{t('frequentlyBoughtTogether')}</h2>
|
|
142
|
+
|
|
143
|
+
<div className="flex flex-wrap items-center gap-3">
|
|
144
|
+
{/* Current product (always included, no checkbox) */}
|
|
145
|
+
<ProductThumb
|
|
146
|
+
name={currentProduct.name}
|
|
147
|
+
imageUrl={currentImageUrl}
|
|
148
|
+
price={currentPrice}
|
|
149
|
+
currency={currency}
|
|
150
|
+
checked={true}
|
|
151
|
+
disabled
|
|
152
|
+
/>
|
|
153
|
+
|
|
154
|
+
{crossSells.map((item) => {
|
|
155
|
+
const img = item.images?.[0];
|
|
156
|
+
const imgUrl = img ? (typeof img === 'string' ? img : img.url) : null;
|
|
157
|
+
return (
|
|
158
|
+
<div key={item.id} className="flex items-center gap-3">
|
|
159
|
+
<span aria-hidden="true">+</span>
|
|
160
|
+
<ProductThumb
|
|
161
|
+
name={item.name}
|
|
162
|
+
imageUrl={imgUrl}
|
|
163
|
+
price={getEffectivePrice(item)}
|
|
164
|
+
currency={currency}
|
|
165
|
+
checked={selected.has(item.id)}
|
|
166
|
+
onToggle={() => toggleItem(item.id)}
|
|
167
|
+
/>
|
|
168
|
+
</div>
|
|
169
|
+
);
|
|
170
|
+
})}
|
|
171
|
+
</div>
|
|
172
|
+
|
|
173
|
+
{/* Total + Add button */}
|
|
174
|
+
<div className="flex flex-wrap items-center gap-4">
|
|
175
|
+
<span>{t('totalPrice', { price: formatPrice(totalPrice, { currency }) as string })}</span>
|
|
176
|
+
<button type="button" onClick={handleAddAll} disabled={adding || selected.size === 0}>
|
|
177
|
+
{adding ? t('addingAll') : t('addSelectedToCart')}
|
|
178
|
+
</button>
|
|
179
|
+
</div>
|
|
180
|
+
</section>
|
|
181
|
+
);
|
|
182
|
+
}
|