@hszrdm/create-commerce 0.0.0-stage → 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/README.md +50 -2
- package/bin/create-commerce.mjs +51 -0
- package/package.json +23 -4
- package/src/cli.mjs +585 -0
- package/templates/backend/.env.example +113 -0
- package/templates/backend/README.md +7 -0
- package/templates/backend/instrumentation.ts +24 -0
- package/templates/backend/medusa-config.ts +11 -0
- package/templates/backend/package.json +42 -0
- package/templates/backend/scripts/prepare-project.mjs +49 -0
- package/templates/backend/src/project/README.md +13 -0
- package/templates/backend/src/project/api/.gitkeep +0 -0
- package/templates/backend/src/project/jobs/.gitkeep +0 -0
- package/templates/backend/src/project/modules/.gitkeep +0 -0
- package/templates/backend/src/project/subscribers/.gitkeep +0 -0
- package/templates/backend/src/project/workflows/.gitkeep +0 -0
- package/templates/backend/src/scripts/core-init.ts +21 -0
- package/templates/backend/src/scripts/create-storefront-key.ts +12 -0
- package/templates/backend/src/scripts/verify-bootstrap.ts +1 -0
- package/templates/backend/tsconfig.json +33 -0
- package/templates/storefront/.env.example +5 -0
- package/templates/storefront/README.md +108 -0
- package/templates/storefront/next-env.d.ts +4 -0
- package/templates/storefront/next.config.ts +8 -0
- package/templates/storefront/package.json +24 -0
- package/templates/storefront/src/app/[countryCode]/categories/[handle]/page.tsx +38 -0
- package/templates/storefront/src/app/[countryCode]/products/[handle]/page.tsx +51 -0
- package/templates/storefront/src/app/[countryCode]/products/page.tsx +47 -0
- package/templates/storefront/src/app/account/orders/[id]/page.tsx +8 -0
- package/templates/storefront/src/app/account/page.tsx +11 -0
- package/templates/storefront/src/app/cart/page.tsx +12 -0
- package/templates/storefront/src/app/checkout/page.tsx +15 -0
- package/templates/storefront/src/app/checkout/success/page.tsx +72 -0
- package/templates/storefront/src/app/error.tsx +14 -0
- package/templates/storefront/src/app/globals.css +250 -0
- package/templates/storefront/src/app/layout.tsx +59 -0
- package/templates/storefront/src/app/legal/page.tsx +26 -0
- package/templates/storefront/src/app/loading.tsx +3 -0
- package/templates/storefront/src/app/not-found.tsx +14 -0
- package/templates/storefront/src/app/page.tsx +47 -0
- package/templates/storefront/src/app/reset-password/page.tsx +14 -0
- package/templates/storefront/src/app/robots.txt/route.ts +13 -0
- package/templates/storefront/src/app/sitemap.xml/route.ts +13 -0
- package/templates/storefront/src/components/account-dashboard.tsx +325 -0
- package/templates/storefront/src/components/add-to-cart.tsx +71 -0
- package/templates/storefront/src/components/analytics.tsx +90 -0
- package/templates/storefront/src/components/cart-provider.tsx +236 -0
- package/templates/storefront/src/components/cart-view.tsx +135 -0
- package/templates/storefront/src/components/checkout-flow.tsx +534 -0
- package/templates/storefront/src/components/consent-banner.tsx +131 -0
- package/templates/storefront/src/components/order-detail.tsx +273 -0
- package/templates/storefront/src/components/product-card.tsx +38 -0
- package/templates/storefront/src/components/product-view-tracker.tsx +37 -0
- package/templates/storefront/src/components/reset-password-form.tsx +65 -0
- package/templates/storefront/src/components/storefront-header.tsx +42 -0
- package/templates/storefront/src/components/stripe-payment.tsx +90 -0
- package/templates/storefront/src/lib/core/account.ts +170 -0
- package/templates/storefront/src/lib/core/after-sales.ts +50 -0
- package/templates/storefront/src/lib/core/catalog.ts +56 -0
- package/templates/storefront/src/lib/core/checkout.ts +330 -0
- package/templates/storefront/src/lib/core/client.ts +107 -0
- package/templates/storefront/src/lib/core/commerce.ts +148 -0
- package/templates/storefront/src/lib/core/consent.ts +111 -0
- package/templates/storefront/src/lib/core/legal.ts +33 -0
- package/templates/storefront/src/lib/core/seo.ts +88 -0
- package/templates/storefront/src/lib/core/tracking.ts +584 -0
- package/templates/storefront/src/lib/core/types.ts +123 -0
- package/templates/storefront/tsconfig.json +23 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
useCallback,
|
|
6
|
+
useContext,
|
|
7
|
+
useEffect,
|
|
8
|
+
useMemo,
|
|
9
|
+
useState,
|
|
10
|
+
type ReactNode,
|
|
11
|
+
} from "react"
|
|
12
|
+
import { checkoutApi } from "@/lib/core/checkout"
|
|
13
|
+
import { cartItemCount, type JsonRecord } from "@/lib/core/commerce"
|
|
14
|
+
import type { CoreConfig } from "@/lib/core/types"
|
|
15
|
+
|
|
16
|
+
const CART_STORAGE_KEY = "hszrdm_core_cart_id"
|
|
17
|
+
|
|
18
|
+
type CartContextValue = {
|
|
19
|
+
cart: JsonRecord | null
|
|
20
|
+
cartId: string | null
|
|
21
|
+
count: number
|
|
22
|
+
loading: boolean
|
|
23
|
+
mutating: boolean
|
|
24
|
+
error: string | null
|
|
25
|
+
drawerOpen: boolean
|
|
26
|
+
setDrawerOpen: (open: boolean) => void
|
|
27
|
+
ensureCart: () => Promise<string>
|
|
28
|
+
refresh: (id?: string) => Promise<JsonRecord | null>
|
|
29
|
+
adoptCart: (id: string) => Promise<JsonRecord | null>
|
|
30
|
+
addItem: (variantId: string, quantity?: number) => Promise<void>
|
|
31
|
+
updateItem: (lineId: string, quantity: number) => Promise<void>
|
|
32
|
+
removeItem: (lineId: string) => Promise<void>
|
|
33
|
+
addPromotion: (code: string) => Promise<void>
|
|
34
|
+
removePromotion: (code: string) => Promise<void>
|
|
35
|
+
transferToCustomer: () => Promise<void>
|
|
36
|
+
clearCart: () => void
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const CartContext = createContext<CartContextValue | null>(null)
|
|
40
|
+
|
|
41
|
+
export function CartProvider({
|
|
42
|
+
config,
|
|
43
|
+
children,
|
|
44
|
+
}: {
|
|
45
|
+
config: CoreConfig
|
|
46
|
+
children: ReactNode
|
|
47
|
+
}) {
|
|
48
|
+
const api = useMemo(() => checkoutApi(config), [config])
|
|
49
|
+
const [cart, setCart] = useState<JsonRecord | null>(null)
|
|
50
|
+
const [cartId, setCartId] = useState<string | null>(null)
|
|
51
|
+
const [loading, setLoading] = useState(true)
|
|
52
|
+
const [mutating, setMutating] = useState(false)
|
|
53
|
+
const [error, setError] = useState<string | null>(null)
|
|
54
|
+
const [drawerOpen, setDrawerOpen] = useState(false)
|
|
55
|
+
|
|
56
|
+
const storeCartId = useCallback((id: string | null) => {
|
|
57
|
+
setCartId(id)
|
|
58
|
+
if (typeof window === "undefined") return
|
|
59
|
+
if (id) window.localStorage.setItem(CART_STORAGE_KEY, id)
|
|
60
|
+
else window.localStorage.removeItem(CART_STORAGE_KEY)
|
|
61
|
+
}, [])
|
|
62
|
+
|
|
63
|
+
const refresh = useCallback(
|
|
64
|
+
async (id?: string) => {
|
|
65
|
+
const target = id || cartId
|
|
66
|
+
if (!target) {
|
|
67
|
+
setCart(null)
|
|
68
|
+
return null
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
const result = await api.getCart(target)
|
|
72
|
+
setCart(result.cart)
|
|
73
|
+
storeCartId(target)
|
|
74
|
+
setError(null)
|
|
75
|
+
return result.cart
|
|
76
|
+
} catch (cause) {
|
|
77
|
+
if (!id || id === cartId) {
|
|
78
|
+
setCart(null)
|
|
79
|
+
storeCartId(null)
|
|
80
|
+
}
|
|
81
|
+
setError(cause instanceof Error ? cause.message : "A kosár nem tölthető be.")
|
|
82
|
+
return null
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
[api, cartId, storeCartId],
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
const id = window.localStorage.getItem(CART_STORAGE_KEY)
|
|
90
|
+
if (!id) {
|
|
91
|
+
setLoading(false)
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
setCartId(id)
|
|
95
|
+
void api
|
|
96
|
+
.getCart(id)
|
|
97
|
+
.then((result) => {
|
|
98
|
+
setCart(result.cart)
|
|
99
|
+
setError(null)
|
|
100
|
+
})
|
|
101
|
+
.catch(() => {
|
|
102
|
+
window.localStorage.removeItem(CART_STORAGE_KEY)
|
|
103
|
+
setCartId(null)
|
|
104
|
+
setCart(null)
|
|
105
|
+
})
|
|
106
|
+
.finally(() => setLoading(false))
|
|
107
|
+
}, [api])
|
|
108
|
+
|
|
109
|
+
const ensureCart = useCallback(async () => {
|
|
110
|
+
if (cartId) return cartId
|
|
111
|
+
setMutating(true)
|
|
112
|
+
try {
|
|
113
|
+
const result = await api.createCart()
|
|
114
|
+
const id = String(result.cart.id || "")
|
|
115
|
+
if (!id) throw new Error("A Medusa nem adott vissza cart ID-t.")
|
|
116
|
+
setCart(result.cart)
|
|
117
|
+
storeCartId(id)
|
|
118
|
+
return id
|
|
119
|
+
} finally {
|
|
120
|
+
setMutating(false)
|
|
121
|
+
}
|
|
122
|
+
}, [api, cartId, storeCartId])
|
|
123
|
+
|
|
124
|
+
const adoptCart = useCallback(
|
|
125
|
+
async (id: string) => {
|
|
126
|
+
const normalized = id.trim()
|
|
127
|
+
if (!normalized) return null
|
|
128
|
+
const result = await api.getCart(normalized)
|
|
129
|
+
setCart(result.cart)
|
|
130
|
+
storeCartId(normalized)
|
|
131
|
+
setError(null)
|
|
132
|
+
return result.cart
|
|
133
|
+
},
|
|
134
|
+
[api, storeCartId],
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
const mutate = useCallback(
|
|
138
|
+
async (work: (id: string) => Promise<{ cart?: JsonRecord } | unknown>) => {
|
|
139
|
+
setMutating(true)
|
|
140
|
+
setError(null)
|
|
141
|
+
try {
|
|
142
|
+
const id = await ensureCart()
|
|
143
|
+
const result = await work(id)
|
|
144
|
+
const next = result && typeof result === "object" && "cart" in result
|
|
145
|
+
? (result as { cart?: JsonRecord }).cart
|
|
146
|
+
: null
|
|
147
|
+
if (next) setCart(next)
|
|
148
|
+
else await refresh(id)
|
|
149
|
+
} catch (cause) {
|
|
150
|
+
const message = cause instanceof Error ? cause.message : "A kosár módosítása nem sikerült."
|
|
151
|
+
setError(message)
|
|
152
|
+
throw cause
|
|
153
|
+
} finally {
|
|
154
|
+
setMutating(false)
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
[ensureCart, refresh],
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
const value = useMemo<CartContextValue>(
|
|
161
|
+
() => ({
|
|
162
|
+
cart,
|
|
163
|
+
cartId,
|
|
164
|
+
count: cartItemCount(cart),
|
|
165
|
+
loading,
|
|
166
|
+
mutating,
|
|
167
|
+
error,
|
|
168
|
+
drawerOpen,
|
|
169
|
+
setDrawerOpen,
|
|
170
|
+
ensureCart,
|
|
171
|
+
refresh,
|
|
172
|
+
adoptCart,
|
|
173
|
+
addItem: async (variantId, quantity = 1) => {
|
|
174
|
+
await mutate((id) => api.addLineItem(id, variantId, quantity))
|
|
175
|
+
setDrawerOpen(true)
|
|
176
|
+
},
|
|
177
|
+
updateItem: async (lineId, quantity) => {
|
|
178
|
+
if (!cartId) return
|
|
179
|
+
if (quantity <= 0) {
|
|
180
|
+
await mutate((id) => api.removeLineItem(id, lineId))
|
|
181
|
+
return
|
|
182
|
+
}
|
|
183
|
+
await mutate((id) => api.updateLineItem(id, lineId, quantity))
|
|
184
|
+
},
|
|
185
|
+
removeItem: async (lineId) => {
|
|
186
|
+
if (!cartId) return
|
|
187
|
+
await mutate((id) => api.removeLineItem(id, lineId))
|
|
188
|
+
},
|
|
189
|
+
addPromotion: async (code) => {
|
|
190
|
+
if (!code.trim()) return
|
|
191
|
+
await mutate((id) => api.addPromotion(id, code.trim()))
|
|
192
|
+
},
|
|
193
|
+
removePromotion: async (code) => {
|
|
194
|
+
if (!cartId) return
|
|
195
|
+
await mutate((id) => api.removePromotion(id, code))
|
|
196
|
+
},
|
|
197
|
+
transferToCustomer: async () => {
|
|
198
|
+
if (!cartId) return
|
|
199
|
+
setMutating(true)
|
|
200
|
+
try {
|
|
201
|
+
const result = await api.transferCart(cartId)
|
|
202
|
+
setCart(result.cart)
|
|
203
|
+
} finally {
|
|
204
|
+
setMutating(false)
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
clearCart: () => {
|
|
208
|
+
setCart(null)
|
|
209
|
+
storeCartId(null)
|
|
210
|
+
setDrawerOpen(false)
|
|
211
|
+
},
|
|
212
|
+
}),
|
|
213
|
+
[
|
|
214
|
+
api,
|
|
215
|
+
adoptCart,
|
|
216
|
+
cart,
|
|
217
|
+
cartId,
|
|
218
|
+
drawerOpen,
|
|
219
|
+
ensureCart,
|
|
220
|
+
error,
|
|
221
|
+
loading,
|
|
222
|
+
mutate,
|
|
223
|
+
mutating,
|
|
224
|
+
refresh,
|
|
225
|
+
storeCartId,
|
|
226
|
+
],
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
return <CartContext.Provider value={value}>{children}</CartContext.Provider>
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function useCart() {
|
|
233
|
+
const context = useContext(CartContext)
|
|
234
|
+
if (!context) throw new Error("useCart csak CartProvider alatt használható.")
|
|
235
|
+
return context
|
|
236
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import Link from "next/link"
|
|
4
|
+
import { useState } from "react"
|
|
5
|
+
import { useCart } from "./cart-provider"
|
|
6
|
+
import {
|
|
7
|
+
cartCurrency,
|
|
8
|
+
cartDiscountTotal,
|
|
9
|
+
cartItems,
|
|
10
|
+
cartShippingTotal,
|
|
11
|
+
cartSubtotal,
|
|
12
|
+
cartTaxTotal,
|
|
13
|
+
cartTotal,
|
|
14
|
+
formatMoney,
|
|
15
|
+
lineTotal,
|
|
16
|
+
numberValue,
|
|
17
|
+
stringValue,
|
|
18
|
+
} from "@/lib/core/commerce"
|
|
19
|
+
import type { CoreConfig } from "@/lib/core/types"
|
|
20
|
+
|
|
21
|
+
export function CartView({ config, compact = false }: { config: CoreConfig; compact?: boolean }) {
|
|
22
|
+
const {
|
|
23
|
+
cart,
|
|
24
|
+
loading,
|
|
25
|
+
mutating,
|
|
26
|
+
error,
|
|
27
|
+
updateItem,
|
|
28
|
+
removeItem,
|
|
29
|
+
addPromotion,
|
|
30
|
+
removePromotion,
|
|
31
|
+
setDrawerOpen,
|
|
32
|
+
} = useCart()
|
|
33
|
+
const [promo, setPromo] = useState("")
|
|
34
|
+
|
|
35
|
+
if (loading) return <div className="empty-state">Kosár betöltése…</div>
|
|
36
|
+
const items = cartItems(cart)
|
|
37
|
+
if (!cart || !items.length) {
|
|
38
|
+
return (
|
|
39
|
+
<div className="empty-state stack-sm">
|
|
40
|
+
<strong>A kosarad még üres.</strong>
|
|
41
|
+
<span className="muted">Válassz terméket a kínálatból.</span>
|
|
42
|
+
<Link className="button inline-button" href={`/${config.seo.default_country_code}/products`} onClick={() => setDrawerOpen(false)}>
|
|
43
|
+
Termékek megtekintése
|
|
44
|
+
</Link>
|
|
45
|
+
</div>
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const currency = cartCurrency(cart, config.store.default_currency)
|
|
50
|
+
const promotions = Array.isArray(cart.promotions)
|
|
51
|
+
? cart.promotions.filter((item): item is Record<string, unknown> => Boolean(item && typeof item === "object"))
|
|
52
|
+
: []
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<div className="stack">
|
|
56
|
+
<div className="cart-lines">
|
|
57
|
+
{items.map((item) => {
|
|
58
|
+
const id = stringValue(item.id)
|
|
59
|
+
const quantity = Math.max(1, numberValue(item.quantity, 1))
|
|
60
|
+
const thumbnail = stringValue(item.thumbnail)
|
|
61
|
+
return (
|
|
62
|
+
<div className="cart-line" key={id}>
|
|
63
|
+
<div className="cart-line-image">
|
|
64
|
+
{thumbnail ? <img src={thumbnail} alt="" /> : <span />}
|
|
65
|
+
</div>
|
|
66
|
+
<div className="cart-line-copy">
|
|
67
|
+
<strong>{stringValue(item.product_title || item.title)}</strong>
|
|
68
|
+
{item.variant_title ? <span className="muted">{stringValue(item.variant_title)}</span> : null}
|
|
69
|
+
<span>{formatMoney(lineTotal(item), currency, config.store.locale)}</span>
|
|
70
|
+
</div>
|
|
71
|
+
<div className="quantity-control">
|
|
72
|
+
<button type="button" disabled={mutating} onClick={() => void updateItem(id, quantity - 1)} aria-label="Mennyiség csökkentése">−</button>
|
|
73
|
+
<span>{quantity}</span>
|
|
74
|
+
<button type="button" disabled={mutating} onClick={() => void updateItem(id, quantity + 1)} aria-label="Mennyiség növelése">+</button>
|
|
75
|
+
</div>
|
|
76
|
+
<button className="text-button danger" type="button" disabled={mutating} onClick={() => void removeItem(id)}>
|
|
77
|
+
Törlés
|
|
78
|
+
</button>
|
|
79
|
+
</div>
|
|
80
|
+
)
|
|
81
|
+
})}
|
|
82
|
+
</div>
|
|
83
|
+
|
|
84
|
+
{!compact ? (
|
|
85
|
+
<div className="panel stack-sm">
|
|
86
|
+
<strong>Kuponkód</strong>
|
|
87
|
+
<div className="inline-form">
|
|
88
|
+
<input value={promo} onChange={(event) => setPromo(event.target.value)} placeholder="Kuponkód" />
|
|
89
|
+
<button className="button secondary" type="button" disabled={mutating || !promo.trim()} onClick={() => void addPromotion(promo).then(() => setPromo(""))}>
|
|
90
|
+
Beváltás
|
|
91
|
+
</button>
|
|
92
|
+
</div>
|
|
93
|
+
{promotions.length ? (
|
|
94
|
+
<div className="chip-row">
|
|
95
|
+
{promotions.map((promotion) => {
|
|
96
|
+
const code = stringValue(promotion.code)
|
|
97
|
+
return (
|
|
98
|
+
<button className="chip" key={stringValue(promotion.id || code)} type="button" onClick={() => void removePromotion(code)}>
|
|
99
|
+
{code || "Kedvezmény"} ×
|
|
100
|
+
</button>
|
|
101
|
+
)
|
|
102
|
+
})}
|
|
103
|
+
</div>
|
|
104
|
+
) : null}
|
|
105
|
+
</div>
|
|
106
|
+
) : null}
|
|
107
|
+
|
|
108
|
+
<div className="totals">
|
|
109
|
+
<TotalRow label="Részösszeg" value={cartSubtotal(cart)} currency={currency} locale={config.store.locale} />
|
|
110
|
+
{cartDiscountTotal(cart) ? <TotalRow label="Kedvezmény" value={-cartDiscountTotal(cart)} currency={currency} locale={config.store.locale} /> : null}
|
|
111
|
+
<TotalRow label="Szállítás" value={cartShippingTotal(cart)} currency={currency} locale={config.store.locale} />
|
|
112
|
+
{cartTaxTotal(cart) ? <TotalRow label="Adó" value={cartTaxTotal(cart)} currency={currency} locale={config.store.locale} subtle /> : null}
|
|
113
|
+
<TotalRow label="Összesen" value={cartTotal(cart)} currency={currency} locale={config.store.locale} strong />
|
|
114
|
+
</div>
|
|
115
|
+
|
|
116
|
+
{error ? <p className="error">{error}</p> : null}
|
|
117
|
+
|
|
118
|
+
<div className="button-row">
|
|
119
|
+
{compact ? (
|
|
120
|
+
<Link className="button secondary" href="/cart" onClick={() => setDrawerOpen(false)}>Kosár</Link>
|
|
121
|
+
) : null}
|
|
122
|
+
<Link className="button" href="/checkout" onClick={() => setDrawerOpen(false)}>Tovább a pénztárhoz</Link>
|
|
123
|
+
</div>
|
|
124
|
+
</div>
|
|
125
|
+
)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function TotalRow({ label, value, currency, locale, strong = false, subtle = false }: { label: string; value: number; currency: string; locale: string; strong?: boolean; subtle?: boolean }) {
|
|
129
|
+
return (
|
|
130
|
+
<div className={`total-row${strong ? " total-strong" : ""}${subtle ? " muted" : ""}`}>
|
|
131
|
+
<span>{label}</span>
|
|
132
|
+
<span>{formatMoney(value, currency, locale)}</span>
|
|
133
|
+
</div>
|
|
134
|
+
)
|
|
135
|
+
}
|