create-brainerce-store 1.0.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.d.ts +1 -0
- package/dist/index.js +502 -0
- package/package.json +44 -0
- package/templates/nextjs/base/.env.local.ejs +3 -0
- package/templates/nextjs/base/.eslintrc.json +3 -0
- package/templates/nextjs/base/gitignore +30 -0
- package/templates/nextjs/base/next.config.ts +9 -0
- package/templates/nextjs/base/package.json.ejs +30 -0
- package/templates/nextjs/base/postcss.config.mjs +9 -0
- package/templates/nextjs/base/src/app/account/page.tsx +105 -0
- package/templates/nextjs/base/src/app/auth/callback/page.tsx +99 -0
- package/templates/nextjs/base/src/app/cart/page.tsx +263 -0
- package/templates/nextjs/base/src/app/checkout/page.tsx +463 -0
- package/templates/nextjs/base/src/app/globals.css +30 -0
- package/templates/nextjs/base/src/app/layout.tsx.ejs +33 -0
- package/templates/nextjs/base/src/app/login/page.tsx +56 -0
- package/templates/nextjs/base/src/app/order-confirmation/page.tsx +191 -0
- package/templates/nextjs/base/src/app/page.tsx +95 -0
- package/templates/nextjs/base/src/app/products/[slug]/page.tsx +346 -0
- package/templates/nextjs/base/src/app/products/page.tsx +243 -0
- package/templates/nextjs/base/src/app/register/page.tsx +66 -0
- package/templates/nextjs/base/src/app/verify-email/page.tsx +291 -0
- package/templates/nextjs/base/src/components/account/order-history.tsx +184 -0
- package/templates/nextjs/base/src/components/account/profile-section.tsx +73 -0
- package/templates/nextjs/base/src/components/auth/login-form.tsx +92 -0
- package/templates/nextjs/base/src/components/auth/oauth-buttons.tsx +134 -0
- package/templates/nextjs/base/src/components/auth/register-form.tsx +177 -0
- package/templates/nextjs/base/src/components/cart/cart-item.tsx +150 -0
- package/templates/nextjs/base/src/components/cart/cart-nudges.tsx +39 -0
- package/templates/nextjs/base/src/components/cart/cart-summary.tsx +67 -0
- package/templates/nextjs/base/src/components/cart/coupon-input.tsx +131 -0
- package/templates/nextjs/base/src/components/cart/reservation-countdown.tsx +100 -0
- package/templates/nextjs/base/src/components/checkout/checkout-form.tsx +273 -0
- package/templates/nextjs/base/src/components/checkout/payment-step.tsx +124 -0
- package/templates/nextjs/base/src/components/checkout/shipping-step.tsx +111 -0
- package/templates/nextjs/base/src/components/checkout/tax-display.tsx +62 -0
- package/templates/nextjs/base/src/components/layout/footer.tsx +35 -0
- package/templates/nextjs/base/src/components/layout/header.tsx +329 -0
- package/templates/nextjs/base/src/components/products/discount-badge.tsx +36 -0
- package/templates/nextjs/base/src/components/products/product-card.tsx +94 -0
- package/templates/nextjs/base/src/components/products/product-grid.tsx +33 -0
- package/templates/nextjs/base/src/components/products/stock-badge.tsx +34 -0
- package/templates/nextjs/base/src/components/products/variant-selector.tsx +147 -0
- package/templates/nextjs/base/src/components/shared/loading-spinner.tsx +30 -0
- package/templates/nextjs/base/src/components/shared/price-display.tsx +62 -0
- package/templates/nextjs/base/src/hooks/use-search.ts +77 -0
- package/templates/nextjs/base/src/lib/brainerce.ts.ejs +59 -0
- package/templates/nextjs/base/src/lib/utils.ts +6 -0
- package/templates/nextjs/base/src/providers/store-provider.tsx.ejs +168 -0
- package/templates/nextjs/base/tailwind.config.ts +30 -0
- package/templates/nextjs/base/tsconfig.json +23 -0
- package/templates/nextjs/themes/minimal/globals.css +30 -0
- package/templates/nextjs/themes/minimal/theme.json +23 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { Suspense, useEffect, useState } from 'react';
|
|
4
|
+
import { useSearchParams } from 'next/navigation';
|
|
5
|
+
import Link from 'next/link';
|
|
6
|
+
import type { WaitForOrderResult } from 'brainerce';
|
|
7
|
+
import { getClient } from '@/lib/brainerce';
|
|
8
|
+
import { LoadingSpinner } from '@/components/shared/loading-spinner';
|
|
9
|
+
|
|
10
|
+
function OrderConfirmationContent() {
|
|
11
|
+
const searchParams = useSearchParams();
|
|
12
|
+
const checkoutId = searchParams.get('checkout_id');
|
|
13
|
+
|
|
14
|
+
const [result, setResult] = useState<WaitForOrderResult | null>(null);
|
|
15
|
+
const [loading, setLoading] = useState(true);
|
|
16
|
+
const [error, setError] = useState<string | null>(null);
|
|
17
|
+
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
if (!checkoutId) {
|
|
20
|
+
setError('Missing checkout information.');
|
|
21
|
+
setLoading(false);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function waitForOrder() {
|
|
26
|
+
try {
|
|
27
|
+
const client = getClient();
|
|
28
|
+
const orderResult = await client.waitForOrder(checkoutId!, {
|
|
29
|
+
maxWaitMs: 30000,
|
|
30
|
+
});
|
|
31
|
+
setResult(orderResult);
|
|
32
|
+
} catch (err) {
|
|
33
|
+
const message = err instanceof Error ? err.message : 'Failed to confirm order';
|
|
34
|
+
setError(message);
|
|
35
|
+
} finally {
|
|
36
|
+
setLoading(false);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
waitForOrder();
|
|
41
|
+
}, [checkoutId]);
|
|
42
|
+
|
|
43
|
+
if (loading) {
|
|
44
|
+
return (
|
|
45
|
+
<div className="flex min-h-[60vh] flex-col items-center justify-center">
|
|
46
|
+
<LoadingSpinner size="lg" />
|
|
47
|
+
<p className="text-muted-foreground mt-4">Confirming your order...</p>
|
|
48
|
+
<p className="text-muted-foreground mt-1 text-xs">This may take a few seconds.</p>
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (error) {
|
|
54
|
+
return (
|
|
55
|
+
<div className="mx-auto max-w-2xl px-4 py-16 text-center sm:px-6 lg:px-8">
|
|
56
|
+
<svg
|
|
57
|
+
className="text-destructive mx-auto mb-4 h-16 w-16"
|
|
58
|
+
fill="none"
|
|
59
|
+
viewBox="0 0 24 24"
|
|
60
|
+
stroke="currentColor"
|
|
61
|
+
>
|
|
62
|
+
<path
|
|
63
|
+
strokeLinecap="round"
|
|
64
|
+
strokeLinejoin="round"
|
|
65
|
+
strokeWidth={1.5}
|
|
66
|
+
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.834-2.694-.834-3.464 0L3.34 16.5c-.77.833.192 2.5 1.732 2.5z"
|
|
67
|
+
/>
|
|
68
|
+
</svg>
|
|
69
|
+
<h1 className="text-foreground text-2xl font-bold">Something went wrong</h1>
|
|
70
|
+
<p className="text-muted-foreground mt-2">{error}</p>
|
|
71
|
+
<p className="text-muted-foreground mt-1 text-sm">
|
|
72
|
+
If you were charged, your order may still be processing. Please check your email for a
|
|
73
|
+
confirmation.
|
|
74
|
+
</p>
|
|
75
|
+
<Link
|
|
76
|
+
href="/"
|
|
77
|
+
className="bg-primary text-primary-foreground mt-6 inline-flex items-center rounded px-6 py-3 font-medium transition-opacity hover:opacity-90"
|
|
78
|
+
>
|
|
79
|
+
Return Home
|
|
80
|
+
</Link>
|
|
81
|
+
</div>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Order was created successfully
|
|
86
|
+
if (result?.success) {
|
|
87
|
+
const orderNumber = result.status.orderNumber;
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<div className="mx-auto max-w-2xl px-4 py-16 text-center sm:px-6 lg:px-8">
|
|
91
|
+
<svg
|
|
92
|
+
className="text-primary mx-auto mb-4 h-16 w-16"
|
|
93
|
+
fill="none"
|
|
94
|
+
viewBox="0 0 24 24"
|
|
95
|
+
stroke="currentColor"
|
|
96
|
+
>
|
|
97
|
+
<path
|
|
98
|
+
strokeLinecap="round"
|
|
99
|
+
strokeLinejoin="round"
|
|
100
|
+
strokeWidth={1.5}
|
|
101
|
+
d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
|
|
102
|
+
/>
|
|
103
|
+
</svg>
|
|
104
|
+
|
|
105
|
+
<h1 className="text-foreground text-2xl font-bold">Thank you for your order!</h1>
|
|
106
|
+
|
|
107
|
+
{orderNumber && (
|
|
108
|
+
<p className="text-foreground mt-3 text-lg">
|
|
109
|
+
Order Number: <span className="font-semibold">{orderNumber}</span>
|
|
110
|
+
</p>
|
|
111
|
+
)}
|
|
112
|
+
|
|
113
|
+
<p className="text-muted-foreground mt-2">
|
|
114
|
+
We've sent a confirmation email with your order details.
|
|
115
|
+
</p>
|
|
116
|
+
|
|
117
|
+
<div className="mt-8 flex flex-col items-center justify-center gap-3 sm:flex-row">
|
|
118
|
+
<Link
|
|
119
|
+
href="/products"
|
|
120
|
+
className="bg-primary text-primary-foreground inline-flex items-center rounded px-6 py-3 font-medium transition-opacity hover:opacity-90"
|
|
121
|
+
>
|
|
122
|
+
Continue Shopping
|
|
123
|
+
</Link>
|
|
124
|
+
|
|
125
|
+
<Link
|
|
126
|
+
href="/account"
|
|
127
|
+
className="border-border text-foreground hover:bg-muted inline-flex items-center rounded border px-6 py-3 font-medium transition-colors"
|
|
128
|
+
>
|
|
129
|
+
View Orders
|
|
130
|
+
</Link>
|
|
131
|
+
</div>
|
|
132
|
+
</div>
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Order not yet confirmed (polling timed out) - still show success
|
|
137
|
+
return (
|
|
138
|
+
<div className="mx-auto max-w-2xl px-4 py-16 text-center sm:px-6 lg:px-8">
|
|
139
|
+
<svg
|
|
140
|
+
className="text-primary mx-auto mb-4 h-16 w-16"
|
|
141
|
+
fill="none"
|
|
142
|
+
viewBox="0 0 24 24"
|
|
143
|
+
stroke="currentColor"
|
|
144
|
+
>
|
|
145
|
+
<path
|
|
146
|
+
strokeLinecap="round"
|
|
147
|
+
strokeLinejoin="round"
|
|
148
|
+
strokeWidth={1.5}
|
|
149
|
+
d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
|
|
150
|
+
/>
|
|
151
|
+
</svg>
|
|
152
|
+
|
|
153
|
+
<h1 className="text-foreground text-2xl font-bold">Payment received!</h1>
|
|
154
|
+
|
|
155
|
+
<p className="text-muted-foreground mt-2">
|
|
156
|
+
Your order is being processed. You'll receive a confirmation email shortly with your
|
|
157
|
+
order details.
|
|
158
|
+
</p>
|
|
159
|
+
|
|
160
|
+
<div className="mt-8 flex flex-col items-center justify-center gap-3 sm:flex-row">
|
|
161
|
+
<Link
|
|
162
|
+
href="/products"
|
|
163
|
+
className="bg-primary text-primary-foreground inline-flex items-center rounded px-6 py-3 font-medium transition-opacity hover:opacity-90"
|
|
164
|
+
>
|
|
165
|
+
Continue Shopping
|
|
166
|
+
</Link>
|
|
167
|
+
|
|
168
|
+
<Link
|
|
169
|
+
href="/account"
|
|
170
|
+
className="border-border text-foreground hover:bg-muted inline-flex items-center rounded border px-6 py-3 font-medium transition-colors"
|
|
171
|
+
>
|
|
172
|
+
View Orders
|
|
173
|
+
</Link>
|
|
174
|
+
</div>
|
|
175
|
+
</div>
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export default function OrderConfirmationPage() {
|
|
180
|
+
return (
|
|
181
|
+
<Suspense
|
|
182
|
+
fallback={
|
|
183
|
+
<div className="flex min-h-[60vh] items-center justify-center">
|
|
184
|
+
<LoadingSpinner size="lg" />
|
|
185
|
+
</div>
|
|
186
|
+
}
|
|
187
|
+
>
|
|
188
|
+
<OrderConfirmationContent />
|
|
189
|
+
</Suspense>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState } from 'react';
|
|
4
|
+
import Link from 'next/link';
|
|
5
|
+
import type { Product, DiscountBanner } from 'brainerce';
|
|
6
|
+
import { getClient } from '@/lib/brainerce';
|
|
7
|
+
import { useStoreInfo } from '@/providers/store-provider';
|
|
8
|
+
import { ProductGrid } from '@/components/products/product-grid';
|
|
9
|
+
import { LoadingSpinner } from '@/components/shared/loading-spinner';
|
|
10
|
+
|
|
11
|
+
export default function HomePage() {
|
|
12
|
+
const { storeInfo, loading: storeLoading } = useStoreInfo();
|
|
13
|
+
const [products, setProducts] = useState<Product[]>([]);
|
|
14
|
+
const [banners, setBanners] = useState<DiscountBanner[]>([]);
|
|
15
|
+
const [loading, setLoading] = useState(true);
|
|
16
|
+
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
async function load() {
|
|
19
|
+
try {
|
|
20
|
+
const client = getClient();
|
|
21
|
+
const [productsRes, bannersRes] = await Promise.allSettled([
|
|
22
|
+
client.getProducts({ limit: 8, sortBy: 'createdAt', sortOrder: 'desc' }),
|
|
23
|
+
client.getDiscountBanners(),
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
if (productsRes.status === 'fulfilled') {
|
|
27
|
+
setProducts(productsRes.value.data);
|
|
28
|
+
}
|
|
29
|
+
if (bannersRes.status === 'fulfilled') {
|
|
30
|
+
setBanners(bannersRes.value);
|
|
31
|
+
}
|
|
32
|
+
} catch (err) {
|
|
33
|
+
console.error('Failed to load home page data:', err);
|
|
34
|
+
} finally {
|
|
35
|
+
setLoading(false);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
load();
|
|
40
|
+
}, []);
|
|
41
|
+
|
|
42
|
+
if (storeLoading || loading) {
|
|
43
|
+
return (
|
|
44
|
+
<div className="flex min-h-[60vh] items-center justify-center">
|
|
45
|
+
<LoadingSpinner size="lg" />
|
|
46
|
+
</div>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<div>
|
|
52
|
+
{/* Discount Banners */}
|
|
53
|
+
{banners.length > 0 && (
|
|
54
|
+
<div className="bg-primary text-primary-foreground">
|
|
55
|
+
<div className="mx-auto max-w-7xl px-4 py-2 sm:px-6 lg:px-8">
|
|
56
|
+
<div className="flex items-center justify-center gap-4 overflow-x-auto text-sm font-medium">
|
|
57
|
+
{banners.map((banner) => (
|
|
58
|
+
<span key={banner.ruleId}>{banner.text}</span>
|
|
59
|
+
))}
|
|
60
|
+
</div>
|
|
61
|
+
</div>
|
|
62
|
+
</div>
|
|
63
|
+
)}
|
|
64
|
+
|
|
65
|
+
{/* Hero Section */}
|
|
66
|
+
<section className="bg-muted">
|
|
67
|
+
<div className="mx-auto max-w-7xl px-4 py-20 text-center sm:px-6 lg:px-8">
|
|
68
|
+
<h1 className="text-foreground text-4xl font-bold tracking-tight sm:text-5xl">
|
|
69
|
+
Welcome to {storeInfo?.name || 'Our Store'}
|
|
70
|
+
</h1>
|
|
71
|
+
<p className="text-muted-foreground mx-auto mt-4 max-w-2xl text-lg">
|
|
72
|
+
Discover our curated collection of products crafted with care.
|
|
73
|
+
</p>
|
|
74
|
+
<Link
|
|
75
|
+
href="/products"
|
|
76
|
+
className="bg-primary text-primary-foreground mt-8 inline-flex items-center rounded px-6 py-3 font-medium transition-opacity hover:opacity-90"
|
|
77
|
+
>
|
|
78
|
+
Shop Now
|
|
79
|
+
</Link>
|
|
80
|
+
</div>
|
|
81
|
+
</section>
|
|
82
|
+
|
|
83
|
+
{/* Featured Products */}
|
|
84
|
+
<section className="mx-auto max-w-7xl px-4 py-16 sm:px-6 lg:px-8">
|
|
85
|
+
<div className="mb-8 flex items-center justify-between">
|
|
86
|
+
<h2 className="text-foreground text-2xl font-bold">Featured Products</h2>
|
|
87
|
+
<Link href="/products" className="text-primary text-sm font-medium hover:underline">
|
|
88
|
+
View all
|
|
89
|
+
</Link>
|
|
90
|
+
</div>
|
|
91
|
+
<ProductGrid products={products} />
|
|
92
|
+
</section>
|
|
93
|
+
</div>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState, useMemo } from 'react';
|
|
4
|
+
import { useParams } from 'next/navigation';
|
|
5
|
+
import Image from 'next/image';
|
|
6
|
+
import Link from 'next/link';
|
|
7
|
+
import type { Product, ProductVariant, ProductImage } from 'brainerce';
|
|
8
|
+
import { getProductPriceInfo, getDescriptionContent } from 'brainerce';
|
|
9
|
+
import { getClient } from '@/lib/brainerce';
|
|
10
|
+
import { useCart } from '@/providers/store-provider';
|
|
11
|
+
import { PriceDisplay } from '@/components/shared/price-display';
|
|
12
|
+
import { LoadingSpinner } from '@/components/shared/loading-spinner';
|
|
13
|
+
import { VariantSelector } from '@/components/products/variant-selector';
|
|
14
|
+
import { StockBadge } from '@/components/products/stock-badge';
|
|
15
|
+
import { cn } from '@/lib/utils';
|
|
16
|
+
|
|
17
|
+
export default function ProductDetailPage() {
|
|
18
|
+
const params = useParams();
|
|
19
|
+
const slug = params.slug as string;
|
|
20
|
+
const { refreshCart } = useCart();
|
|
21
|
+
const [product, setProduct] = useState<Product | null>(null);
|
|
22
|
+
const [loading, setLoading] = useState(true);
|
|
23
|
+
const [error, setError] = useState<string | null>(null);
|
|
24
|
+
const [selectedVariant, setSelectedVariant] = useState<ProductVariant | null>(null);
|
|
25
|
+
const [selectedImageIndex, setSelectedImageIndex] = useState(0);
|
|
26
|
+
const [quantity, setQuantity] = useState(1);
|
|
27
|
+
const [addingToCart, setAddingToCart] = useState(false);
|
|
28
|
+
const [addedMessage, setAddedMessage] = useState(false);
|
|
29
|
+
|
|
30
|
+
// Load product
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
async function load() {
|
|
33
|
+
try {
|
|
34
|
+
setLoading(true);
|
|
35
|
+
setError(null);
|
|
36
|
+
const client = getClient();
|
|
37
|
+
const p = await client.getProductBySlug(slug);
|
|
38
|
+
setProduct(p);
|
|
39
|
+
|
|
40
|
+
// Auto-select first variant
|
|
41
|
+
if (p.variants && p.variants.length > 0) {
|
|
42
|
+
setSelectedVariant(p.variants[0]);
|
|
43
|
+
}
|
|
44
|
+
} catch {
|
|
45
|
+
setError('Product not found.');
|
|
46
|
+
} finally {
|
|
47
|
+
setLoading(false);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
load();
|
|
51
|
+
}, [slug]);
|
|
52
|
+
|
|
53
|
+
// Images list - switch main image when variant changes
|
|
54
|
+
const images: ProductImage[] = useMemo(() => {
|
|
55
|
+
return product?.images || [];
|
|
56
|
+
}, [product]);
|
|
57
|
+
|
|
58
|
+
// When variant changes, update selected image to variant image if available
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
if (!selectedVariant?.image || !product) return;
|
|
61
|
+
|
|
62
|
+
const variantImgUrl =
|
|
63
|
+
typeof selectedVariant.image === 'string' ? selectedVariant.image : selectedVariant.image.url;
|
|
64
|
+
|
|
65
|
+
// Find if variant image exists in product images
|
|
66
|
+
const idx = images.findIndex((img) => img.url === variantImgUrl);
|
|
67
|
+
if (idx >= 0) {
|
|
68
|
+
setSelectedImageIndex(idx);
|
|
69
|
+
} else {
|
|
70
|
+
// Variant image not in product images - select index 0 as fallback
|
|
71
|
+
// (The variant image will be shown as the main image via override)
|
|
72
|
+
setSelectedImageIndex(-1);
|
|
73
|
+
}
|
|
74
|
+
}, [selectedVariant, images, product]);
|
|
75
|
+
|
|
76
|
+
// Determine which image to show
|
|
77
|
+
const mainImageUrl = useMemo(() => {
|
|
78
|
+
if (selectedImageIndex === -1 && selectedVariant?.image) {
|
|
79
|
+
const img = selectedVariant.image;
|
|
80
|
+
return typeof img === 'string' ? img : img.url;
|
|
81
|
+
}
|
|
82
|
+
return images[selectedImageIndex]?.url || null;
|
|
83
|
+
}, [selectedImageIndex, selectedVariant, images]);
|
|
84
|
+
|
|
85
|
+
// Price info - use variant price if selected, else product price
|
|
86
|
+
const priceInfo = useMemo(() => {
|
|
87
|
+
if (selectedVariant?.price) {
|
|
88
|
+
return {
|
|
89
|
+
price: parseFloat(selectedVariant.salePrice || selectedVariant.price),
|
|
90
|
+
originalPrice: parseFloat(selectedVariant.price),
|
|
91
|
+
isOnSale:
|
|
92
|
+
selectedVariant.salePrice != null &&
|
|
93
|
+
parseFloat(selectedVariant.salePrice) < parseFloat(selectedVariant.price),
|
|
94
|
+
discountPercent:
|
|
95
|
+
selectedVariant.salePrice != null &&
|
|
96
|
+
parseFloat(selectedVariant.salePrice) < parseFloat(selectedVariant.price)
|
|
97
|
+
? Math.round(
|
|
98
|
+
((parseFloat(selectedVariant.price) - parseFloat(selectedVariant.salePrice)) /
|
|
99
|
+
parseFloat(selectedVariant.price)) *
|
|
100
|
+
100
|
|
101
|
+
)
|
|
102
|
+
: 0,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return getProductPriceInfo(product);
|
|
106
|
+
}, [product, selectedVariant]);
|
|
107
|
+
|
|
108
|
+
// Inventory: use variant inventory if selected, else product inventory
|
|
109
|
+
const inventory = selectedVariant?.inventory ?? product?.inventory ?? null;
|
|
110
|
+
const canPurchase = inventory?.canPurchase !== false;
|
|
111
|
+
|
|
112
|
+
// Description
|
|
113
|
+
const description = useMemo(() => {
|
|
114
|
+
return product ? getDescriptionContent(product) : null;
|
|
115
|
+
}, [product]);
|
|
116
|
+
|
|
117
|
+
async function handleAddToCart() {
|
|
118
|
+
if (!product || addingToCart) return;
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
setAddingToCart(true);
|
|
122
|
+
const client = getClient();
|
|
123
|
+
await client.smartAddToCart({
|
|
124
|
+
productId: product.id,
|
|
125
|
+
variantId: selectedVariant?.id,
|
|
126
|
+
quantity,
|
|
127
|
+
name: product.name,
|
|
128
|
+
price: String(priceInfo.price),
|
|
129
|
+
image: mainImageUrl || undefined,
|
|
130
|
+
});
|
|
131
|
+
await refreshCart();
|
|
132
|
+
setAddedMessage(true);
|
|
133
|
+
setTimeout(() => setAddedMessage(false), 2000);
|
|
134
|
+
} catch (err) {
|
|
135
|
+
console.error('Failed to add to cart:', err);
|
|
136
|
+
} finally {
|
|
137
|
+
setAddingToCart(false);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (loading) {
|
|
142
|
+
return (
|
|
143
|
+
<div className="flex min-h-[60vh] items-center justify-center">
|
|
144
|
+
<LoadingSpinner size="lg" />
|
|
145
|
+
</div>
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (error || !product) {
|
|
150
|
+
return (
|
|
151
|
+
<div className="mx-auto max-w-7xl px-4 py-16 text-center sm:px-6 lg:px-8">
|
|
152
|
+
<h1 className="text-foreground text-2xl font-bold">{error || 'Product not found'}</h1>
|
|
153
|
+
<Link href="/products" className="text-primary mt-4 inline-block hover:underline">
|
|
154
|
+
Back to products
|
|
155
|
+
</Link>
|
|
156
|
+
</div>
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
<div className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
|
|
162
|
+
<div className="grid grid-cols-1 gap-8 lg:grid-cols-2 lg:gap-12">
|
|
163
|
+
{/* Image Gallery */}
|
|
164
|
+
<div className="space-y-4">
|
|
165
|
+
{/* Main Image */}
|
|
166
|
+
<div className="bg-muted relative aspect-square overflow-hidden rounded-lg">
|
|
167
|
+
{mainImageUrl ? (
|
|
168
|
+
<Image
|
|
169
|
+
src={mainImageUrl}
|
|
170
|
+
alt={product.name}
|
|
171
|
+
fill
|
|
172
|
+
sizes="(max-width: 1024px) 100vw, 50vw"
|
|
173
|
+
className="object-contain"
|
|
174
|
+
priority
|
|
175
|
+
/>
|
|
176
|
+
) : (
|
|
177
|
+
<div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
|
|
178
|
+
<svg className="h-24 w-24" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
179
|
+
<path
|
|
180
|
+
strokeLinecap="round"
|
|
181
|
+
strokeLinejoin="round"
|
|
182
|
+
strokeWidth={1}
|
|
183
|
+
d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"
|
|
184
|
+
/>
|
|
185
|
+
</svg>
|
|
186
|
+
</div>
|
|
187
|
+
)}
|
|
188
|
+
</div>
|
|
189
|
+
|
|
190
|
+
{/* Thumbnails */}
|
|
191
|
+
{images.length > 1 && (
|
|
192
|
+
<div className="flex gap-2 overflow-x-auto pb-2">
|
|
193
|
+
{images.map((img, idx) => (
|
|
194
|
+
<button
|
|
195
|
+
key={idx}
|
|
196
|
+
type="button"
|
|
197
|
+
onClick={() => setSelectedImageIndex(idx)}
|
|
198
|
+
className={cn(
|
|
199
|
+
'relative h-16 w-16 flex-shrink-0 overflow-hidden rounded border-2 transition-colors',
|
|
200
|
+
selectedImageIndex === idx
|
|
201
|
+
? 'border-primary'
|
|
202
|
+
: 'border-border hover:border-muted-foreground'
|
|
203
|
+
)}
|
|
204
|
+
>
|
|
205
|
+
<Image
|
|
206
|
+
src={img.url}
|
|
207
|
+
alt={img.alt || `${product.name} ${idx + 1}`}
|
|
208
|
+
fill
|
|
209
|
+
sizes="64px"
|
|
210
|
+
className="object-cover"
|
|
211
|
+
/>
|
|
212
|
+
</button>
|
|
213
|
+
))}
|
|
214
|
+
</div>
|
|
215
|
+
)}
|
|
216
|
+
</div>
|
|
217
|
+
|
|
218
|
+
{/* Product Info */}
|
|
219
|
+
<div className="space-y-6">
|
|
220
|
+
{/* Categories */}
|
|
221
|
+
{product.categories && product.categories.length > 0 && (
|
|
222
|
+
<div className="flex flex-wrap gap-2">
|
|
223
|
+
{product.categories.map((cat) => (
|
|
224
|
+
<span
|
|
225
|
+
key={cat.id}
|
|
226
|
+
className="text-muted-foreground bg-muted rounded px-2 py-1 text-xs"
|
|
227
|
+
>
|
|
228
|
+
{cat.name}
|
|
229
|
+
</span>
|
|
230
|
+
))}
|
|
231
|
+
</div>
|
|
232
|
+
)}
|
|
233
|
+
|
|
234
|
+
{/* Title */}
|
|
235
|
+
<h1 className="text-foreground text-2xl font-bold sm:text-3xl">{product.name}</h1>
|
|
236
|
+
|
|
237
|
+
{/* Price */}
|
|
238
|
+
<PriceDisplay
|
|
239
|
+
price={priceInfo.originalPrice}
|
|
240
|
+
salePrice={priceInfo.isOnSale ? priceInfo.price : undefined}
|
|
241
|
+
size="lg"
|
|
242
|
+
/>
|
|
243
|
+
|
|
244
|
+
{/* Stock */}
|
|
245
|
+
<StockBadge inventory={inventory} lowStockThreshold={5} />
|
|
246
|
+
|
|
247
|
+
{/* Variant Selector */}
|
|
248
|
+
{product.type === 'VARIABLE' && product.variants && product.variants.length > 0 && (
|
|
249
|
+
<VariantSelector
|
|
250
|
+
product={product}
|
|
251
|
+
selectedVariant={selectedVariant}
|
|
252
|
+
onVariantChange={setSelectedVariant}
|
|
253
|
+
/>
|
|
254
|
+
)}
|
|
255
|
+
|
|
256
|
+
{/* Quantity + Add to Cart */}
|
|
257
|
+
<div className="flex items-center gap-4">
|
|
258
|
+
<div className="border-border flex items-center rounded border">
|
|
259
|
+
<button
|
|
260
|
+
type="button"
|
|
261
|
+
onClick={() => setQuantity((q) => Math.max(1, q - 1))}
|
|
262
|
+
className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
|
|
263
|
+
aria-label="Decrease quantity"
|
|
264
|
+
>
|
|
265
|
+
-
|
|
266
|
+
</button>
|
|
267
|
+
<span className="text-foreground min-w-[3rem] px-4 py-2 text-center text-sm font-medium">
|
|
268
|
+
{quantity}
|
|
269
|
+
</span>
|
|
270
|
+
<button
|
|
271
|
+
type="button"
|
|
272
|
+
onClick={() => setQuantity((q) => q + 1)}
|
|
273
|
+
className="text-foreground hover:bg-muted px-3 py-2 transition-colors"
|
|
274
|
+
aria-label="Increase quantity"
|
|
275
|
+
>
|
|
276
|
+
+
|
|
277
|
+
</button>
|
|
278
|
+
</div>
|
|
279
|
+
|
|
280
|
+
<button
|
|
281
|
+
type="button"
|
|
282
|
+
onClick={handleAddToCart}
|
|
283
|
+
disabled={!canPurchase || addingToCart}
|
|
284
|
+
className={cn(
|
|
285
|
+
'flex-1 rounded px-6 py-3 text-sm font-medium transition-all',
|
|
286
|
+
canPurchase
|
|
287
|
+
? 'bg-primary text-primary-foreground hover:opacity-90'
|
|
288
|
+
: 'bg-muted text-muted-foreground cursor-not-allowed'
|
|
289
|
+
)}
|
|
290
|
+
>
|
|
291
|
+
{addingToCart ? (
|
|
292
|
+
<span className="inline-flex items-center gap-2">
|
|
293
|
+
<LoadingSpinner
|
|
294
|
+
size="sm"
|
|
295
|
+
className="border-primary-foreground/30 border-t-primary-foreground"
|
|
296
|
+
/>
|
|
297
|
+
Adding...
|
|
298
|
+
</span>
|
|
299
|
+
) : addedMessage ? (
|
|
300
|
+
'Added to Cart!'
|
|
301
|
+
) : !canPurchase ? (
|
|
302
|
+
'Out of Stock'
|
|
303
|
+
) : (
|
|
304
|
+
'Add to Cart'
|
|
305
|
+
)}
|
|
306
|
+
</button>
|
|
307
|
+
</div>
|
|
308
|
+
|
|
309
|
+
{/* Description */}
|
|
310
|
+
{description && (
|
|
311
|
+
<div className="border-border border-t pt-4">
|
|
312
|
+
<h2 className="text-foreground mb-3 text-lg font-semibold">Description</h2>
|
|
313
|
+
{'html' in description ? (
|
|
314
|
+
<div
|
|
315
|
+
className="prose prose-sm text-muted-foreground max-w-none"
|
|
316
|
+
dangerouslySetInnerHTML={{ __html: description.html }}
|
|
317
|
+
/>
|
|
318
|
+
) : (
|
|
319
|
+
<p className="text-muted-foreground whitespace-pre-wrap">{description.text}</p>
|
|
320
|
+
)}
|
|
321
|
+
</div>
|
|
322
|
+
)}
|
|
323
|
+
|
|
324
|
+
{/* Metafields / Specifications */}
|
|
325
|
+
{product.metafields && product.metafields.length > 0 && (
|
|
326
|
+
<div className="border-border border-t pt-4">
|
|
327
|
+
<h2 className="text-foreground mb-3 text-lg font-semibold">Specifications</h2>
|
|
328
|
+
<table className="w-full text-sm">
|
|
329
|
+
<tbody>
|
|
330
|
+
{product.metafields.map((field) => (
|
|
331
|
+
<tr key={field.id} className="border-border border-b last:border-0">
|
|
332
|
+
<td className="text-foreground whitespace-nowrap py-2 pe-4 font-medium">
|
|
333
|
+
{field.definitionName}
|
|
334
|
+
</td>
|
|
335
|
+
<td className="text-muted-foreground py-2">{field.value}</td>
|
|
336
|
+
</tr>
|
|
337
|
+
))}
|
|
338
|
+
</tbody>
|
|
339
|
+
</table>
|
|
340
|
+
</div>
|
|
341
|
+
)}
|
|
342
|
+
</div>
|
|
343
|
+
</div>
|
|
344
|
+
</div>
|
|
345
|
+
);
|
|
346
|
+
}
|