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,202 +1,210 @@
|
|
|
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
|
-
import { IconPlus, IconBag } from '@/ui/shared/icons';
|
|
12
|
-
|
|
13
|
-
interface FrequentlyBoughtTogetherProps {
|
|
14
|
-
items: ProductRecommendation[];
|
|
15
|
-
currentProduct: Product;
|
|
16
|
-
className?: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// ⛔ STORE CURRENCY THROUGHOUT, deliberately. The cross-sells are
|
|
20
|
-
// `ProductRecommendation`, a trimmed shape with no displayPrice/displayCurrency,
|
|
21
|
-
// so there is nothing to convert them to. Converting only `currentProduct`
|
|
22
|
-
// (which is a full Product and does carry FX fields) would add a euro amount to
|
|
23
|
-
// two dollar amounts and print the result as one total. One currency wins, and
|
|
24
|
-
// it has to be the one the cart actually charges.
|
|
25
|
-
function getEffectivePrice(item: { basePrice: string; salePrice?: string | null }): number {
|
|
26
|
-
const sale = item.salePrice ? parseFloat(item.salePrice) : null;
|
|
27
|
-
const base = parseFloat(item.basePrice);
|
|
28
|
-
return sale != null && sale < base ? sale : base;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function ProductThumb({
|
|
32
|
-
name,
|
|
33
|
-
imageUrl,
|
|
34
|
-
price,
|
|
35
|
-
currency,
|
|
36
|
-
checked,
|
|
37
|
-
onToggle,
|
|
38
|
-
disabled,
|
|
39
|
-
}: {
|
|
40
|
-
name: string;
|
|
41
|
-
imageUrl: string | null;
|
|
42
|
-
price: number;
|
|
43
|
-
currency: string;
|
|
44
|
-
checked: boolean;
|
|
45
|
-
onToggle?: () => void;
|
|
46
|
-
disabled?: boolean;
|
|
47
|
-
}) {
|
|
48
|
-
return (
|
|
49
|
-
<label
|
|
50
|
-
className={cn(
|
|
51
|
-
'flex w-40 cursor-pointer flex-col gap-2 rounded-xl border bg-background p-3 transition-colors',
|
|
52
|
-
checked ? 'border-primary' : 'border-border',
|
|
53
|
-
disabled && 'cursor-default'
|
|
54
|
-
)}
|
|
55
|
-
>
|
|
56
|
-
{/* `relative` + fixed box are layout-critical for next/image fill */}
|
|
57
|
-
<span className="relative block aspect-square overflow-hidden rounded-lg bg-secondary">
|
|
58
|
-
{imageUrl ? <Image src={imageUrl} alt={name} fill sizes="160px" className="object-cover" /> : null}
|
|
59
|
-
{onToggle && (
|
|
60
|
-
<input
|
|
61
|
-
type="checkbox"
|
|
62
|
-
checked={checked}
|
|
63
|
-
onChange={onToggle}
|
|
64
|
-
disabled={disabled}
|
|
65
|
-
className="absolute end-2 top-2 h-[18px] w-[18px] accent-primary"
|
|
66
|
-
aria-label={name}
|
|
67
|
-
/>
|
|
68
|
-
)}
|
|
69
|
-
</span>
|
|
70
|
-
<span className="line-clamp-2 text-xs font-semibold leading-snug text-foreground">
|
|
71
|
-
{name}
|
|
72
|
-
</span>
|
|
73
|
-
<span className="text-sm font-semibold text-foreground">
|
|
74
|
-
{formatPrice(price, { currency }) as string}
|
|
75
|
-
</span>
|
|
76
|
-
</label>
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* "Frequently bought together" bundle on the product page: current product +
|
|
82
|
-
* selectable cross-sells + combined total + add-all button; items come from
|
|
83
|
-
* useProductPage().recommendations.crossSells, the feature gate from
|
|
84
|
-
* useStoreInfo().upsell.
|
|
85
|
-
*/
|
|
86
|
-
export function FrequentlyBoughtTogether({
|
|
87
|
-
items,
|
|
88
|
-
currentProduct,
|
|
89
|
-
className,
|
|
90
|
-
}: FrequentlyBoughtTogetherProps) {
|
|
91
|
-
// Hooks must be called unconditionally and in the same order on every
|
|
92
|
-
// render — keep all of them above any early `return null` branch.
|
|
93
|
-
const { storeInfo } = useStoreInfo();
|
|
94
|
-
const { refreshCart } = useCart();
|
|
95
|
-
const t = useTranslations('productDetail');
|
|
96
|
-
const currency = useCurrency();
|
|
97
|
-
|
|
98
|
-
//
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const
|
|
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
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
>
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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
|
+
import { IconPlus, IconBag } from '@/ui/shared/icons';
|
|
12
|
+
|
|
13
|
+
interface FrequentlyBoughtTogetherProps {
|
|
14
|
+
items: ProductRecommendation[];
|
|
15
|
+
currentProduct: Product;
|
|
16
|
+
className?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// ⛔ STORE CURRENCY THROUGHOUT, deliberately. The cross-sells are
|
|
20
|
+
// `ProductRecommendation`, a trimmed shape with no displayPrice/displayCurrency,
|
|
21
|
+
// so there is nothing to convert them to. Converting only `currentProduct`
|
|
22
|
+
// (which is a full Product and does carry FX fields) would add a euro amount to
|
|
23
|
+
// two dollar amounts and print the result as one total. One currency wins, and
|
|
24
|
+
// it has to be the one the cart actually charges.
|
|
25
|
+
function getEffectivePrice(item: { basePrice: string; salePrice?: string | null }): number {
|
|
26
|
+
const sale = item.salePrice ? parseFloat(item.salePrice) : null;
|
|
27
|
+
const base = parseFloat(item.basePrice);
|
|
28
|
+
return sale != null && sale < base ? sale : base;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function ProductThumb({
|
|
32
|
+
name,
|
|
33
|
+
imageUrl,
|
|
34
|
+
price,
|
|
35
|
+
currency,
|
|
36
|
+
checked,
|
|
37
|
+
onToggle,
|
|
38
|
+
disabled,
|
|
39
|
+
}: {
|
|
40
|
+
name: string;
|
|
41
|
+
imageUrl: string | null;
|
|
42
|
+
price: number;
|
|
43
|
+
currency: string;
|
|
44
|
+
checked: boolean;
|
|
45
|
+
onToggle?: () => void;
|
|
46
|
+
disabled?: boolean;
|
|
47
|
+
}) {
|
|
48
|
+
return (
|
|
49
|
+
<label
|
|
50
|
+
className={cn(
|
|
51
|
+
'flex w-40 cursor-pointer flex-col gap-2 rounded-xl border bg-background p-3 transition-colors',
|
|
52
|
+
checked ? 'border-primary' : 'border-border',
|
|
53
|
+
disabled && 'cursor-default'
|
|
54
|
+
)}
|
|
55
|
+
>
|
|
56
|
+
{/* `relative` + fixed box are layout-critical for next/image fill */}
|
|
57
|
+
<span className="relative block aspect-square overflow-hidden rounded-lg bg-secondary">
|
|
58
|
+
{imageUrl ? <Image src={imageUrl} alt={name} fill sizes="160px" className="object-cover" /> : null}
|
|
59
|
+
{onToggle && (
|
|
60
|
+
<input
|
|
61
|
+
type="checkbox"
|
|
62
|
+
checked={checked}
|
|
63
|
+
onChange={onToggle}
|
|
64
|
+
disabled={disabled}
|
|
65
|
+
className="absolute end-2 top-2 h-[18px] w-[18px] accent-primary"
|
|
66
|
+
aria-label={name}
|
|
67
|
+
/>
|
|
68
|
+
)}
|
|
69
|
+
</span>
|
|
70
|
+
<span className="line-clamp-2 text-xs font-semibold leading-snug text-foreground">
|
|
71
|
+
{name}
|
|
72
|
+
</span>
|
|
73
|
+
<span className="text-sm font-semibold text-foreground">
|
|
74
|
+
{formatPrice(price, { currency }) as string}
|
|
75
|
+
</span>
|
|
76
|
+
</label>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* "Frequently bought together" bundle on the product page: current product +
|
|
82
|
+
* selectable cross-sells + combined total + add-all button; items come from
|
|
83
|
+
* useProductPage().recommendations.crossSells, the feature gate from
|
|
84
|
+
* useStoreInfo().upsell.
|
|
85
|
+
*/
|
|
86
|
+
export function FrequentlyBoughtTogether({
|
|
87
|
+
items,
|
|
88
|
+
currentProduct,
|
|
89
|
+
className,
|
|
90
|
+
}: FrequentlyBoughtTogetherProps) {
|
|
91
|
+
// Hooks must be called unconditionally and in the same order on every
|
|
92
|
+
// render — keep all of them above any early `return null` branch.
|
|
93
|
+
const { storeInfo } = useStoreInfo();
|
|
94
|
+
const { refreshCart } = useCart();
|
|
95
|
+
const t = useTranslations('productDetail');
|
|
96
|
+
const currency = useCurrency();
|
|
97
|
+
|
|
98
|
+
// ⛔ KITs are dropped, not priced. A recommendation is a `ProductRecommendation`
|
|
99
|
+
// — an UNRESOLVED list shape, and the recommendations endpoint does not resolve
|
|
100
|
+
// kits, so `item.basePrice` on a kit is the stored PLACEHOLDER, wrong for every
|
|
101
|
+
// kit priced SUM / SUM_MINUS_PERCENT. This block sums those numbers into one
|
|
102
|
+
// "Total" beside a button that adds them all, so a wrong figure here is a
|
|
103
|
+
// number the shopper commits to. Adding a kit by its own `productId` would in
|
|
104
|
+
// fact be correct — it is the displayed price that cannot be trusted — so a
|
|
105
|
+
// kit still sells fine from its own product page, where the API resolves it.
|
|
106
|
+
// Only show up to 3 cross-sells.
|
|
107
|
+
const crossSells = items.filter((item) => item.type !== 'KIT').slice(0, 3);
|
|
108
|
+
|
|
109
|
+
const [selected, setSelected] = useState<Set<string>>(() => new Set(crossSells.map((i) => i.id)));
|
|
110
|
+
const [adding, setAdding] = useState(false);
|
|
111
|
+
|
|
112
|
+
if (!storeInfo?.upsell?.frequentlyBoughtTogetherEnabled) return null;
|
|
113
|
+
if (crossSells.length === 0) return null;
|
|
114
|
+
|
|
115
|
+
const currentPrice = getEffectivePrice(currentProduct);
|
|
116
|
+
const currentImage = currentProduct.images?.[0];
|
|
117
|
+
const currentImageUrl = currentImage
|
|
118
|
+
? typeof currentImage === 'string'
|
|
119
|
+
? currentImage
|
|
120
|
+
: currentImage.url
|
|
121
|
+
: null;
|
|
122
|
+
|
|
123
|
+
const totalPrice = crossSells
|
|
124
|
+
.filter((item) => selected.has(item.id))
|
|
125
|
+
.reduce((sum, item) => sum + getEffectivePrice(item), currentPrice);
|
|
126
|
+
|
|
127
|
+
const toggleItem = (id: string) => {
|
|
128
|
+
setSelected((prev) => {
|
|
129
|
+
const next = new Set(prev);
|
|
130
|
+
if (next.has(id)) {
|
|
131
|
+
next.delete(id);
|
|
132
|
+
} else {
|
|
133
|
+
next.add(id);
|
|
134
|
+
}
|
|
135
|
+
return next;
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
async function handleAddAll() {
|
|
140
|
+
if (adding || selected.size === 0) return;
|
|
141
|
+
try {
|
|
142
|
+
setAdding(true);
|
|
143
|
+
const { getClient } = await import('@/core/lib/brainerce');
|
|
144
|
+
const client = getClient();
|
|
145
|
+
const selectedItems = crossSells.filter((item) => selected.has(item.id));
|
|
146
|
+
for (const item of selectedItems) {
|
|
147
|
+
await client.smartAddToCart({ productId: item.id, quantity: 1 });
|
|
148
|
+
}
|
|
149
|
+
await refreshCart();
|
|
150
|
+
} catch (err) {
|
|
151
|
+
console.error('Failed to add items to cart:', err);
|
|
152
|
+
} finally {
|
|
153
|
+
setAdding(false);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return (
|
|
158
|
+
<section className={cn('rounded-2xl border bg-secondary/50 p-6 sm:p-8', className)}>
|
|
159
|
+
<h2 className="mb-6 text-2xl">{t('frequentlyBoughtTogether')}</h2>
|
|
160
|
+
|
|
161
|
+
<div className="flex flex-wrap items-center gap-3">
|
|
162
|
+
{/* Current product (always included, no checkbox) */}
|
|
163
|
+
<ProductThumb
|
|
164
|
+
name={currentProduct.name}
|
|
165
|
+
imageUrl={currentImageUrl}
|
|
166
|
+
price={currentPrice}
|
|
167
|
+
currency={currency}
|
|
168
|
+
checked={true}
|
|
169
|
+
disabled
|
|
170
|
+
/>
|
|
171
|
+
|
|
172
|
+
{crossSells.map((item) => {
|
|
173
|
+
const img = item.images?.[0];
|
|
174
|
+
const imgUrl = img ? (typeof img === 'string' ? img : img.url) : null;
|
|
175
|
+
return (
|
|
176
|
+
<div key={item.id} className="flex items-center gap-3">
|
|
177
|
+
<span aria-hidden="true" className="text-muted-foreground">
|
|
178
|
+
<IconPlus size={20} />
|
|
179
|
+
</span>
|
|
180
|
+
<ProductThumb
|
|
181
|
+
name={item.name}
|
|
182
|
+
imageUrl={imgUrl}
|
|
183
|
+
price={getEffectivePrice(item)}
|
|
184
|
+
currency={currency}
|
|
185
|
+
checked={selected.has(item.id)}
|
|
186
|
+
onToggle={() => toggleItem(item.id)}
|
|
187
|
+
/>
|
|
188
|
+
</div>
|
|
189
|
+
);
|
|
190
|
+
})}
|
|
191
|
+
</div>
|
|
192
|
+
|
|
193
|
+
{/* Total + Add button */}
|
|
194
|
+
<div className="mt-6 flex flex-wrap items-center gap-4 border-t pt-5">
|
|
195
|
+
<span className="text-lg font-semibold text-foreground">
|
|
196
|
+
{t('totalPrice').replace('{price}', formatPrice(totalPrice, { currency }) as string)}
|
|
197
|
+
</span>
|
|
198
|
+
<button
|
|
199
|
+
type="button"
|
|
200
|
+
onClick={handleAddAll}
|
|
201
|
+
disabled={adding || selected.size === 0}
|
|
202
|
+
className="btn-primary btn-md"
|
|
203
|
+
>
|
|
204
|
+
<IconBag size={18} />
|
|
205
|
+
{adding ? t('addingAll') : t('addSelectedToCart')}
|
|
206
|
+
</button>
|
|
207
|
+
</div>
|
|
208
|
+
</section>
|
|
209
|
+
);
|
|
210
|
+
}
|