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,158 +1,168 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import { useState } from 'react';
|
|
4
|
-
import { CdnImage as Image } from '@/ui/shared/cdn-image';
|
|
5
|
-
import type { CartItem as CartItemType } from 'brainerce';
|
|
6
|
-
import { getCartItemImage, formatPrice } from 'brainerce';
|
|
7
|
-
import { getClient } from '@/core/lib/brainerce';
|
|
8
|
-
import { useTranslations } from '@/core/lib/translations';
|
|
9
|
-
import { useCurrency } from '@/core/lib/use-currency';
|
|
10
|
-
import { LoadingSpinner } from '@/ui/shared/loading-spinner';
|
|
11
|
-
import { cn } from '@/core/lib/utils';
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
*
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
const
|
|
29
|
-
const [
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
//
|
|
39
|
-
//
|
|
40
|
-
//
|
|
41
|
-
|
|
42
|
-
const
|
|
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
|
-
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useState } from 'react';
|
|
4
|
+
import { CdnImage as Image } from '@/ui/shared/cdn-image';
|
|
5
|
+
import type { CartItem as CartItemType } from 'brainerce';
|
|
6
|
+
import { getCartItemImage, formatPrice } from 'brainerce';
|
|
7
|
+
import { getClient } from '@/core/lib/brainerce';
|
|
8
|
+
import { useTranslations } from '@/core/lib/translations';
|
|
9
|
+
import { useCurrency } from '@/core/lib/use-currency';
|
|
10
|
+
import { LoadingSpinner } from '@/ui/shared/loading-spinner';
|
|
11
|
+
import { cn } from '@/core/lib/utils';
|
|
12
|
+
import { OrderCustomizations } from '@/components/account/order-customizations';
|
|
13
|
+
import { IconMinus, IconPlus, IconTrash } from '@/ui/shared/icons';
|
|
14
|
+
|
|
15
|
+
interface CartItemProps {
|
|
16
|
+
item: CartItemType;
|
|
17
|
+
onUpdate: () => void;
|
|
18
|
+
className?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Single cart line: image, name, variant, unit price, quantity controls,
|
|
23
|
+
* remove, line total; the `item` prop comes from useCartPage().cart.
|
|
24
|
+
*/
|
|
25
|
+
export function CartItem({ item, onUpdate, className }: CartItemProps) {
|
|
26
|
+
const t = useTranslations('common');
|
|
27
|
+
const td = useTranslations('productDetail');
|
|
28
|
+
const currency = useCurrency();
|
|
29
|
+
const [updating, setUpdating] = useState(false);
|
|
30
|
+
const [removing, setRemoving] = useState(false);
|
|
31
|
+
|
|
32
|
+
const productName = item.product.name;
|
|
33
|
+
const imageUrl = getCartItemImage(item);
|
|
34
|
+
const variantName = item.variant?.name;
|
|
35
|
+
const unitPrice = parseFloat(item.unitPrice);
|
|
36
|
+
const lineTotal = unitPrice * item.quantity;
|
|
37
|
+
|
|
38
|
+
// The server decides purchasability, per line. `isAvailable === false` means
|
|
39
|
+
// this line blocks checkout until it is removed, whether the stock ran out
|
|
40
|
+
// (a released reservation is the common cause) or the product/variant was
|
|
41
|
+
// withdrawn from sale. `unavailableReason` separates the two for the label.
|
|
42
|
+
const isUnavailable = item.isAvailable === false;
|
|
43
|
+
const unavailableLabel = isUnavailable
|
|
44
|
+
? item.unavailableReason === 'OUT_OF_STOCK'
|
|
45
|
+
? td('outOfStock')
|
|
46
|
+
: td('unavailable')
|
|
47
|
+
: null;
|
|
48
|
+
|
|
49
|
+
async function handleQuantityChange(newQuantity: number) {
|
|
50
|
+
if (newQuantity < 1 || updating) return;
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
setUpdating(true);
|
|
54
|
+
const client = getClient();
|
|
55
|
+
await client.smartUpdateCartItem(item.productId, newQuantity, item.variantId || undefined);
|
|
56
|
+
onUpdate();
|
|
57
|
+
} catch (err) {
|
|
58
|
+
console.error('Failed to update quantity:', err);
|
|
59
|
+
} finally {
|
|
60
|
+
setUpdating(false);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function handleRemove() {
|
|
65
|
+
if (removing) return;
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
setRemoving(true);
|
|
69
|
+
const client = getClient();
|
|
70
|
+
await client.smartRemoveFromCart(item.productId, item.variantId || undefined);
|
|
71
|
+
onUpdate();
|
|
72
|
+
} catch (err) {
|
|
73
|
+
console.error('Failed to remove item:', err);
|
|
74
|
+
} finally {
|
|
75
|
+
setRemoving(false);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<div className={cn('flex items-start gap-4', className)}>
|
|
81
|
+
{/* Image — `relative` + fixed box are layout-critical for next/image fill */}
|
|
82
|
+
<div className="bg-secondary relative h-20 w-20 shrink-0 overflow-hidden rounded-lg border sm:h-24 sm:w-24">
|
|
83
|
+
{imageUrl ? (
|
|
84
|
+
<Image src={imageUrl} alt={productName} fill sizes="96px" className="object-cover" />
|
|
85
|
+
) : (
|
|
86
|
+
<span className="sr-only">{productName}</span>
|
|
87
|
+
)}
|
|
88
|
+
</div>
|
|
89
|
+
|
|
90
|
+
{/* Details */}
|
|
91
|
+
<div className="min-w-0 flex-1">
|
|
92
|
+
<h3 className="text-foreground font-sans text-[15px] font-semibold leading-snug">
|
|
93
|
+
{productName}
|
|
94
|
+
</h3>
|
|
95
|
+
|
|
96
|
+
{/* Availability badge. This line blocks checkout while it shows. */}
|
|
97
|
+
{unavailableLabel && (
|
|
98
|
+
<span className="bg-destructive/10 text-destructive mt-1 inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium">
|
|
99
|
+
{unavailableLabel}
|
|
100
|
+
</span>
|
|
101
|
+
)}
|
|
102
|
+
|
|
103
|
+
{/* Variant name */}
|
|
104
|
+
{variantName && <p className="text-muted-foreground mt-0.5 text-xs">{variantName}</p>}
|
|
105
|
+
|
|
106
|
+
{/* What the buyer typed / uploaded / picked, keyed by the field's own
|
|
107
|
+
`key` and carrying the merchant's label. The SDK resolves this on
|
|
108
|
+
the cart line itself (`CartItem.customizations`, same shape as
|
|
109
|
+
`OrderItem.customizations`) so no product fetch is needed for the
|
|
110
|
+
labels. Present only when the line has at least one value set, and
|
|
111
|
+
the component returns null on an empty map, so a plain product
|
|
112
|
+
renders exactly as before. */}
|
|
113
|
+
{item.customizations && <OrderCustomizations customizations={item.customizations} />}
|
|
114
|
+
|
|
115
|
+
{/* Unit price */}
|
|
116
|
+
<p className="text-muted-foreground mt-1 text-sm">
|
|
117
|
+
{formatPrice(unitPrice, { currency }) as string}
|
|
118
|
+
</p>
|
|
119
|
+
|
|
120
|
+
{/* Quantity controls + remove */}
|
|
121
|
+
<div className="mt-3 flex flex-wrap items-center gap-3">
|
|
122
|
+
<div
|
|
123
|
+
className="flex h-9 items-center rounded-full border"
|
|
124
|
+
role="group"
|
|
125
|
+
aria-label={td('quantityLabel')}
|
|
126
|
+
>
|
|
127
|
+
<button
|
|
128
|
+
type="button"
|
|
129
|
+
onClick={() => handleQuantityChange(item.quantity - 1)}
|
|
130
|
+
disabled={updating || item.quantity <= 1}
|
|
131
|
+
aria-label={td('decreaseQuantity')}
|
|
132
|
+
className="hover:bg-secondary flex h-full w-9 items-center justify-center rounded-s-full transition-colors disabled:opacity-40"
|
|
133
|
+
>
|
|
134
|
+
<IconMinus size={16} />
|
|
135
|
+
</button>
|
|
136
|
+
<span aria-live="polite" className="min-w-7 text-center text-sm font-semibold">
|
|
137
|
+
{updating ? <LoadingSpinner size="sm" /> : item.quantity}
|
|
138
|
+
</span>
|
|
139
|
+
<button
|
|
140
|
+
type="button"
|
|
141
|
+
onClick={() => handleQuantityChange(item.quantity + 1)}
|
|
142
|
+
disabled={updating || isUnavailable}
|
|
143
|
+
aria-label={td('increaseQuantity')}
|
|
144
|
+
className="hover:bg-secondary flex h-full w-9 items-center justify-center rounded-e-full transition-colors disabled:opacity-40"
|
|
145
|
+
>
|
|
146
|
+
<IconPlus size={16} />
|
|
147
|
+
</button>
|
|
148
|
+
</div>
|
|
149
|
+
|
|
150
|
+
<button
|
|
151
|
+
type="button"
|
|
152
|
+
onClick={handleRemove}
|
|
153
|
+
disabled={removing}
|
|
154
|
+
className="text-muted-foreground hover:text-destructive inline-flex items-center gap-1.5 text-xs font-medium transition-colors disabled:opacity-50"
|
|
155
|
+
>
|
|
156
|
+
<IconTrash size={16} />
|
|
157
|
+
{removing ? t('removing') : t('remove')}
|
|
158
|
+
</button>
|
|
159
|
+
</div>
|
|
160
|
+
</div>
|
|
161
|
+
|
|
162
|
+
{/* Line total */}
|
|
163
|
+
<span className="text-foreground text-base font-semibold">
|
|
164
|
+
{formatPrice(lineTotal, { currency }) as string}
|
|
165
|
+
</span>
|
|
166
|
+
</div>
|
|
167
|
+
);
|
|
168
|
+
}
|
|
@@ -1,107 +1,106 @@
|
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
const
|
|
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
|
-
|
|
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
|
-
}
|
|
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 { cn } from '@/core/lib/utils';
|
|
9
|
+
|
|
10
|
+
interface CartSummaryProps {
|
|
11
|
+
className?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Order summary (subtotal, rule/coupon discounts, shipping, tax note, total);
|
|
16
|
+
* all figures come from useCart().totals and useCart().cart. Every discount
|
|
17
|
+
* branch is kept — merchants rely on the per-rule breakdown.
|
|
18
|
+
*/
|
|
19
|
+
export function CartSummary({ className }: CartSummaryProps) {
|
|
20
|
+
const t = useTranslations('cart');
|
|
21
|
+
const tc = useTranslations('common');
|
|
22
|
+
const { totals, cart } = useCart();
|
|
23
|
+
const currency = useCurrency();
|
|
24
|
+
|
|
25
|
+
const rules = cart?.appliedDiscounts;
|
|
26
|
+
const ruleAmt = cart?.ruleDiscountAmount ? parseFloat(cart.ruleDiscountAmount) : 0;
|
|
27
|
+
const couponAmt = totals.discount - ruleAmt;
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<section className={cn(className)}>
|
|
31
|
+
<h3 className="mb-4 font-sans text-base font-semibold">{t('orderSummary')}</h3>
|
|
32
|
+
|
|
33
|
+
<dl className="space-y-2.5 text-sm">
|
|
34
|
+
{/* Subtotal */}
|
|
35
|
+
<div className="flex items-center justify-between gap-4">
|
|
36
|
+
<dt className="text-muted-foreground">{tc('subtotal')}</dt>
|
|
37
|
+
<dd className="font-medium">{formatPrice(totals.subtotal, { currency }) as string}</dd>
|
|
38
|
+
</div>
|
|
39
|
+
|
|
40
|
+
{/* Rule discounts - show each rule by name */}
|
|
41
|
+
{rules && rules.length > 0
|
|
42
|
+
? rules.map((rule) => (
|
|
43
|
+
<div key={rule.ruleId} className="flex items-center justify-between gap-4">
|
|
44
|
+
<dt className="text-primary">{rule.ruleName}</dt>
|
|
45
|
+
<dd className="font-medium text-primary">
|
|
46
|
+
-{formatPrice(parseFloat(rule.discountAmount), { currency }) as string}
|
|
47
|
+
</dd>
|
|
48
|
+
</div>
|
|
49
|
+
))
|
|
50
|
+
: ruleAmt > 0 && (
|
|
51
|
+
<div className="flex items-center justify-between gap-4">
|
|
52
|
+
<dt className="text-primary">{tc('generalDiscount')}</dt>
|
|
53
|
+
<dd className="font-medium text-primary">
|
|
54
|
+
-{formatPrice(ruleAmt, { currency }) as string}
|
|
55
|
+
</dd>
|
|
56
|
+
</div>
|
|
57
|
+
)}
|
|
58
|
+
|
|
59
|
+
{/* Coupon discount */}
|
|
60
|
+
{cart?.couponCode && couponAmt > 0 && (
|
|
61
|
+
<div className="flex items-center justify-between gap-4">
|
|
62
|
+
<dt className="text-primary">
|
|
63
|
+
{tc('couponDiscount')} ({cart.couponCode})
|
|
64
|
+
</dt>
|
|
65
|
+
<dd className="font-medium text-primary">
|
|
66
|
+
-{formatPrice(couponAmt, { currency }) as string}
|
|
67
|
+
</dd>
|
|
68
|
+
</div>
|
|
69
|
+
)}
|
|
70
|
+
|
|
71
|
+
{/* Fallback: generic discount when no breakdown available */}
|
|
72
|
+
{totals.discount > 0 &&
|
|
73
|
+
ruleAmt <= 0 &&
|
|
74
|
+
!cart?.couponCode &&
|
|
75
|
+
(!rules || rules.length === 0) && (
|
|
76
|
+
<div className="flex items-center justify-between gap-4">
|
|
77
|
+
<dt className="text-primary">{tc('discount')}</dt>
|
|
78
|
+
<dd className="font-medium text-primary">
|
|
79
|
+
-{formatPrice(totals.discount, { currency }) as string}
|
|
80
|
+
</dd>
|
|
81
|
+
</div>
|
|
82
|
+
)}
|
|
83
|
+
|
|
84
|
+
{/* Shipping */}
|
|
85
|
+
{totals.shipping > 0 && (
|
|
86
|
+
<div className="flex items-center justify-between gap-4">
|
|
87
|
+
<dt className="text-muted-foreground">{tc('shipping')}</dt>
|
|
88
|
+
<dd className="font-medium">{formatPrice(totals.shipping, { currency }) as string}</dd>
|
|
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
|
+
{/* Total */}
|
|
97
|
+
<div className="flex items-center justify-between gap-4 border-t pt-3">
|
|
98
|
+
<dt className="text-base font-semibold text-foreground">{tc('total')}</dt>
|
|
99
|
+
<dd className="text-lg font-bold text-foreground">
|
|
100
|
+
{formatPrice(totals.total, { currency }) as string}
|
|
101
|
+
</dd>
|
|
102
|
+
</div>
|
|
103
|
+
</dl>
|
|
104
|
+
</section>
|
|
105
|
+
);
|
|
106
|
+
}
|