create-brainerce-store 1.72.0 → 1.73.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.
@@ -1,158 +1,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 { IconMinus, IconPlus, IconTrash } from '@/ui/shared/icons';
13
-
14
- interface CartItemProps {
15
- item: CartItemType;
16
- onUpdate: () => void;
17
- className?: string;
18
- }
19
-
20
- /**
21
- * Single cart line: image, name, variant, unit price, quantity controls,
22
- * remove, line total; the `item` prop comes from useCartPage().cart.
23
- */
24
- export function CartItem({ item, onUpdate, className }: CartItemProps) {
25
- const t = useTranslations('common');
26
- const td = useTranslations('productDetail');
27
- const currency = useCurrency();
28
- const [updating, setUpdating] = useState(false);
29
- const [removing, setRemoving] = useState(false);
30
-
31
- const productName = item.product.name;
32
- const imageUrl = getCartItemImage(item);
33
- const variantName = item.variant?.name;
34
- const unitPrice = parseFloat(item.unitPrice);
35
- const lineTotal = unitPrice * item.quantity;
36
-
37
- // The server decides purchasability, per line. `isAvailable === false` means
38
- // this line blocks checkout until it is removed, whether the stock ran out
39
- // (a released reservation is the common cause) or the product/variant was
40
- // withdrawn from sale. `unavailableReason` separates the two for the label.
41
- const isUnavailable = item.isAvailable === false;
42
- const unavailableLabel = isUnavailable
43
- ? item.unavailableReason === 'OUT_OF_STOCK'
44
- ? td('outOfStock')
45
- : td('unavailable')
46
- : null;
47
-
48
- async function handleQuantityChange(newQuantity: number) {
49
- if (newQuantity < 1 || updating) return;
50
-
51
- try {
52
- setUpdating(true);
53
- const client = getClient();
54
- await client.smartUpdateCartItem(item.productId, newQuantity, item.variantId || undefined);
55
- onUpdate();
56
- } catch (err) {
57
- console.error('Failed to update quantity:', err);
58
- } finally {
59
- setUpdating(false);
60
- }
61
- }
62
-
63
- async function handleRemove() {
64
- if (removing) return;
65
-
66
- try {
67
- setRemoving(true);
68
- const client = getClient();
69
- await client.smartRemoveFromCart(item.productId, item.variantId || undefined);
70
- onUpdate();
71
- } catch (err) {
72
- console.error('Failed to remove item:', err);
73
- } finally {
74
- setRemoving(false);
75
- }
76
- }
77
-
78
- return (
79
- <div className={cn('flex items-start gap-4', className)}>
80
- {/* Image — `relative` + fixed box are layout-critical for next/image fill */}
81
- <div className="relative h-20 w-20 shrink-0 overflow-hidden rounded-lg border bg-secondary sm:h-24 sm:w-24">
82
- {imageUrl ? (
83
- <Image src={imageUrl} alt={productName} fill sizes="96px" className="object-cover" />
84
- ) : (
85
- <span className="sr-only">{productName}</span>
86
- )}
87
- </div>
88
-
89
- {/* Details */}
90
- <div className="min-w-0 flex-1">
91
- <h3 className="font-sans text-[15px] font-semibold leading-snug text-foreground">
92
- {productName}
93
- </h3>
94
-
95
- {/* Availability badge. This line blocks checkout while it shows. */}
96
- {unavailableLabel && (
97
- <span className="mt-1 inline-flex items-center rounded-full bg-destructive/10 px-2 py-0.5 text-xs font-medium text-destructive">
98
- {unavailableLabel}
99
- </span>
100
- )}
101
-
102
- {/* Variant name */}
103
- {variantName && <p className="mt-0.5 text-xs text-muted-foreground">{variantName}</p>}
104
-
105
- {/* Unit price */}
106
- <p className="mt-1 text-sm text-muted-foreground">
107
- {formatPrice(unitPrice, { currency }) as string}
108
- </p>
109
-
110
- {/* Quantity controls + remove */}
111
- <div className="mt-3 flex flex-wrap items-center gap-3">
112
- <div
113
- className="flex h-9 items-center rounded-full border"
114
- role="group"
115
- aria-label={td('quantityLabel')}
116
- >
117
- <button
118
- type="button"
119
- onClick={() => handleQuantityChange(item.quantity - 1)}
120
- disabled={updating || item.quantity <= 1}
121
- aria-label={td('decreaseQuantity')}
122
- className="flex h-full w-9 items-center justify-center rounded-s-full transition-colors hover:bg-secondary disabled:opacity-40"
123
- >
124
- <IconMinus size={16} />
125
- </button>
126
- <span aria-live="polite" className="min-w-7 text-center text-sm font-semibold">
127
- {updating ? <LoadingSpinner size="sm" /> : item.quantity}
128
- </span>
129
- <button
130
- type="button"
131
- onClick={() => handleQuantityChange(item.quantity + 1)}
132
- disabled={updating || isUnavailable}
133
- aria-label={td('increaseQuantity')}
134
- className="flex h-full w-9 items-center justify-center rounded-e-full transition-colors hover:bg-secondary disabled:opacity-40"
135
- >
136
- <IconPlus size={16} />
137
- </button>
138
- </div>
139
-
140
- <button
141
- type="button"
142
- onClick={handleRemove}
143
- disabled={removing}
144
- className="inline-flex items-center gap-1.5 text-xs font-medium text-muted-foreground transition-colors hover:text-destructive disabled:opacity-50"
145
- >
146
- <IconTrash size={16} />
147
- {removing ? t('removing') : t('remove')}
148
- </button>
149
- </div>
150
- </div>
151
-
152
- {/* Line total */}
153
- <span className="text-base font-semibold text-foreground">
154
- {formatPrice(lineTotal, { currency }) as string}
155
- </span>
156
- </div>
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 { IconMinus, IconPlus, IconTrash } from '@/ui/shared/icons';
13
+
14
+ interface CartItemProps {
15
+ item: CartItemType;
16
+ onUpdate: () => void;
17
+ className?: string;
18
+ }
19
+
20
+ /**
21
+ * Single cart line: image, name, variant, unit price, quantity controls,
22
+ * remove, line total; the `item` prop comes from useCartPage().cart.
23
+ */
24
+ export function CartItem({ item, onUpdate, className }: CartItemProps) {
25
+ const t = useTranslations('common');
26
+ const td = useTranslations('productDetail');
27
+ const currency = useCurrency();
28
+ const [updating, setUpdating] = useState(false);
29
+ const [removing, setRemoving] = useState(false);
30
+
31
+ const productName = item.product.name;
32
+ const imageUrl = getCartItemImage(item);
33
+ const variantName = item.variant?.name;
34
+ const unitPrice = parseFloat(item.unitPrice);
35
+ const lineTotal = unitPrice * item.quantity;
36
+
37
+ // The server decides purchasability, per line. `isAvailable === false` means
38
+ // this line blocks checkout until it is removed, whether the stock ran out
39
+ // (a released reservation is the common cause) or the product/variant was
40
+ // withdrawn from sale. `unavailableReason` separates the two for the label.
41
+ const isUnavailable = item.isAvailable === false;
42
+ const unavailableLabel = isUnavailable
43
+ ? item.unavailableReason === 'OUT_OF_STOCK'
44
+ ? td('outOfStock')
45
+ : td('unavailable')
46
+ : null;
47
+
48
+ async function handleQuantityChange(newQuantity: number) {
49
+ if (newQuantity < 1 || updating) return;
50
+
51
+ try {
52
+ setUpdating(true);
53
+ const client = getClient();
54
+ await client.smartUpdateCartItem(item.productId, newQuantity, item.variantId || undefined);
55
+ onUpdate();
56
+ } catch (err) {
57
+ console.error('Failed to update quantity:', err);
58
+ } finally {
59
+ setUpdating(false);
60
+ }
61
+ }
62
+
63
+ async function handleRemove() {
64
+ if (removing) return;
65
+
66
+ try {
67
+ setRemoving(true);
68
+ const client = getClient();
69
+ await client.smartRemoveFromCart(item.productId, item.variantId || undefined);
70
+ onUpdate();
71
+ } catch (err) {
72
+ console.error('Failed to remove item:', err);
73
+ } finally {
74
+ setRemoving(false);
75
+ }
76
+ }
77
+
78
+ return (
79
+ <div className={cn('flex items-start gap-4', className)}>
80
+ {/* Image — `relative` + fixed box are layout-critical for next/image fill */}
81
+ <div className="bg-secondary relative h-20 w-20 shrink-0 overflow-hidden rounded-lg border sm:h-24 sm:w-24">
82
+ {imageUrl ? (
83
+ <Image src={imageUrl} alt={productName} fill sizes="96px" className="object-cover" />
84
+ ) : (
85
+ <span className="sr-only">{productName}</span>
86
+ )}
87
+ </div>
88
+
89
+ {/* Details */}
90
+ <div className="min-w-0 flex-1">
91
+ <h3 className="text-foreground font-sans text-[15px] font-semibold leading-snug">
92
+ {productName}
93
+ </h3>
94
+
95
+ {/* Availability badge. This line blocks checkout while it shows. */}
96
+ {unavailableLabel && (
97
+ <span className="bg-destructive/10 text-destructive mt-1 inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium">
98
+ {unavailableLabel}
99
+ </span>
100
+ )}
101
+
102
+ {/* Variant name */}
103
+ {variantName && <p className="text-muted-foreground mt-0.5 text-xs">{variantName}</p>}
104
+
105
+ {/* Unit price */}
106
+ <p className="text-muted-foreground mt-1 text-sm">
107
+ {formatPrice(unitPrice, { currency }) as string}
108
+ </p>
109
+
110
+ {/* Quantity controls + remove */}
111
+ <div className="mt-3 flex flex-wrap items-center gap-3">
112
+ <div
113
+ className="flex h-9 items-center rounded-full border"
114
+ role="group"
115
+ aria-label={td('quantityLabel')}
116
+ >
117
+ <button
118
+ type="button"
119
+ onClick={() => handleQuantityChange(item.quantity - 1)}
120
+ disabled={updating || item.quantity <= 1}
121
+ aria-label={td('decreaseQuantity')}
122
+ className="hover:bg-secondary flex h-full w-9 items-center justify-center rounded-s-full transition-colors disabled:opacity-40"
123
+ >
124
+ <IconMinus size={16} />
125
+ </button>
126
+ <span aria-live="polite" className="min-w-7 text-center text-sm font-semibold">
127
+ {updating ? <LoadingSpinner size="sm" /> : item.quantity}
128
+ </span>
129
+ <button
130
+ type="button"
131
+ onClick={() => handleQuantityChange(item.quantity + 1)}
132
+ disabled={updating || isUnavailable}
133
+ aria-label={td('increaseQuantity')}
134
+ className="hover:bg-secondary flex h-full w-9 items-center justify-center rounded-e-full transition-colors disabled:opacity-40"
135
+ >
136
+ <IconPlus size={16} />
137
+ </button>
138
+ </div>
139
+
140
+ <button
141
+ type="button"
142
+ onClick={handleRemove}
143
+ disabled={removing}
144
+ className="text-muted-foreground hover:text-destructive inline-flex items-center gap-1.5 text-xs font-medium transition-colors disabled:opacity-50"
145
+ >
146
+ <IconTrash size={16} />
147
+ {removing ? t('removing') : t('remove')}
148
+ </button>
149
+ </div>
150
+ </div>
151
+
152
+ {/* Line total */}
153
+ <span className="text-foreground text-base font-semibold">
154
+ {formatPrice(lineTotal, { currency }) as string}
155
+ </span>
156
+ </div>
157
+ );
158
+ }