@hszrdm/create-commerce 1.0.3 → 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.
Files changed (40) hide show
  1. package/bin/create-commerce.mjs +9 -2
  2. package/package.json +1 -1
  3. package/templates/backend/.env.example +66 -7
  4. package/templates/backend/medusa-config.ts +17 -1
  5. package/templates/backend/package.json +2 -1
  6. package/templates/backend/src/scripts/core-init.ts +13 -1
  7. package/templates/storefront/next-env.d.ts +4 -1
  8. package/templates/storefront/src/app/[countryCode]/categories/[handle]/page.tsx +9 -3
  9. package/templates/storefront/src/app/[countryCode]/products/[handle]/page.tsx +9 -5
  10. package/templates/storefront/src/app/[countryCode]/products/page.tsx +17 -10
  11. package/templates/storefront/src/app/checkout/page.tsx +12 -3
  12. package/templates/storefront/src/app/checkout/success/page.tsx +58 -18
  13. package/templates/storefront/src/app/layout.tsx +10 -3
  14. package/templates/storefront/src/app/legal/page.tsx +11 -5
  15. package/templates/storefront/src/app/page.tsx +18 -12
  16. package/templates/storefront/src/app/reset-password/page.tsx +8 -2
  17. package/templates/storefront/src/components/account-dashboard.tsx +197 -76
  18. package/templates/storefront/src/components/add-to-cart.tsx +24 -10
  19. package/templates/storefront/src/components/cart-provider.tsx +29 -12
  20. package/templates/storefront/src/components/cart-view.tsx +28 -20
  21. package/templates/storefront/src/components/checkout-analytics.tsx +23 -0
  22. package/templates/storefront/src/components/checkout-flow.tsx +354 -102
  23. package/templates/storefront/src/components/order-detail.tsx +84 -45
  24. package/templates/storefront/src/components/product-card.tsx +16 -8
  25. package/templates/storefront/src/components/reset-password-form.tsx +19 -13
  26. package/templates/storefront/src/components/storefront-header.tsx +25 -11
  27. package/templates/storefront/src/components/stripe-payment.tsx +20 -4
  28. package/templates/storefront/src/lib/core/catalog.ts +37 -5
  29. package/templates/storefront/src/lib/core/client.ts +5 -0
  30. package/templates/storefront/src/lib/core/cms.ts +112 -0
  31. package/templates/storefront/src/lib/core/commerce.ts +14 -4
  32. package/templates/storefront/src/lib/core/leads.ts +50 -0
  33. package/templates/storefront/src/lib/core/pricing.ts +68 -0
  34. package/templates/storefront/src/lib/core/quotes.ts +43 -0
  35. package/templates/storefront/src/lib/core/seo.ts +44 -6
  36. package/templates/storefront/src/lib/storefront-copy.ts +435 -0
  37. package/templates/storefront/src/lib/storefront-locale-server.ts +11 -0
  38. package/templates/storefront/src/lib/storefront-routes.ts +91 -0
  39. package/templates/storefront/src/proxy.ts +142 -0
  40. package/templates/storefront/tsconfig.json +23 -5
@@ -1,6 +1,7 @@
1
1
  "use client"
2
2
 
3
3
  import { useMemo, useState } from "react"
4
+ import { usePathname } from "next/navigation"
4
5
  import { useCart } from "./cart-provider"
5
6
  import {
6
7
  asRecords,
@@ -10,32 +11,38 @@ import {
10
11
  stringValue,
11
12
  } from "@/lib/core/commerce"
12
13
  import type { CoreConfig } from "@/lib/core/types"
14
+ import { localeFromPathname } from "@/lib/storefront-routes"
13
15
 
14
16
  export function AddToCart({ product, config }: { product: Record<string, unknown>; config: CoreConfig }) {
17
+ const pathname = usePathname()
18
+ const locale = localeFromPathname(pathname)
19
+ const en = locale === "en"
15
20
  const variants = useMemo(() => asRecords(product.variants), [product])
16
21
  const [variantId, setVariantId] = useState(() => stringValue(variants[0]?.id))
17
22
  const [quantity, setQuantity] = useState(1)
18
23
  const [message, setMessage] = useState<string | null>(null)
24
+ const [success, setSuccess] = useState(false)
19
25
  const { addItem, mutating } = useCart()
20
26
  const selected = variants.find((variant) => stringValue(variant.id) === variantId) || variants[0]
21
27
  const price = productVariantPrice(selected)
22
28
  const currency = productVariantCurrency(selected, config.store.default_currency)
29
+ const formatLocale = en ? "en-US" : "hu-HU"
23
30
 
24
31
  if (!variants.length) {
25
- return <div className="notice warning">Ehhez a termékhez nincs megvásárolható variáns.</div>
32
+ return <div className="notice warning">{en ? "This product has no purchasable variant." : "Ehhez a termékhez nincs megvásárolható variáns."}</div>
26
33
  }
27
34
 
28
35
  return (
29
36
  <div className="buy-box stack-sm">
30
37
  {variants.length > 1 ? (
31
38
  <label>
32
- Változat
39
+ {en ? "Variant" : "Változat"}
33
40
  <select value={variantId} onChange={(event) => setVariantId(event.target.value)}>
34
41
  {variants.map((variant) => (
35
42
  <option key={stringValue(variant.id)} value={stringValue(variant.id)}>
36
43
  {stringValue(variant.title || variant.sku || variant.id)}
37
44
  {Number.isFinite(productVariantPrice(variant))
38
- ? ` – ${formatMoney(productVariantPrice(variant), productVariantCurrency(variant, currency), config.store.locale)}`
45
+ ? ` – ${formatMoney(productVariantPrice(variant), productVariantCurrency(variant, currency), formatLocale)}`
39
46
  : ""}
40
47
  </option>
41
48
  ))}
@@ -44,12 +51,12 @@ export function AddToCart({ product, config }: { product: Record<string, unknown
44
51
  ) : null}
45
52
  <div className="buy-row">
46
53
  <label className="quantity-field">
47
- Mennyiség
54
+ {en ? "Quantity" : "Mennyiség"}
48
55
  <input min={1} max={99} type="number" value={quantity} onChange={(event) => setQuantity(Math.max(1, Number(event.target.value) || 1))} />
49
56
  </label>
50
57
  <div className="buy-price">
51
- <span className="muted">Ár</span>
52
- <strong>{Number.isFinite(price) ? formatMoney(price, currency, config.store.locale) : "—"}</strong>
58
+ <span className="muted">{en ? "Price" : "Ár"}</span>
59
+ <strong>{Number.isFinite(price) ? formatMoney(price, currency, formatLocale) : "—"}</strong>
53
60
  </div>
54
61
  </div>
55
62
  <button
@@ -58,14 +65,21 @@ export function AddToCart({ product, config }: { product: Record<string, unknown
58
65
  disabled={mutating || !variantId}
59
66
  onClick={() => {
60
67
  setMessage(null)
68
+ setSuccess(false)
61
69
  void addItem(variantId, quantity)
62
- .then(() => setMessage("A termék bekerült a kosárba."))
63
- .catch((error) => setMessage(error instanceof Error ? error.message : "Nem sikerült kosárba tenni."))
70
+ .then(() => {
71
+ setSuccess(true)
72
+ setMessage(en ? "The product was added to your cart." : "A termék bekerült a kosárba.")
73
+ })
74
+ .catch((error) => {
75
+ setSuccess(false)
76
+ setMessage(error instanceof Error ? error.message : en ? "Could not add the product to the cart." : "Nem sikerült kosárba tenni.")
77
+ })
64
78
  }}
65
79
  >
66
- {mutating ? "Kosár frissítése…" : "Kosárba"}
80
+ {mutating ? (en ? "Updating cart…" : "Kosár frissítése…") : (en ? "Add to cart" : "Kosárba")}
67
81
  </button>
68
- {message ? <p className={message.includes("bekerült") ? "success" : "error"}>{message}</p> : null}
82
+ {message ? <p className={success ? "success" : "error"}>{message}</p> : null}
69
83
  </div>
70
84
  )
71
85
  }
@@ -9,9 +9,11 @@ import {
9
9
  useState,
10
10
  type ReactNode,
11
11
  } from "react"
12
+ import { usePathname } from "next/navigation"
12
13
  import { checkoutApi } from "@/lib/core/checkout"
13
- import { cartItemCount, type JsonRecord } from "@/lib/core/commerce"
14
+ import { cartItemCount, stringValue, type JsonRecord } from "@/lib/core/commerce"
14
15
  import type { CoreConfig } from "@/lib/core/types"
16
+ import { localeFromPathname, medusaLocale } from "@/lib/storefront-routes"
15
17
 
16
18
  const CART_STORAGE_KEY = "hszrdm_core_cart_id"
17
19
 
@@ -46,6 +48,8 @@ export function CartProvider({
46
48
  children: ReactNode
47
49
  }) {
48
50
  const api = useMemo(() => checkoutApi(config), [config])
51
+ const pathname = usePathname()
52
+ const activeLocale = medusaLocale(localeFromPathname(pathname))
49
53
  const [cart, setCart] = useState<JsonRecord | null>(null)
50
54
  const [cartId, setCartId] = useState<string | null>(null)
51
55
  const [loading, setLoading] = useState(true)
@@ -60,6 +64,16 @@ export function CartProvider({
60
64
  else window.localStorage.removeItem(CART_STORAGE_KEY)
61
65
  }, [])
62
66
 
67
+ const syncLocale = useCallback(
68
+ async (id: string, current: JsonRecord) => {
69
+ if (stringValue(current.locale).toLowerCase() === activeLocale.toLowerCase()) {
70
+ return current
71
+ }
72
+ return (await api.updateCart(id, { locale: activeLocale })).cart
73
+ },
74
+ [activeLocale, api],
75
+ )
76
+
63
77
  const refresh = useCallback(
64
78
  async (id?: string) => {
65
79
  const target = id || cartId
@@ -69,10 +83,11 @@ export function CartProvider({
69
83
  }
70
84
  try {
71
85
  const result = await api.getCart(target)
72
- setCart(result.cart)
86
+ const localizedCart = await syncLocale(target, result.cart)
87
+ setCart(localizedCart)
73
88
  storeCartId(target)
74
89
  setError(null)
75
- return result.cart
90
+ return localizedCart
76
91
  } catch (cause) {
77
92
  if (!id || id === cartId) {
78
93
  setCart(null)
@@ -82,7 +97,7 @@ export function CartProvider({
82
97
  return null
83
98
  }
84
99
  },
85
- [api, cartId, storeCartId],
100
+ [api, cartId, storeCartId, syncLocale],
86
101
  )
87
102
 
88
103
  useEffect(() => {
@@ -94,8 +109,9 @@ export function CartProvider({
94
109
  setCartId(id)
95
110
  void api
96
111
  .getCart(id)
97
- .then((result) => {
98
- setCart(result.cart)
112
+ .then((result) => syncLocale(id, result.cart))
113
+ .then((localizedCart) => {
114
+ setCart(localizedCart)
99
115
  setError(null)
100
116
  })
101
117
  .catch(() => {
@@ -104,13 +120,13 @@ export function CartProvider({
104
120
  setCart(null)
105
121
  })
106
122
  .finally(() => setLoading(false))
107
- }, [api])
123
+ }, [api, syncLocale])
108
124
 
109
125
  const ensureCart = useCallback(async () => {
110
126
  if (cartId) return cartId
111
127
  setMutating(true)
112
128
  try {
113
- const result = await api.createCart()
129
+ const result = await api.createCart({ locale: activeLocale })
114
130
  const id = String(result.cart.id || "")
115
131
  if (!id) throw new Error("A Medusa nem adott vissza cart ID-t.")
116
132
  setCart(result.cart)
@@ -119,19 +135,20 @@ export function CartProvider({
119
135
  } finally {
120
136
  setMutating(false)
121
137
  }
122
- }, [api, cartId, storeCartId])
138
+ }, [activeLocale, api, cartId, storeCartId])
123
139
 
124
140
  const adoptCart = useCallback(
125
141
  async (id: string) => {
126
142
  const normalized = id.trim()
127
143
  if (!normalized) return null
128
144
  const result = await api.getCart(normalized)
129
- setCart(result.cart)
145
+ const localizedCart = await syncLocale(normalized, result.cart)
146
+ setCart(localizedCart)
130
147
  storeCartId(normalized)
131
148
  setError(null)
132
- return result.cart
149
+ return localizedCart
133
150
  },
134
- [api, storeCartId],
151
+ [api, storeCartId, syncLocale],
135
152
  )
136
153
 
137
154
  const mutate = useCallback(
@@ -1,6 +1,7 @@
1
1
  "use client"
2
2
 
3
3
  import Link from "next/link"
4
+ import { usePathname } from "next/navigation"
4
5
  import { useState } from "react"
5
6
  import { useCart } from "./cart-provider"
6
7
  import {
@@ -17,6 +18,8 @@ import {
17
18
  stringValue,
18
19
  } from "@/lib/core/commerce"
19
20
  import type { CoreConfig } from "@/lib/core/types"
21
+ import { storefrontCopy } from "@/lib/storefront-copy"
22
+ import { localeFromPathname, STOREFRONT_ROUTES } from "@/lib/storefront-routes"
20
23
 
21
24
  export function CartView({ config, compact = false }: { config: CoreConfig; compact?: boolean }) {
22
25
  const {
@@ -30,17 +33,22 @@ export function CartView({ config, compact = false }: { config: CoreConfig; comp
30
33
  removePromotion,
31
34
  setDrawerOpen,
32
35
  } = useCart()
36
+ const pathname = usePathname()
37
+ const locale = localeFromPathname(pathname)
38
+ const routes = STOREFRONT_ROUTES[locale]
39
+ const copy = storefrontCopy(locale)
40
+ const formatLocale = locale === "en" ? "en-US" : "hu-HU"
33
41
  const [promo, setPromo] = useState("")
34
42
 
35
- if (loading) return <div className="empty-state">Kosár betöltése…</div>
43
+ if (loading) return <div className="empty-state">{copy.cartLoading}</div>
36
44
  const items = cartItems(cart)
37
45
  if (!cart || !items.length) {
38
46
  return (
39
47
  <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
48
+ <strong>{copy.emptyCart}</strong>
49
+ <span className="muted">{copy.chooseProduct}</span>
50
+ <Link className="button inline-button" href={routes.products} onClick={() => setDrawerOpen(false)}>
51
+ {copy.viewProducts}
44
52
  </Link>
45
53
  </div>
46
54
  )
@@ -66,15 +74,15 @@ export function CartView({ config, compact = false }: { config: CoreConfig; comp
66
74
  <div className="cart-line-copy">
67
75
  <strong>{stringValue(item.product_title || item.title)}</strong>
68
76
  {item.variant_title ? <span className="muted">{stringValue(item.variant_title)}</span> : null}
69
- <span>{formatMoney(lineTotal(item), currency, config.store.locale)}</span>
77
+ <span>{formatMoney(lineTotal(item), currency, formatLocale)}</span>
70
78
  </div>
71
79
  <div className="quantity-control">
72
- <button type="button" disabled={mutating} onClick={() => void updateItem(id, quantity - 1)} aria-label="Mennyiség csökkentése">−</button>
80
+ <button type="button" disabled={mutating} onClick={() => void updateItem(id, quantity - 1)} aria-label={copy.decreaseQuantity}>−</button>
73
81
  <span>{quantity}</span>
74
- <button type="button" disabled={mutating} onClick={() => void updateItem(id, quantity + 1)} aria-label="Mennyiség növelése">+</button>
82
+ <button type="button" disabled={mutating} onClick={() => void updateItem(id, quantity + 1)} aria-label={copy.increaseQuantity}>+</button>
75
83
  </div>
76
84
  <button className="text-button danger" type="button" disabled={mutating} onClick={() => void removeItem(id)}>
77
- Törlés
85
+ {copy.remove}
78
86
  </button>
79
87
  </div>
80
88
  )
@@ -83,11 +91,11 @@ export function CartView({ config, compact = false }: { config: CoreConfig; comp
83
91
 
84
92
  {!compact ? (
85
93
  <div className="panel stack-sm">
86
- <strong>Kuponkód</strong>
94
+ <strong>{copy.couponCode}</strong>
87
95
  <div className="inline-form">
88
- <input value={promo} onChange={(event) => setPromo(event.target.value)} placeholder="Kuponkód" />
96
+ <input value={promo} onChange={(event) => setPromo(event.target.value)} placeholder={copy.couponCode} />
89
97
  <button className="button secondary" type="button" disabled={mutating || !promo.trim()} onClick={() => void addPromotion(promo).then(() => setPromo(""))}>
90
- Beváltás
98
+ {copy.redeem}
91
99
  </button>
92
100
  </div>
93
101
  {promotions.length ? (
@@ -96,7 +104,7 @@ export function CartView({ config, compact = false }: { config: CoreConfig; comp
96
104
  const code = stringValue(promotion.code)
97
105
  return (
98
106
  <button className="chip" key={stringValue(promotion.id || code)} type="button" onClick={() => void removePromotion(code)}>
99
- {code || "Kedvezmény"} ×
107
+ {code || copy.discount} ×
100
108
  </button>
101
109
  )
102
110
  })}
@@ -106,20 +114,20 @@ export function CartView({ config, compact = false }: { config: CoreConfig; comp
106
114
  ) : null}
107
115
 
108
116
  <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 />
117
+ <TotalRow label={copy.subtotal} value={cartSubtotal(cart)} currency={currency} locale={formatLocale} />
118
+ {cartDiscountTotal(cart) ? <TotalRow label={copy.discount} value={-cartDiscountTotal(cart)} currency={currency} locale={formatLocale} /> : null}
119
+ <TotalRow label={copy.shipping} value={cartShippingTotal(cart)} currency={currency} locale={formatLocale} />
120
+ {cartTaxTotal(cart) ? <TotalRow label={copy.tax} value={cartTaxTotal(cart)} currency={currency} locale={formatLocale} subtle /> : null}
121
+ <TotalRow label={copy.total} value={cartTotal(cart)} currency={currency} locale={formatLocale} strong />
114
122
  </div>
115
123
 
116
124
  {error ? <p className="error">{error}</p> : null}
117
125
 
118
126
  <div className="button-row">
119
127
  {compact ? (
120
- <Link className="button secondary" href="/cart" onClick={() => setDrawerOpen(false)}>Kosár</Link>
128
+ <Link className="button secondary" href={routes.cart} onClick={() => setDrawerOpen(false)}>{copy.cart}</Link>
121
129
  ) : null}
122
- <Link className="button" href="/checkout" onClick={() => setDrawerOpen(false)}>Tovább a pénztárhoz</Link>
130
+ <Link className="button" href={routes.checkout} onClick={() => setDrawerOpen(false)}>{copy.checkoutContinue}</Link>
123
131
  </div>
124
132
  </div>
125
133
  )
@@ -0,0 +1,23 @@
1
+ "use client"
2
+
3
+ import { useEffect, useMemo, useRef } from "react"
4
+ import { useCart } from "./cart-provider"
5
+ import { checkoutApi } from "@/lib/core/checkout"
6
+ import type { CoreConfig } from "@/lib/core/types"
7
+
8
+ export function CheckoutAnalytics({ config }: { config: CoreConfig }) {
9
+ const api = useMemo(() => checkoutApi(config), [config])
10
+ const { cart, cartId } = useCart()
11
+ const trackedCart = useRef<string | null>(null)
12
+
13
+ useEffect(() => {
14
+ if (!cart || !cartId || trackedCart.current === cartId) return
15
+
16
+ trackedCart.current = cartId
17
+ void api.beginCheckout(cartId).catch(() => {
18
+ if (trackedCart.current === cartId) trackedCart.current = null
19
+ })
20
+ }, [api, cart, cartId])
21
+
22
+ return null
23
+ }