@forgecart/cli 1.0.0 → 1.1.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/LICENSE +21 -0
- package/README.md +7 -7
- package/bin/forgecart.js +1 -1
- package/dist/package.json +50 -0
- package/dist/src/cli.d.ts.map +1 -0
- package/dist/{cli.js → src/cli.js} +8 -6
- package/dist/src/cli.js.map +1 -0
- package/dist/src/commands/delete.d.ts.map +1 -0
- package/dist/{commands → src/commands}/delete.js +8 -3
- package/dist/src/commands/delete.js.map +1 -0
- package/dist/src/commands/deploy.d.ts.map +1 -0
- package/dist/src/commands/deploy.js +243 -0
- package/dist/src/commands/deploy.js.map +1 -0
- package/dist/src/commands/env.d.ts.map +1 -0
- package/dist/{commands → src/commands}/env.js +70 -12
- package/dist/src/commands/env.js.map +1 -0
- package/dist/src/commands/init.d.ts.map +1 -0
- package/dist/{commands → src/commands}/init.js +103 -17
- package/dist/src/commands/init.js.map +1 -0
- package/dist/src/commands/list.d.ts.map +1 -0
- package/dist/{commands → src/commands}/list.js +6 -2
- package/dist/src/commands/list.js.map +1 -0
- package/dist/src/commands/login.d.ts +3 -0
- package/dist/src/commands/login.d.ts.map +1 -0
- package/dist/src/commands/login.js +120 -0
- package/dist/src/commands/login.js.map +1 -0
- package/dist/src/commands/logs.d.ts.map +1 -0
- package/dist/src/commands/logs.js +88 -0
- package/dist/src/commands/logs.js.map +1 -0
- package/dist/src/commands/scale.d.ts.map +1 -0
- package/dist/{commands → src/commands}/scale.js +7 -3
- package/dist/src/commands/scale.js.map +1 -0
- package/dist/src/commands/status.d.ts.map +1 -0
- package/dist/{commands → src/commands}/status.js +7 -2
- package/dist/src/commands/status.js.map +1 -0
- package/dist/src/commands/whoami.d.ts.map +1 -0
- package/dist/{commands → src/commands}/whoami.js +20 -7
- package/dist/src/commands/whoami.js.map +1 -0
- package/dist/{index.d.ts.map → src/index.d.ts.map} +1 -1
- package/dist/src/index.js.map +1 -0
- package/dist/src/lib/api-client.d.ts +34 -0
- package/dist/src/lib/api-client.d.ts.map +1 -0
- package/dist/src/lib/api-client.js +141 -0
- package/dist/src/lib/api-client.js.map +1 -0
- package/dist/{lib → src/lib}/archive.d.ts +2 -1
- package/dist/src/lib/archive.d.ts.map +1 -0
- package/dist/{lib → src/lib}/archive.js +27 -5
- package/dist/src/lib/archive.js.map +1 -0
- package/dist/src/lib/config-updater.d.ts +10 -0
- package/dist/src/lib/config-updater.d.ts.map +1 -0
- package/dist/src/lib/config-updater.js +34 -0
- package/dist/src/lib/config-updater.js.map +1 -0
- package/dist/src/lib/config.d.ts +53 -0
- package/dist/src/lib/config.d.ts.map +1 -0
- package/dist/src/lib/config.js +162 -0
- package/dist/src/lib/config.js.map +1 -0
- package/dist/src/lib/env-sync.d.ts +44 -0
- package/dist/src/lib/env-sync.d.ts.map +1 -0
- package/dist/src/lib/env-sync.js +207 -0
- package/dist/src/lib/env-sync.js.map +1 -0
- package/dist/src/lib/logger.d.ts.map +1 -0
- package/dist/src/lib/logger.js.map +1 -0
- package/dist/src/lib/nx-generator.d.ts +22 -0
- package/dist/src/lib/nx-generator.d.ts.map +1 -0
- package/dist/src/lib/nx-generator.js +345 -0
- package/dist/src/lib/nx-generator.js.map +1 -0
- package/dist/src/lib/standalone.d.ts +28 -0
- package/dist/src/lib/standalone.d.ts.map +1 -0
- package/dist/src/lib/standalone.js +107 -0
- package/dist/src/lib/standalone.js.map +1 -0
- package/dist/{types → src/types}/index.d.ts +10 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/utils/spinner.d.ts +16 -0
- package/dist/src/utils/spinner.d.ts.map +1 -0
- package/dist/src/utils/spinner.js +88 -0
- package/dist/src/utils/spinner.js.map +1 -0
- package/dist/src/utils/validation.d.ts.map +1 -0
- package/dist/{utils → src/utils}/validation.js +18 -2
- package/dist/src/utils/validation.js.map +1 -0
- package/dist/templates/application-additions/.forgecartignore +1 -1
- package/dist/templates/application-additions/Procfile +1 -1
- package/dist/templates/application-additions/src/app/app.module.ts +16 -0
- package/dist/templates/application-additions/src/services/forgecart.service.ts +62 -0
- package/dist/templates/application-additions/src/services/index.ts +2 -0
- package/dist/templates/application-additions/src/services/product-event-listener.service.ts +150 -0
- package/dist/templates/application-e2e-additions/src/support/global-setup.ts +16 -0
- package/dist/templates/application-e2e-additions/src/support/test-setup.ts +9 -0
- package/dist/templates/storefront-additions/.forgecartignore +30 -6
- package/dist/templates/storefront-additions/Procfile +1 -1
- package/dist/templates/storefront-additions/package.json +11 -0
- package/dist/templates/storefront-additions/postcss.config.js +5 -0
- package/dist/templates/storefront-additions/src/app/cart/page.tsx +77 -0
- package/dist/templates/storefront-additions/src/app/global.css +1 -0
- package/dist/templates/storefront-additions/src/app/layout.tsx +25 -0
- package/dist/templates/storefront-additions/src/app/page.tsx +43 -0
- package/dist/templates/storefront-additions/src/components/AddToCartButton.tsx +29 -0
- package/dist/templates/storefront-additions/src/components/CartItem.tsx +82 -0
- package/dist/templates/storefront-additions/src/components/Header.tsx +42 -0
- package/dist/templates/storefront-additions/src/components/ProductCard.tsx +57 -0
- package/dist/templates/storefront-additions/src/components/QuantitySelector.tsx +35 -0
- package/dist/templates/storefront-additions/src/context/CartContext.tsx +158 -0
- package/dist/templates/storefront-additions/src/lib/forgecart.ts +19 -8
- package/dist/templates/storefront-additions/tailwind.config.js +8 -0
- package/dist/templates/storefront-e2e-additions/playwright.config.ts +26 -0
- package/dist/templates/storefront-e2e-additions/src/example.spec.ts +11 -0
- package/dist/templates/workspace/.nxignore +2 -0
- package/package.json +6 -28
- package/templates/application-additions/.forgecartignore +1 -1
- package/templates/application-additions/Procfile +1 -1
- package/templates/application-additions/src/app/app.module.ts +16 -0
- package/templates/application-additions/src/services/forgecart.service.ts +62 -0
- package/templates/application-additions/src/services/index.ts +2 -0
- package/templates/application-additions/src/services/product-event-listener.service.ts +150 -0
- package/templates/application-e2e-additions/src/support/global-setup.ts +16 -0
- package/templates/application-e2e-additions/src/support/test-setup.ts +9 -0
- package/templates/storefront-additions/.forgecartignore +30 -6
- package/templates/storefront-additions/Procfile +1 -1
- package/templates/storefront-additions/package.json +11 -0
- package/templates/storefront-additions/postcss.config.js +5 -0
- package/templates/storefront-additions/src/app/cart/page.tsx +77 -0
- package/templates/storefront-additions/src/app/global.css +1 -0
- package/templates/storefront-additions/src/app/layout.tsx +25 -0
- package/templates/storefront-additions/src/app/page.tsx +43 -0
- package/templates/storefront-additions/src/components/AddToCartButton.tsx +29 -0
- package/templates/storefront-additions/src/components/CartItem.tsx +82 -0
- package/templates/storefront-additions/src/components/Header.tsx +42 -0
- package/templates/storefront-additions/src/components/ProductCard.tsx +57 -0
- package/templates/storefront-additions/src/components/QuantitySelector.tsx +35 -0
- package/templates/storefront-additions/src/context/CartContext.tsx +158 -0
- package/templates/storefront-additions/src/lib/forgecart.ts +19 -8
- package/templates/storefront-additions/tailwind.config.js +8 -0
- package/templates/storefront-e2e-additions/playwright.config.ts +26 -0
- package/templates/storefront-e2e-additions/src/example.spec.ts +11 -0
- package/templates/workspace/.nxignore +2 -0
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/commands/delete.d.ts.map +0 -1
- package/dist/commands/delete.js.map +0 -1
- package/dist/commands/deploy.d.ts.map +0 -1
- package/dist/commands/deploy.js +0 -128
- package/dist/commands/deploy.js.map +0 -1
- package/dist/commands/env.d.ts.map +0 -1
- package/dist/commands/env.js.map +0 -1
- package/dist/commands/init.d.ts.map +0 -1
- package/dist/commands/init.js.map +0 -1
- package/dist/commands/list.d.ts.map +0 -1
- package/dist/commands/list.js.map +0 -1
- package/dist/commands/logs.d.ts.map +0 -1
- package/dist/commands/logs.js +0 -105
- package/dist/commands/logs.js.map +0 -1
- package/dist/commands/scale.d.ts.map +0 -1
- package/dist/commands/scale.js.map +0 -1
- package/dist/commands/status.d.ts.map +0 -1
- package/dist/commands/status.js.map +0 -1
- package/dist/commands/whoami.d.ts.map +0 -1
- package/dist/commands/whoami.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lib/api-client.d.ts +0 -21
- package/dist/lib/api-client.d.ts.map +0 -1
- package/dist/lib/api-client.js +0 -220
- package/dist/lib/api-client.js.map +0 -1
- package/dist/lib/archive.d.ts.map +0 -1
- package/dist/lib/archive.js.map +0 -1
- package/dist/lib/config.d.ts +0 -12
- package/dist/lib/config.d.ts.map +0 -1
- package/dist/lib/config.js +0 -79
- package/dist/lib/config.js.map +0 -1
- package/dist/lib/logger.d.ts.map +0 -1
- package/dist/lib/logger.js.map +0 -1
- package/dist/lib/nx-generator.d.ts +0 -8
- package/dist/lib/nx-generator.d.ts.map +0 -1
- package/dist/lib/nx-generator.js +0 -146
- package/dist/lib/nx-generator.js.map +0 -1
- package/dist/templates/application-additions/src/services/price-fetcher.service.ts +0 -72
- package/dist/templates/storefront-additions/src/app/products/page.tsx +0 -78
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js.map +0 -1
- package/dist/utils/spinner.d.ts +0 -4
- package/dist/utils/spinner.d.ts.map +0 -1
- package/dist/utils/spinner.js +0 -20
- package/dist/utils/spinner.js.map +0 -1
- package/dist/utils/validation.d.ts.map +0 -1
- package/dist/utils/validation.js.map +0 -1
- package/templates/application-additions/src/services/price-fetcher.service.ts +0 -72
- package/templates/storefront-additions/src/app/products/page.tsx +0 -78
- /package/dist/{cli.d.ts → src/cli.d.ts} +0 -0
- /package/dist/{commands → src/commands}/delete.d.ts +0 -0
- /package/dist/{commands → src/commands}/deploy.d.ts +0 -0
- /package/dist/{commands → src/commands}/env.d.ts +0 -0
- /package/dist/{commands → src/commands}/init.d.ts +0 -0
- /package/dist/{commands → src/commands}/list.d.ts +0 -0
- /package/dist/{commands → src/commands}/logs.d.ts +0 -0
- /package/dist/{commands → src/commands}/scale.d.ts +0 -0
- /package/dist/{commands → src/commands}/status.d.ts +0 -0
- /package/dist/{commands → src/commands}/whoami.d.ts +0 -0
- /package/dist/{index.d.ts → src/index.d.ts} +0 -0
- /package/dist/{index.js → src/index.js} +0 -0
- /package/dist/{lib → src/lib}/logger.d.ts +0 -0
- /package/dist/{lib → src/lib}/logger.js +0 -0
- /package/dist/{types → src/types}/index.js +0 -0
- /package/dist/{utils → src/utils}/validation.d.ts +0 -0
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ForgeCart deployment ignore file
|
|
2
2
|
# Files and directories listed here will not be uploaded during deployment
|
|
3
3
|
|
|
4
|
-
# Dependencies (
|
|
5
|
-
node_modules/
|
|
4
|
+
# Dependencies - only exclude root node_modules (standalone has its own)
|
|
5
|
+
/node_modules/
|
|
6
6
|
|
|
7
7
|
# Development files
|
|
8
8
|
.git/
|
|
@@ -18,9 +18,17 @@ node_modules/
|
|
|
18
18
|
*.swp
|
|
19
19
|
*.swo
|
|
20
20
|
|
|
21
|
-
# Build artifacts (
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
# Build artifacts - exclude root level only (standalone has nested .next that must be kept)
|
|
22
|
+
/.next/cache/
|
|
23
|
+
/.next/server/
|
|
24
|
+
/.next/types/
|
|
25
|
+
/.next/trace
|
|
26
|
+
/.next/trace-build
|
|
27
|
+
/.next/dev/
|
|
28
|
+
/.next/build/
|
|
29
|
+
/.next/diagnostics/
|
|
30
|
+
# Keep .next/standalone/ and everything inside it
|
|
31
|
+
/out/
|
|
24
32
|
|
|
25
33
|
# Test files
|
|
26
34
|
__tests__/
|
|
@@ -29,6 +37,22 @@ __tests__/
|
|
|
29
37
|
*.spec.ts
|
|
30
38
|
*.spec.tsx
|
|
31
39
|
coverage/
|
|
40
|
+
specs/
|
|
41
|
+
|
|
42
|
+
# Source files - not needed for runtime
|
|
43
|
+
src/
|
|
44
|
+
|
|
45
|
+
# Config files not needed at runtime
|
|
46
|
+
tsconfig.json
|
|
47
|
+
tsconfig.spec.json
|
|
48
|
+
project.json
|
|
49
|
+
jest.config.cts
|
|
50
|
+
eslint.config.mjs
|
|
51
|
+
postcss.config.js
|
|
52
|
+
tailwind.config.js
|
|
53
|
+
.swcrc
|
|
54
|
+
index.d.ts
|
|
55
|
+
next-env.d.ts
|
|
32
56
|
|
|
33
57
|
# Documentation
|
|
34
58
|
README.md
|
|
@@ -1 +1 @@
|
|
|
1
|
-
web:
|
|
1
|
+
web: node .next/standalone/apps/storefront/server.js
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import Link from 'next/link'
|
|
4
|
+
import { useCart } from '../../context/CartContext'
|
|
5
|
+
import { CartItem } from '../../components/CartItem'
|
|
6
|
+
|
|
7
|
+
export default function CartPage() {
|
|
8
|
+
const { cart, loading, itemCount } = useCart()
|
|
9
|
+
|
|
10
|
+
if (loading && !cart) {
|
|
11
|
+
return (
|
|
12
|
+
<div className="max-w-4xl mx-auto px-4 py-8">
|
|
13
|
+
<h1 className="text-3xl font-bold mb-8 text-gray-900">Your Cart</h1>
|
|
14
|
+
<p className="text-gray-600">Loading cart...</p>
|
|
15
|
+
</div>
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!cart || cart.lines.length === 0) {
|
|
20
|
+
return (
|
|
21
|
+
<div className="max-w-4xl mx-auto px-4 py-8">
|
|
22
|
+
<h1 className="text-3xl font-bold mb-8 text-gray-900">Your Cart</h1>
|
|
23
|
+
<p className="text-gray-600 mb-4">Your cart is empty.</p>
|
|
24
|
+
<Link
|
|
25
|
+
href="/"
|
|
26
|
+
className="inline-block bg-blue-600 text-white py-2 px-4 rounded-lg hover:bg-blue-700"
|
|
27
|
+
>
|
|
28
|
+
Continue Shopping
|
|
29
|
+
</Link>
|
|
30
|
+
</div>
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const subtotal = (cart.subTotalWithTax / 100).toFixed(2)
|
|
35
|
+
const total = (cart.totalWithTax / 100).toFixed(2)
|
|
36
|
+
|
|
37
|
+
return (
|
|
38
|
+
<div className="max-w-4xl mx-auto px-4 py-8">
|
|
39
|
+
<h1 className="text-3xl font-bold mb-8 text-gray-900">Your Cart</h1>
|
|
40
|
+
|
|
41
|
+
<div className="bg-white rounded-lg shadow p-6">
|
|
42
|
+
{cart.lines
|
|
43
|
+
.filter((line) => line.productVariant)
|
|
44
|
+
.map((line) => (
|
|
45
|
+
<CartItem key={line.id} line={line as never} />
|
|
46
|
+
))}
|
|
47
|
+
|
|
48
|
+
<div className="mt-6 pt-4 border-t">
|
|
49
|
+
<div className="flex justify-between text-lg mb-2">
|
|
50
|
+
<span className="text-gray-600">Subtotal ({itemCount} items)</span>
|
|
51
|
+
<span className="font-semibold">
|
|
52
|
+
{subtotal} {cart.currencyCode}
|
|
53
|
+
</span>
|
|
54
|
+
</div>
|
|
55
|
+
<div className="flex justify-between text-xl font-bold">
|
|
56
|
+
<span>Total</span>
|
|
57
|
+
<span>
|
|
58
|
+
{total} {cart.currencyCode}
|
|
59
|
+
</span>
|
|
60
|
+
</div>
|
|
61
|
+
</div>
|
|
62
|
+
|
|
63
|
+
<div className="mt-6 flex gap-4">
|
|
64
|
+
<Link
|
|
65
|
+
href="/"
|
|
66
|
+
className="flex-1 text-center border border-gray-300 py-3 rounded-lg hover:bg-gray-50"
|
|
67
|
+
>
|
|
68
|
+
Continue Shopping
|
|
69
|
+
</Link>
|
|
70
|
+
<button className="flex-1 bg-blue-600 text-white py-3 rounded-lg hover:bg-blue-700">
|
|
71
|
+
Checkout
|
|
72
|
+
</button>
|
|
73
|
+
</div>
|
|
74
|
+
</div>
|
|
75
|
+
</div>
|
|
76
|
+
)
|
|
77
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@import "tailwindcss";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import './global.css'
|
|
2
|
+
import { CartProvider } from '../context/CartContext'
|
|
3
|
+
import { Header } from '../components/Header'
|
|
4
|
+
|
|
5
|
+
export const metadata = {
|
|
6
|
+
title: 'Store',
|
|
7
|
+
description: 'Your ecommerce store',
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default function RootLayout({
|
|
11
|
+
children,
|
|
12
|
+
}: {
|
|
13
|
+
children: React.ReactNode
|
|
14
|
+
}) {
|
|
15
|
+
return (
|
|
16
|
+
<html lang="en">
|
|
17
|
+
<body className="bg-gray-50 min-h-screen">
|
|
18
|
+
<CartProvider>
|
|
19
|
+
<Header />
|
|
20
|
+
<main>{children}</main>
|
|
21
|
+
</CartProvider>
|
|
22
|
+
</body>
|
|
23
|
+
</html>
|
|
24
|
+
)
|
|
25
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ProductList, Product } from '@forgecart/sdk/shop'
|
|
2
|
+
import forgecart from '../lib/forgecart'
|
|
3
|
+
import { ProductCard } from '../components/ProductCard'
|
|
4
|
+
|
|
5
|
+
async function getProducts(): Promise<ProductList> {
|
|
6
|
+
try {
|
|
7
|
+
const result = await forgecart.shop.products.products({
|
|
8
|
+
options: {
|
|
9
|
+
take: 20,
|
|
10
|
+
},
|
|
11
|
+
})
|
|
12
|
+
return (result.products as ProductList) || { items: [], totalItems: 0 }
|
|
13
|
+
} catch (error) {
|
|
14
|
+
console.error('Failed to fetch products:', error)
|
|
15
|
+
return {
|
|
16
|
+
items: [] as Product[],
|
|
17
|
+
totalItems: 0,
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default async function HomePage() {
|
|
23
|
+
const products = await getProducts()
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<div className="max-w-7xl mx-auto px-4 py-8">
|
|
27
|
+
<h1 className="text-3xl font-bold mb-8 text-gray-900">Our Products</h1>
|
|
28
|
+
|
|
29
|
+
{products.items.length === 0 ? (
|
|
30
|
+
<p className="text-gray-600">
|
|
31
|
+
No products found. Make sure your ForgeCart channel is configured
|
|
32
|
+
correctly.
|
|
33
|
+
</p>
|
|
34
|
+
) : (
|
|
35
|
+
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-6">
|
|
36
|
+
{products.items.map((product: Product) => (
|
|
37
|
+
<ProductCard key={product.id} product={product as never} />
|
|
38
|
+
))}
|
|
39
|
+
</div>
|
|
40
|
+
)}
|
|
41
|
+
</div>
|
|
42
|
+
)
|
|
43
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useState } from 'react'
|
|
4
|
+
import { useCart } from '../context/CartContext'
|
|
5
|
+
|
|
6
|
+
interface AddToCartButtonProps {
|
|
7
|
+
productVariantId: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function AddToCartButton({ productVariantId }: AddToCartButtonProps) {
|
|
11
|
+
const { addToCart } = useCart()
|
|
12
|
+
const [isAdding, setIsAdding] = useState(false)
|
|
13
|
+
|
|
14
|
+
const handleClick = async () => {
|
|
15
|
+
setIsAdding(true)
|
|
16
|
+
await addToCart(productVariantId, 1)
|
|
17
|
+
setIsAdding(false)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<button
|
|
22
|
+
onClick={handleClick}
|
|
23
|
+
disabled={isAdding}
|
|
24
|
+
className="w-full bg-blue-600 text-white py-2 px-4 rounded-lg hover:bg-blue-700 disabled:bg-blue-300 disabled:cursor-not-allowed transition-colors"
|
|
25
|
+
>
|
|
26
|
+
{isAdding ? 'Adding...' : 'Add to Cart'}
|
|
27
|
+
</button>
|
|
28
|
+
)
|
|
29
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useCart } from '../context/CartContext'
|
|
4
|
+
import { QuantitySelector } from './QuantitySelector'
|
|
5
|
+
|
|
6
|
+
interface CartLine {
|
|
7
|
+
id: string
|
|
8
|
+
quantity: number
|
|
9
|
+
productVariant: {
|
|
10
|
+
id: string
|
|
11
|
+
name: string
|
|
12
|
+
price: number
|
|
13
|
+
priceWithTax: number
|
|
14
|
+
currencyCode: string
|
|
15
|
+
}
|
|
16
|
+
featuredAsset?: {
|
|
17
|
+
preview: string
|
|
18
|
+
}
|
|
19
|
+
unitPriceWithTax: number
|
|
20
|
+
linePriceWithTax: number
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface CartItemProps {
|
|
24
|
+
line: CartLine
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function CartItem({ line }: CartItemProps) {
|
|
28
|
+
const { updateQuantity, removeItem, loading } = useCart()
|
|
29
|
+
|
|
30
|
+
// Defensive check for incomplete line data
|
|
31
|
+
if (!line.productVariant) {
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const unitPrice = (line.unitPriceWithTax / 100).toFixed(2)
|
|
36
|
+
const lineTotal = (line.linePriceWithTax / 100).toFixed(2)
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<div className="flex gap-4 py-4 border-b">
|
|
40
|
+
{line.featuredAsset && (
|
|
41
|
+
<img
|
|
42
|
+
src={line.featuredAsset.preview}
|
|
43
|
+
alt={line.productVariant.name}
|
|
44
|
+
className="w-20 h-20 object-cover rounded"
|
|
45
|
+
/>
|
|
46
|
+
)}
|
|
47
|
+
{!line.featuredAsset && (
|
|
48
|
+
<div className="w-20 h-20 bg-gray-200 rounded flex items-center justify-center">
|
|
49
|
+
<span className="text-gray-400 text-xs">No img</span>
|
|
50
|
+
</div>
|
|
51
|
+
)}
|
|
52
|
+
<div className="flex-1">
|
|
53
|
+
<h3 className="font-semibold text-gray-900">
|
|
54
|
+
{line.productVariant.name}
|
|
55
|
+
</h3>
|
|
56
|
+
<p className="text-gray-600 text-sm">
|
|
57
|
+
{unitPrice} {line.productVariant.currencyCode} each
|
|
58
|
+
</p>
|
|
59
|
+
<div className="flex items-center justify-between mt-2">
|
|
60
|
+
<QuantitySelector
|
|
61
|
+
quantity={line.quantity}
|
|
62
|
+
onIncrease={() => updateQuantity(line.id, line.quantity + 1)}
|
|
63
|
+
onDecrease={() => updateQuantity(line.id, line.quantity - 1)}
|
|
64
|
+
disabled={loading}
|
|
65
|
+
/>
|
|
66
|
+
<button
|
|
67
|
+
onClick={() => removeItem(line.id)}
|
|
68
|
+
disabled={loading}
|
|
69
|
+
className="text-red-600 hover:text-red-800 text-sm disabled:opacity-50"
|
|
70
|
+
>
|
|
71
|
+
Remove
|
|
72
|
+
</button>
|
|
73
|
+
</div>
|
|
74
|
+
</div>
|
|
75
|
+
<div className="text-right">
|
|
76
|
+
<p className="font-bold text-gray-900">
|
|
77
|
+
{lineTotal} {line.productVariant.currencyCode}
|
|
78
|
+
</p>
|
|
79
|
+
</div>
|
|
80
|
+
</div>
|
|
81
|
+
)
|
|
82
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import Link from 'next/link'
|
|
4
|
+
import { useCart } from '../context/CartContext'
|
|
5
|
+
|
|
6
|
+
export function Header() {
|
|
7
|
+
const { itemCount, loading } = useCart()
|
|
8
|
+
|
|
9
|
+
return (
|
|
10
|
+
<header className="bg-white shadow-sm border-b">
|
|
11
|
+
<div className="max-w-7xl mx-auto px-4 py-4 flex justify-between items-center">
|
|
12
|
+
<Link href="/" className="text-xl font-bold text-gray-900">
|
|
13
|
+
Store
|
|
14
|
+
</Link>
|
|
15
|
+
<Link
|
|
16
|
+
href="/cart"
|
|
17
|
+
className="relative flex items-center gap-2 text-gray-700 hover:text-gray-900"
|
|
18
|
+
>
|
|
19
|
+
<svg
|
|
20
|
+
className="w-6 h-6"
|
|
21
|
+
fill="none"
|
|
22
|
+
stroke="currentColor"
|
|
23
|
+
viewBox="0 0 24 24"
|
|
24
|
+
>
|
|
25
|
+
<path
|
|
26
|
+
strokeLinecap="round"
|
|
27
|
+
strokeLinejoin="round"
|
|
28
|
+
strokeWidth={2}
|
|
29
|
+
d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17m0 0a2 2 0 100 4 2 2 0 000-4zm-8 2a2 2 0 11-4 0 2 2 0 014 0z"
|
|
30
|
+
/>
|
|
31
|
+
</svg>
|
|
32
|
+
<span>Cart</span>
|
|
33
|
+
{itemCount > 0 && (
|
|
34
|
+
<span className="absolute -top-2 -right-2 bg-blue-600 text-white text-xs rounded-full w-5 h-5 flex items-center justify-center">
|
|
35
|
+
{loading ? '...' : itemCount}
|
|
36
|
+
</span>
|
|
37
|
+
)}
|
|
38
|
+
</Link>
|
|
39
|
+
</div>
|
|
40
|
+
</header>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { AddToCartButton } from './AddToCartButton'
|
|
2
|
+
|
|
3
|
+
interface ProductVariant {
|
|
4
|
+
id: string
|
|
5
|
+
price: number
|
|
6
|
+
priceWithTax: number
|
|
7
|
+
currencyCode: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface Product {
|
|
11
|
+
id: string
|
|
12
|
+
name: string
|
|
13
|
+
description: string
|
|
14
|
+
featuredAsset?: {
|
|
15
|
+
preview: string
|
|
16
|
+
}
|
|
17
|
+
variants: ProductVariant[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface ProductCardProps {
|
|
21
|
+
product: Product
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function ProductCard({ product }: ProductCardProps) {
|
|
25
|
+
const variant = product.variants[0]
|
|
26
|
+
const price = variant ? (variant.priceWithTax / 100).toFixed(2) : '0.00'
|
|
27
|
+
const currencyCode = variant?.currencyCode ?? 'USD'
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<div className="bg-white border rounded-lg overflow-hidden shadow-sm hover:shadow-md transition-shadow">
|
|
31
|
+
{product.featuredAsset && (
|
|
32
|
+
<img
|
|
33
|
+
src={product.featuredAsset.preview}
|
|
34
|
+
alt={product.name}
|
|
35
|
+
className="w-full h-48 object-cover"
|
|
36
|
+
/>
|
|
37
|
+
)}
|
|
38
|
+
{!product.featuredAsset && (
|
|
39
|
+
<div className="w-full h-48 bg-gray-200 flex items-center justify-center">
|
|
40
|
+
<span className="text-gray-400">No image</span>
|
|
41
|
+
</div>
|
|
42
|
+
)}
|
|
43
|
+
<div className="p-4">
|
|
44
|
+
<h2 className="text-lg font-semibold mb-2 text-gray-900">
|
|
45
|
+
{product.name}
|
|
46
|
+
</h2>
|
|
47
|
+
<p className="text-gray-600 text-sm mb-3 line-clamp-2">
|
|
48
|
+
{product.description}
|
|
49
|
+
</p>
|
|
50
|
+
<p className="text-lg font-bold text-blue-600 mb-4">
|
|
51
|
+
{price} {currencyCode}
|
|
52
|
+
</p>
|
|
53
|
+
{variant && <AddToCartButton productVariantId={variant.id} />}
|
|
54
|
+
</div>
|
|
55
|
+
</div>
|
|
56
|
+
)
|
|
57
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
interface QuantitySelectorProps {
|
|
4
|
+
quantity: number
|
|
5
|
+
onIncrease: () => void
|
|
6
|
+
onDecrease: () => void
|
|
7
|
+
disabled?: boolean
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function QuantitySelector({
|
|
11
|
+
quantity,
|
|
12
|
+
onIncrease,
|
|
13
|
+
onDecrease,
|
|
14
|
+
disabled,
|
|
15
|
+
}: QuantitySelectorProps) {
|
|
16
|
+
return (
|
|
17
|
+
<div className="flex items-center gap-2">
|
|
18
|
+
<button
|
|
19
|
+
onClick={onDecrease}
|
|
20
|
+
disabled={disabled || quantity <= 1}
|
|
21
|
+
className="w-8 h-8 flex items-center justify-center border rounded-md hover:bg-gray-100 disabled:opacity-50 disabled:cursor-not-allowed"
|
|
22
|
+
>
|
|
23
|
+
-
|
|
24
|
+
</button>
|
|
25
|
+
<span className="w-8 text-center">{quantity}</span>
|
|
26
|
+
<button
|
|
27
|
+
onClick={onIncrease}
|
|
28
|
+
disabled={disabled}
|
|
29
|
+
className="w-8 h-8 flex items-center justify-center border rounded-md hover:bg-gray-100 disabled:opacity-50 disabled:cursor-not-allowed"
|
|
30
|
+
>
|
|
31
|
+
+
|
|
32
|
+
</button>
|
|
33
|
+
</div>
|
|
34
|
+
)
|
|
35
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
useContext,
|
|
6
|
+
useState,
|
|
7
|
+
useEffect,
|
|
8
|
+
useCallback,
|
|
9
|
+
ReactNode,
|
|
10
|
+
} from 'react'
|
|
11
|
+
import forgecart from '../lib/forgecart'
|
|
12
|
+
|
|
13
|
+
interface CartLine {
|
|
14
|
+
id: string
|
|
15
|
+
quantity: number
|
|
16
|
+
productVariant: {
|
|
17
|
+
id: string
|
|
18
|
+
name: string
|
|
19
|
+
price: number
|
|
20
|
+
priceWithTax: number
|
|
21
|
+
currencyCode: string
|
|
22
|
+
}
|
|
23
|
+
featuredAsset?: {
|
|
24
|
+
preview: string
|
|
25
|
+
}
|
|
26
|
+
unitPriceWithTax: number
|
|
27
|
+
linePriceWithTax: number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface Cart {
|
|
31
|
+
id: string
|
|
32
|
+
lines: CartLine[]
|
|
33
|
+
totalQuantity: number
|
|
34
|
+
subTotalWithTax: number
|
|
35
|
+
totalWithTax: number
|
|
36
|
+
currencyCode: string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface CartContextType {
|
|
40
|
+
cart: Cart | null
|
|
41
|
+
loading: boolean
|
|
42
|
+
itemCount: number
|
|
43
|
+
addToCart: (productVariantId: string, quantity: number) => Promise<void>
|
|
44
|
+
updateQuantity: (orderLineId: string, quantity: number) => Promise<void>
|
|
45
|
+
removeItem: (orderLineId: string) => Promise<void>
|
|
46
|
+
refreshCart: () => Promise<void>
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const CartContext = createContext<CartContextType | undefined>(undefined)
|
|
50
|
+
|
|
51
|
+
export function CartProvider({ children }: { children: ReactNode }) {
|
|
52
|
+
const [cart, setCart] = useState<Cart | null>(null)
|
|
53
|
+
const [loading, setLoading] = useState(true)
|
|
54
|
+
|
|
55
|
+
const refreshCart = useCallback(async () => {
|
|
56
|
+
try {
|
|
57
|
+
const result = await forgecart.shop.order.activeOrder()
|
|
58
|
+
if (result.activeOrder) {
|
|
59
|
+
setCart(result.activeOrder as unknown as Cart)
|
|
60
|
+
} else {
|
|
61
|
+
setCart(null)
|
|
62
|
+
}
|
|
63
|
+
} catch (error) {
|
|
64
|
+
console.error('Failed to fetch cart:', error)
|
|
65
|
+
} finally {
|
|
66
|
+
setLoading(false)
|
|
67
|
+
}
|
|
68
|
+
}, [])
|
|
69
|
+
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
refreshCart()
|
|
72
|
+
}, [refreshCart])
|
|
73
|
+
|
|
74
|
+
const addToCart = async (productVariantId: string, quantity: number) => {
|
|
75
|
+
setLoading(true)
|
|
76
|
+
try {
|
|
77
|
+
const result = await forgecart.shop.order.addItemToOrder({
|
|
78
|
+
productVariantId,
|
|
79
|
+
quantity,
|
|
80
|
+
})
|
|
81
|
+
if (
|
|
82
|
+
result.addItemToOrder &&
|
|
83
|
+
result.addItemToOrder.__typename === 'Order'
|
|
84
|
+
) {
|
|
85
|
+
setCart(result.addItemToOrder as unknown as Cart)
|
|
86
|
+
}
|
|
87
|
+
} catch (error) {
|
|
88
|
+
console.error('Failed to add item:', error)
|
|
89
|
+
} finally {
|
|
90
|
+
setLoading(false)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const updateQuantity = async (orderLineId: string, quantity: number) => {
|
|
95
|
+
setLoading(true)
|
|
96
|
+
try {
|
|
97
|
+
const result = await forgecart.shop.order.adjustOrderLine({
|
|
98
|
+
orderLineId,
|
|
99
|
+
quantity,
|
|
100
|
+
})
|
|
101
|
+
if (
|
|
102
|
+
result.adjustOrderLine &&
|
|
103
|
+
result.adjustOrderLine.__typename === 'Order'
|
|
104
|
+
) {
|
|
105
|
+
setCart(result.adjustOrderLine as unknown as Cart)
|
|
106
|
+
}
|
|
107
|
+
} catch (error) {
|
|
108
|
+
console.error('Failed to update quantity:', error)
|
|
109
|
+
} finally {
|
|
110
|
+
setLoading(false)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const removeItem = async (orderLineId: string) => {
|
|
115
|
+
setLoading(true)
|
|
116
|
+
try {
|
|
117
|
+
const result = await forgecart.shop.order.removeOrderLine({
|
|
118
|
+
orderLineId,
|
|
119
|
+
})
|
|
120
|
+
if (
|
|
121
|
+
result.removeOrderLine &&
|
|
122
|
+
result.removeOrderLine.__typename === 'Order'
|
|
123
|
+
) {
|
|
124
|
+
setCart(result.removeOrderLine as unknown as Cart)
|
|
125
|
+
}
|
|
126
|
+
} catch (error) {
|
|
127
|
+
console.error('Failed to remove item:', error)
|
|
128
|
+
} finally {
|
|
129
|
+
setLoading(false)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const itemCount = cart?.totalQuantity ?? 0
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
<CartContext.Provider
|
|
137
|
+
value={{
|
|
138
|
+
cart,
|
|
139
|
+
loading,
|
|
140
|
+
itemCount,
|
|
141
|
+
addToCart,
|
|
142
|
+
updateQuantity,
|
|
143
|
+
removeItem,
|
|
144
|
+
refreshCart,
|
|
145
|
+
}}
|
|
146
|
+
>
|
|
147
|
+
{children}
|
|
148
|
+
</CartContext.Provider>
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function useCart() {
|
|
153
|
+
const context = useContext(CartContext)
|
|
154
|
+
if (!context) {
|
|
155
|
+
throw new Error('useCart must be used within CartProvider')
|
|
156
|
+
}
|
|
157
|
+
return context
|
|
158
|
+
}
|
|
@@ -1,11 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ForgeCartSDK } from '@forgecart/sdk'
|
|
2
2
|
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
// Singleton instance of the ForgeCart SDK
|
|
4
|
+
// Initialized once to maintain socket connection
|
|
5
|
+
let sdkInstance: ForgeCartSDK | null = null
|
|
6
|
+
|
|
7
|
+
export function getForgeCartSDK(): ForgeCartSDK {
|
|
8
|
+
if (!sdkInstance) {
|
|
9
|
+
sdkInstance = new ForgeCartSDK({
|
|
10
|
+
endpoint:
|
|
11
|
+
process.env.NEXT_PUBLIC_FORGECART_API_URL ||
|
|
12
|
+
'https://api.forgecart.com',
|
|
13
|
+
token: process.env.NEXT_PUBLIC_FORGECART_CHANNEL_TOKEN || '',
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
return sdkInstance
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Export singleton instance for convenience
|
|
20
|
+
export const forgecart = getForgeCartSDK()
|
|
9
21
|
|
|
10
|
-
// Export for use in server components
|
|
11
22
|
export default forgecart
|