create-brainerce-store 1.72.0 → 1.74.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 +37 -4
- package/messages/en.json +30 -1
- package/messages/he.json +30 -1
- package/package.json +1 -1
- package/templates/nextjs/base/AGENTS.md.ejs +63 -16
- package/templates/nextjs/base/AI-GUIDE.md +17 -4
- package/templates/nextjs/base/CLAUDE.md.ejs +67 -20
- package/templates/nextjs/base/TRANSLATIONS.md +207 -207
- package/templates/nextjs/base/src/app/account/page.tsx +168 -122
- package/templates/nextjs/base/src/app/category/[slug]/page.tsx +10 -1
- package/templates/nextjs/base/src/app/checkout/page.tsx +1179 -1074
- package/templates/nextjs/base/src/app/layout.tsx.ejs +48 -4
- package/templates/nextjs/base/src/app/order-confirmation/page.tsx +98 -3
- package/templates/nextjs/base/src/app/products/[slug]/page.tsx +14 -2
- package/templates/nextjs/base/src/components/account/loyalty-panel.tsx +226 -0
- package/templates/nextjs/base/src/components/account/order-history.tsx +422 -371
- package/templates/nextjs/base/src/components/account/order-status-timeline.tsx +85 -85
- package/templates/nextjs/base/src/components/account/saved-payment-methods.tsx +104 -0
- package/templates/nextjs/base/src/components/checkout/checkout-form.tsx +618 -612
- package/templates/nextjs/base/src/components/checkout/custom-fields-step.tsx +354 -306
- package/templates/nextjs/base/src/components/checkout/tax-display.tsx +141 -74
- package/templates/nextjs/base/src/core/hooks/use-cart-page.ts +218 -127
- package/templates/nextjs/base/src/core/hooks/use-home-data.ts +63 -51
- package/templates/nextjs/base/src/core/hooks/use-product-listing.ts +379 -295
- package/templates/nextjs/base/src/core/hooks/use-product-page.ts +33 -1
- package/templates/nextjs/base/src/core/lib/capabilities.ts +65 -0
- package/templates/nextjs/base/src/core/lib/display-price.ts +139 -0
- package/templates/nextjs/base/src/core/lib/region.server.ts +112 -0
- package/templates/nextjs/base/src/core/lib/region.ts +112 -0
- package/templates/nextjs/base/src/core/lib/store-info.ts +19 -0
- package/templates/nextjs/base/src/core/providers/store-provider.tsx.ejs +423 -243
- package/templates/nextjs/base/src/ui/cart/cart-item.tsx +174 -164
- package/templates/nextjs/base/src/ui/cart/cart-summary.tsx +109 -110
- package/templates/nextjs/base/src/ui/cart/cart-view.tsx +219 -176
- package/templates/nextjs/base/src/ui/cart/gift-card-input.tsx +191 -0
- package/templates/nextjs/base/src/ui/cart/reservation-countdown.tsx +137 -137
- package/templates/nextjs/base/src/ui/cart/tax-estimate-line.tsx +108 -0
- package/templates/nextjs/base/src/ui/home/home-client.tsx +27 -29
- package/templates/nextjs/base/src/ui/layout/header-search.tsx +249 -0
- package/templates/nextjs/base/src/ui/layout/region-switcher.tsx +59 -0
- package/templates/nextjs/base/src/ui/layout/site-header.tsx.ejs +171 -152
- package/templates/nextjs/base/src/ui/product/frequently-bought-together.tsx +197 -191
- package/templates/nextjs/base/src/ui/product/product-card.tsx +25 -52
- package/templates/nextjs/base/src/ui/product/product-client-section.tsx +12 -3
- package/templates/nextjs/base/src/ui/product/product-listing.tsx +460 -446
- package/templates/nextjs/base/src/ui/product/recommendation-section.tsx +108 -102
- package/templates/nextjs/base/src/ui/product/review-form.tsx +33 -11
- package/templates/nextjs/base/src/ui/product/stock-badge.tsx +15 -3
- package/templates/nextjs/designs/atelier/app-overlay/layout.tsx.ejs +48 -4
- package/templates/nextjs/designs/atelier/messages-patch/en.json +21 -25
- package/templates/nextjs/designs/atelier/messages-patch/he.json +21 -25
- package/templates/nextjs/designs/atelier/ui/cart/cart-drawer.tsx +177 -181
- package/templates/nextjs/designs/atelier/ui/cart/cart-item.tsx +168 -158
- package/templates/nextjs/designs/atelier/ui/cart/cart-summary.tsx +106 -107
- package/templates/nextjs/designs/atelier/ui/cart/cart-view.tsx +229 -184
- package/templates/nextjs/designs/atelier/ui/cart/reservation-countdown.tsx +131 -131
- package/templates/nextjs/designs/atelier/ui/cart/tax-estimate-line.tsx +108 -0
- package/templates/nextjs/designs/atelier/ui/home/benefits-band.tsx +10 -1
- package/templates/nextjs/designs/atelier/ui/home/hero-section.tsx +178 -174
- package/templates/nextjs/designs/atelier/ui/home/home-client.tsx +4 -6
- package/templates/nextjs/designs/atelier/ui/home/newsletter-section.tsx +150 -78
- package/templates/nextjs/designs/atelier/ui/home/testimonials-band.tsx +79 -46
- package/templates/nextjs/designs/atelier/ui/layout/header-search.tsx +264 -0
- package/templates/nextjs/designs/atelier/ui/layout/region-switcher.tsx +44 -0
- package/templates/nextjs/designs/atelier/ui/layout/site-header.tsx.ejs +5 -0
- package/templates/nextjs/designs/atelier/ui/product/frequently-bought-together.tsx +202 -196
- package/templates/nextjs/designs/atelier/ui/product/product-card.tsx +27 -52
- package/templates/nextjs/designs/atelier/ui/product/product-client-section.tsx +12 -3
- package/templates/nextjs/designs/atelier/ui/product/product-listing.tsx +332 -260
- package/templates/nextjs/designs/atelier/ui/product/recommendation-section.tsx +101 -95
- package/templates/nextjs/designs/atelier/ui/product/review-form.tsx +30 -10
- package/templates/nextjs/designs/atelier/ui/product/stock-badge.tsx +17 -2
- package/templates/nextjs/designs/atelier/ui/shared/icons.tsx +9 -0
- package/templates/nextjs/ui-canvas/cart/cart-item.tsx +137 -137
- package/templates/nextjs/ui-canvas/cart/cart-summary.tsx +98 -99
- package/templates/nextjs/ui-canvas/cart/cart-view.tsx +140 -140
- package/templates/nextjs/ui-canvas/cart/gift-card-input.tsx +142 -0
- package/templates/nextjs/ui-canvas/cart/reservation-countdown.tsx +124 -124
- package/templates/nextjs/ui-canvas/cart/tax-estimate-line.tsx +104 -0
- package/templates/nextjs/ui-canvas/home/home-client.tsx +24 -26
- package/templates/nextjs/ui-canvas/layout/region-switcher.tsx +51 -0
- package/templates/nextjs/ui-canvas/layout/site-header.tsx.ejs +3 -0
- package/templates/nextjs/ui-canvas/product/frequently-bought-together.tsx +174 -168
- package/templates/nextjs/ui-canvas/product/product-card.tsx +9 -51
- package/templates/nextjs/ui-canvas/product/product-client-section.tsx +5 -3
- package/templates/nextjs/ui-canvas/product/recommendation-section.tsx +105 -99
- package/templates/nextjs/ui-canvas/product/review-form.tsx +9 -1
- package/templates/nextjs/ui-canvas/product/stock-badge.tsx +17 -2
|
@@ -1,164 +1,174 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import { useState } from 'react';
|
|
4
|
-
import { Image as ImageIcon } from 'lucide-react';
|
|
5
|
-
import { CdnImage as Image } from '@/ui/shared/cdn-image';
|
|
6
|
-
import type { CartItem as CartItemType } from 'brainerce';
|
|
7
|
-
import { getCartItemImage, formatPrice } from 'brainerce';
|
|
8
|
-
import { getClient } from '@/core/lib/brainerce';
|
|
9
|
-
import { useTranslations } from '@/core/lib/translations';
|
|
10
|
-
import { useCurrency } from '@/core/lib/use-currency';
|
|
11
|
-
import { LoadingSpinner } from '@/ui/shared/loading-spinner';
|
|
12
|
-
import { cn } from '@/core/lib/utils';
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const [
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
//
|
|
35
|
-
//
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
const
|
|
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
|
-
|
|
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
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useState } from 'react';
|
|
4
|
+
import { Image as ImageIcon } from 'lucide-react';
|
|
5
|
+
import { CdnImage as Image } from '@/ui/shared/cdn-image';
|
|
6
|
+
import type { CartItem as CartItemType } from 'brainerce';
|
|
7
|
+
import { getCartItemImage, formatPrice } from 'brainerce';
|
|
8
|
+
import { getClient } from '@/core/lib/brainerce';
|
|
9
|
+
import { useTranslations } from '@/core/lib/translations';
|
|
10
|
+
import { useCurrency } from '@/core/lib/use-currency';
|
|
11
|
+
import { LoadingSpinner } from '@/ui/shared/loading-spinner';
|
|
12
|
+
import { cn } from '@/core/lib/utils';
|
|
13
|
+
import { OrderCustomizations } from '@/components/account/order-customizations';
|
|
14
|
+
|
|
15
|
+
interface CartItemProps {
|
|
16
|
+
item: CartItemType;
|
|
17
|
+
onUpdate: () => void;
|
|
18
|
+
className?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function CartItem({ item, onUpdate, className }: CartItemProps) {
|
|
22
|
+
const t = useTranslations('common');
|
|
23
|
+
const td = useTranslations('productDetail');
|
|
24
|
+
const currency = useCurrency();
|
|
25
|
+
const [updating, setUpdating] = useState(false);
|
|
26
|
+
const [removing, setRemoving] = useState(false);
|
|
27
|
+
|
|
28
|
+
const productName = item.product.name;
|
|
29
|
+
const imageUrl = getCartItemImage(item);
|
|
30
|
+
const variantName = item.variant?.name;
|
|
31
|
+
const unitPrice = parseFloat(item.unitPrice);
|
|
32
|
+
const lineTotal = unitPrice * item.quantity;
|
|
33
|
+
|
|
34
|
+
// The server decides purchasability, per line. `isAvailable === false` means
|
|
35
|
+
// this line blocks checkout until it is removed, whether the stock ran out
|
|
36
|
+
// (a released reservation is the common cause) or the product/variant was
|
|
37
|
+
// withdrawn from sale. `unavailableReason` separates the two for the label.
|
|
38
|
+
const isUnavailable = item.isAvailable === false;
|
|
39
|
+
const unavailableLabel = isUnavailable
|
|
40
|
+
? item.unavailableReason === 'OUT_OF_STOCK'
|
|
41
|
+
? td('outOfStock')
|
|
42
|
+
: td('unavailable')
|
|
43
|
+
: null;
|
|
44
|
+
|
|
45
|
+
async function handleQuantityChange(newQuantity: number) {
|
|
46
|
+
if (newQuantity < 1 || updating) return;
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
setUpdating(true);
|
|
50
|
+
const client = getClient();
|
|
51
|
+
await client.smartUpdateCartItem(item.productId, newQuantity, item.variantId || undefined);
|
|
52
|
+
onUpdate();
|
|
53
|
+
} catch (err) {
|
|
54
|
+
console.error('Failed to update quantity:', err);
|
|
55
|
+
} finally {
|
|
56
|
+
setUpdating(false);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function handleRemove() {
|
|
61
|
+
if (removing) return;
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
setRemoving(true);
|
|
65
|
+
const client = getClient();
|
|
66
|
+
await client.smartRemoveFromCart(item.productId, item.variantId || undefined);
|
|
67
|
+
onUpdate();
|
|
68
|
+
} catch (err) {
|
|
69
|
+
console.error('Failed to remove item:', err);
|
|
70
|
+
} finally {
|
|
71
|
+
setRemoving(false);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return (
|
|
76
|
+
<div
|
|
77
|
+
className={cn(
|
|
78
|
+
'border-border flex gap-4 border-b py-4 last:border-0',
|
|
79
|
+
(updating || removing) && 'opacity-60',
|
|
80
|
+
className
|
|
81
|
+
)}
|
|
82
|
+
>
|
|
83
|
+
{/* Image */}
|
|
84
|
+
<div className="bg-muted relative h-20 w-20 flex-shrink-0 overflow-hidden rounded">
|
|
85
|
+
{imageUrl ? (
|
|
86
|
+
<Image src={imageUrl} alt={productName} fill sizes="80px" className="object-cover" />
|
|
87
|
+
) : (
|
|
88
|
+
<div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
|
|
89
|
+
<ImageIcon className="h-8 w-8" strokeWidth={1.5} aria-hidden="true" />
|
|
90
|
+
</div>
|
|
91
|
+
)}
|
|
92
|
+
</div>
|
|
93
|
+
|
|
94
|
+
{/* Details */}
|
|
95
|
+
<div className="min-w-0 flex-1">
|
|
96
|
+
<h3 className="text-foreground truncate text-sm font-medium">{productName}</h3>
|
|
97
|
+
|
|
98
|
+
{/* Availability badge. This line blocks checkout while it shows. */}
|
|
99
|
+
{unavailableLabel && (
|
|
100
|
+
<span className="bg-destructive/10 text-destructive mt-1 inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium">
|
|
101
|
+
{unavailableLabel}
|
|
102
|
+
</span>
|
|
103
|
+
)}
|
|
104
|
+
|
|
105
|
+
{/* Variant name */}
|
|
106
|
+
{variantName && <p className="text-muted-foreground mt-1 text-xs">{variantName}</p>}
|
|
107
|
+
|
|
108
|
+
{/* What the buyer typed / uploaded / picked, keyed by the field's own
|
|
109
|
+
`key` and carrying the merchant's label. The SDK resolves this on
|
|
110
|
+
the cart line itself (`CartItem.customizations`, same shape as
|
|
111
|
+
`OrderItem.customizations`) so no product fetch is needed for the
|
|
112
|
+
labels. Present only when the line has at least one value set, and
|
|
113
|
+
the component returns null on an empty map, so a plain product
|
|
114
|
+
renders exactly as before. */}
|
|
115
|
+
{item.customizations && <OrderCustomizations customizations={item.customizations} />}
|
|
116
|
+
|
|
117
|
+
{/* Unit price */}
|
|
118
|
+
<p className="text-muted-foreground mt-1 text-sm">
|
|
119
|
+
{formatPrice(unitPrice, { currency }) as string}
|
|
120
|
+
</p>
|
|
121
|
+
|
|
122
|
+
{/* Quantity controls */}
|
|
123
|
+
<div className="mt-2 flex items-center gap-3">
|
|
124
|
+
<div className="border-border flex items-center rounded border">
|
|
125
|
+
<button
|
|
126
|
+
type="button"
|
|
127
|
+
onClick={() => handleQuantityChange(item.quantity - 1)}
|
|
128
|
+
disabled={updating || item.quantity <= 1}
|
|
129
|
+
className="text-foreground hover:bg-muted px-2 py-1 text-sm transition-colors disabled:cursor-not-allowed disabled:opacity-40"
|
|
130
|
+
aria-label={td('decreaseQuantity')}
|
|
131
|
+
>
|
|
132
|
+
-
|
|
133
|
+
</button>
|
|
134
|
+
<span className="text-foreground min-w-[2.5rem] px-3 py-1 text-center text-sm font-medium">
|
|
135
|
+
{updating ? (
|
|
136
|
+
<LoadingSpinner
|
|
137
|
+
size="sm"
|
|
138
|
+
className="border-muted-foreground/30 border-t-foreground mx-auto"
|
|
139
|
+
/>
|
|
140
|
+
) : (
|
|
141
|
+
item.quantity
|
|
142
|
+
)}
|
|
143
|
+
</span>
|
|
144
|
+
<button
|
|
145
|
+
type="button"
|
|
146
|
+
onClick={() => handleQuantityChange(item.quantity + 1)}
|
|
147
|
+
disabled={updating || isUnavailable}
|
|
148
|
+
className="text-foreground hover:bg-muted px-2 py-1 text-sm transition-colors disabled:cursor-not-allowed disabled:opacity-40"
|
|
149
|
+
aria-label={td('increaseQuantity')}
|
|
150
|
+
>
|
|
151
|
+
+
|
|
152
|
+
</button>
|
|
153
|
+
</div>
|
|
154
|
+
|
|
155
|
+
<button
|
|
156
|
+
type="button"
|
|
157
|
+
onClick={handleRemove}
|
|
158
|
+
disabled={removing}
|
|
159
|
+
className="text-destructive hover:text-destructive/80 text-xs transition-colors disabled:opacity-40"
|
|
160
|
+
>
|
|
161
|
+
{removing ? t('removing') : t('remove')}
|
|
162
|
+
</button>
|
|
163
|
+
</div>
|
|
164
|
+
</div>
|
|
165
|
+
|
|
166
|
+
{/* Line total */}
|
|
167
|
+
<div className="flex-shrink-0 text-end">
|
|
168
|
+
<span className="text-foreground text-sm font-medium">
|
|
169
|
+
{formatPrice(lineTotal, { currency }) as string}
|
|
170
|
+
</span>
|
|
171
|
+
</div>
|
|
172
|
+
</div>
|
|
173
|
+
);
|
|
174
|
+
}
|
|
@@ -1,110 +1,109 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import { formatPrice } from 'brainerce';
|
|
4
|
-
import { useTranslations } from '@/core/lib/translations';
|
|
5
|
-
import { useCart } from '@/core/providers/store-provider';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
<span className="text-foreground
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
{
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
<span className="text-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
<span className="text-
|
|
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
|
-
<span className="text-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
{
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
<span className="text-foreground
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
<
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
<span className="text-foreground font-semibold">
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { formatPrice } from 'brainerce';
|
|
4
|
+
import { useTranslations } from '@/core/lib/translations';
|
|
5
|
+
import { useCart } from '@/core/providers/store-provider';
|
|
6
|
+
import { TaxEstimateLine } from '@/ui/cart/tax-estimate-line';
|
|
7
|
+
import { useCurrency } from '@/core/lib/use-currency';
|
|
8
|
+
import { Separator } from '@/components/ui/separator';
|
|
9
|
+
import { cn } from '@/core/lib/utils';
|
|
10
|
+
|
|
11
|
+
interface CartSummaryProps {
|
|
12
|
+
className?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function CartSummary({ className }: CartSummaryProps) {
|
|
16
|
+
const t = useTranslations('cart');
|
|
17
|
+
const tc = useTranslations('common');
|
|
18
|
+
const { totals, cart } = useCart();
|
|
19
|
+
const currency = useCurrency();
|
|
20
|
+
|
|
21
|
+
const rules = cart?.appliedDiscounts;
|
|
22
|
+
const ruleAmt = cart?.ruleDiscountAmount ? parseFloat(cart.ruleDiscountAmount) : 0;
|
|
23
|
+
const couponAmt = totals.discount - ruleAmt;
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<div className={cn('space-y-3', className)}>
|
|
27
|
+
<h3 className="text-foreground text-lg font-semibold">{t('orderSummary')}</h3>
|
|
28
|
+
|
|
29
|
+
<div className="space-y-2 text-sm">
|
|
30
|
+
{/* Subtotal */}
|
|
31
|
+
<div className="flex items-center justify-between">
|
|
32
|
+
<span className="text-muted-foreground">{tc('subtotal')}</span>
|
|
33
|
+
<span className="text-foreground font-medium">
|
|
34
|
+
{formatPrice(totals.subtotal, { currency }) as string}
|
|
35
|
+
</span>
|
|
36
|
+
</div>
|
|
37
|
+
|
|
38
|
+
{/* Rule discounts - show each rule by name */}
|
|
39
|
+
{rules && rules.length > 0
|
|
40
|
+
? rules.map((rule) => (
|
|
41
|
+
<div key={rule.ruleId} className="flex items-center justify-between">
|
|
42
|
+
<span className="text-muted-foreground">{rule.ruleName}</span>
|
|
43
|
+
<span className="text-destructive font-medium">
|
|
44
|
+
-{formatPrice(parseFloat(rule.discountAmount), { currency }) as string}
|
|
45
|
+
</span>
|
|
46
|
+
</div>
|
|
47
|
+
))
|
|
48
|
+
: ruleAmt > 0 && (
|
|
49
|
+
<div className="flex items-center justify-between">
|
|
50
|
+
<span className="text-muted-foreground">{tc('generalDiscount')}</span>
|
|
51
|
+
<span className="text-destructive font-medium">
|
|
52
|
+
-{formatPrice(ruleAmt, { currency }) as string}
|
|
53
|
+
</span>
|
|
54
|
+
</div>
|
|
55
|
+
)}
|
|
56
|
+
|
|
57
|
+
{/* Coupon discount */}
|
|
58
|
+
{cart?.couponCode && couponAmt > 0 && (
|
|
59
|
+
<div className="flex items-center justify-between">
|
|
60
|
+
<span className="text-muted-foreground">
|
|
61
|
+
{tc('couponDiscount')} ({cart.couponCode})
|
|
62
|
+
</span>
|
|
63
|
+
<span className="text-destructive font-medium">
|
|
64
|
+
-{formatPrice(couponAmt, { currency }) as string}
|
|
65
|
+
</span>
|
|
66
|
+
</div>
|
|
67
|
+
)}
|
|
68
|
+
|
|
69
|
+
{/* Fallback: generic discount when no breakdown available */}
|
|
70
|
+
{totals.discount > 0 &&
|
|
71
|
+
ruleAmt <= 0 &&
|
|
72
|
+
!cart?.couponCode &&
|
|
73
|
+
(!rules || rules.length === 0) && (
|
|
74
|
+
<div className="flex items-center justify-between">
|
|
75
|
+
<span className="text-muted-foreground">{tc('discount')}</span>
|
|
76
|
+
<span className="text-destructive font-medium">
|
|
77
|
+
-{formatPrice(totals.discount, { currency }) as string}
|
|
78
|
+
</span>
|
|
79
|
+
</div>
|
|
80
|
+
)}
|
|
81
|
+
|
|
82
|
+
{/* Shipping */}
|
|
83
|
+
{totals.shipping > 0 && (
|
|
84
|
+
<div className="flex items-center justify-between">
|
|
85
|
+
<span className="text-muted-foreground">{tc('shipping')}</span>
|
|
86
|
+
<span className="text-foreground font-medium">
|
|
87
|
+
{formatPrice(totals.shipping, { currency }) as string}
|
|
88
|
+
</span>
|
|
89
|
+
</div>
|
|
90
|
+
)}
|
|
91
|
+
|
|
92
|
+
{/* Tax: a real estimate when the store has a rate for the buyer's
|
|
93
|
+
country, and the original "calculated at checkout" row otherwise. */}
|
|
94
|
+
<TaxEstimateLine />
|
|
95
|
+
|
|
96
|
+
{/* Divider */}
|
|
97
|
+
<Separator className="mt-2" />
|
|
98
|
+
<div className="pt-1">
|
|
99
|
+
<div className="flex items-center justify-between">
|
|
100
|
+
<span className="text-foreground font-semibold">{tc('total')}</span>
|
|
101
|
+
<span className="text-foreground text-base font-semibold">
|
|
102
|
+
{formatPrice(totals.total, { currency }) as string}
|
|
103
|
+
</span>
|
|
104
|
+
</div>
|
|
105
|
+
</div>
|
|
106
|
+
</div>
|
|
107
|
+
</div>
|
|
108
|
+
);
|
|
109
|
+
}
|