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