@growth-labs/store 0.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/README.md +41 -0
- package/dist/api.d.ts +30 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +126 -0
- package/dist/api.js.map +1 -0
- package/dist/availability.d.ts +30 -0
- package/dist/availability.d.ts.map +1 -0
- package/dist/availability.js +47 -0
- package/dist/availability.js.map +1 -0
- package/dist/cache.d.ts +34 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +80 -0
- package/dist/cache.js.map +1 -0
- package/dist/cart-client.d.ts +54 -0
- package/dist/cart-client.d.ts.map +1 -0
- package/dist/cart-client.js +347 -0
- package/dist/cart-client.js.map +1 -0
- package/dist/checkout-client.d.ts +20 -0
- package/dist/checkout-client.d.ts.map +1 -0
- package/dist/checkout-client.js +33 -0
- package/dist/checkout-client.js.map +1 -0
- package/dist/checkout-url.d.ts +25 -0
- package/dist/checkout-url.d.ts.map +1 -0
- package/dist/checkout-url.js +36 -0
- package/dist/checkout-url.js.map +1 -0
- package/dist/client.d.ts +459 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +139 -0
- package/dist/client.js.map +1 -0
- package/dist/collections.d.ts +27 -0
- package/dist/collections.d.ts.map +1 -0
- package/dist/collections.js +24 -0
- package/dist/collections.js.map +1 -0
- package/dist/health.d.ts +17 -0
- package/dist/health.d.ts.map +1 -0
- package/dist/health.js +38 -0
- package/dist/health.js.map +1 -0
- package/dist/image.d.ts +25 -0
- package/dist/image.d.ts.map +1 -0
- package/dist/image.js +43 -0
- package/dist/image.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +48 -0
- package/dist/index.js.map +1 -0
- package/dist/options.d.ts +174 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +59 -0
- package/dist/options.js.map +1 -0
- package/dist/product-card-variant.d.ts +13 -0
- package/dist/product-card-variant.d.ts.map +1 -0
- package/dist/product-card-variant.js +33 -0
- package/dist/product-card-variant.js.map +1 -0
- package/dist/routes/cart-bootstrap.d.ts +3 -0
- package/dist/routes/cart-bootstrap.d.ts.map +1 -0
- package/dist/routes/cart-bootstrap.js +5 -0
- package/dist/routes/cart-bootstrap.js.map +1 -0
- package/dist/routes/checkout.d.ts +4 -0
- package/dist/routes/checkout.d.ts.map +1 -0
- package/dist/routes/checkout.js +14 -0
- package/dist/routes/checkout.js.map +1 -0
- package/dist/routes/webhook.d.ts +4 -0
- package/dist/routes/webhook.d.ts.map +1 -0
- package/dist/routes/webhook.js +12 -0
- package/dist/routes/webhook.js.map +1 -0
- package/dist/state.d.ts +5 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +14 -0
- package/dist/state.js.map +1 -0
- package/dist/types.d.ts +5111 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +167 -0
- package/dist/types.js.map +1 -0
- package/dist/variant.d.ts +59 -0
- package/dist/variant.d.ts.map +1 -0
- package/dist/variant.js +81 -0
- package/dist/variant.js.map +1 -0
- package/dist/vite-plugin.d.ts +4 -0
- package/dist/vite-plugin.d.ts.map +1 -0
- package/dist/vite-plugin.js +24 -0
- package/dist/vite-plugin.js.map +1 -0
- package/package.json +59 -0
- package/src/analytics-window.d.ts +15 -0
- package/src/api.ts +184 -0
- package/src/availability.ts +65 -0
- package/src/cache.ts +118 -0
- package/src/cart-client.ts +423 -0
- package/src/checkout-client.ts +49 -0
- package/src/checkout-url.ts +52 -0
- package/src/client.ts +216 -0
- package/src/cloudflare-workers.d.ts +3 -0
- package/src/collections.ts +63 -0
- package/src/components/AddToCartButton.astro +146 -0
- package/src/components/CartDrawer.astro +276 -0
- package/src/components/CartIcon.astro +96 -0
- package/src/components/CollectionCard.astro +81 -0
- package/src/components/CurrencySelector.astro +73 -0
- package/src/components/Price.astro +34 -0
- package/src/components/ProductCard.astro +225 -0
- package/src/components/StoreCartPage.astro +219 -0
- package/src/components/StoreCollectionPage.astro +166 -0
- package/src/components/StoreIndexPage.astro +193 -0
- package/src/components/StoreProductPage.astro +296 -0
- package/src/components/VariantPicker.astro +190 -0
- package/src/health.ts +54 -0
- package/src/image.ts +58 -0
- package/src/index.ts +75 -0
- package/src/options.ts +74 -0
- package/src/product-card-variant.ts +47 -0
- package/src/routes/cart-bootstrap.ts +6 -0
- package/src/routes/checkout.ts +16 -0
- package/src/routes/webhook.ts +14 -0
- package/src/state.ts +20 -0
- package/src/types.ts +196 -0
- package/src/variant.ts +119 -0
- package/src/virtual.d.ts +6 -0
- package/src/vite-plugin.ts +27 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { optimizeFwImage } from '../image.js'
|
|
3
|
+
import { formatStorePrice, getProductCardColorOptions } from '../product-card-variant.js'
|
|
4
|
+
import type { FwProduct } from '../types.js'
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
product: FwProduct
|
|
8
|
+
href?: string
|
|
9
|
+
/** First cards on the listing should hint the browser to fetch eagerly. */
|
|
10
|
+
priority?: boolean
|
|
11
|
+
imageTransformEnabled?: boolean
|
|
12
|
+
/** Base path for product routes; default `/store`. */
|
|
13
|
+
basePath?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const { product, href, priority = false, imageTransformEnabled = false, basePath = '/store' } = Astro.props
|
|
17
|
+
const linkHref = href ?? `${basePath}/${product.slug}/`
|
|
18
|
+
const colorOptions = getProductCardColorOptions(product)
|
|
19
|
+
const initialOption = colorOptions[0] ?? null
|
|
20
|
+
const initialImage = initialOption?.image ?? product.images[0] ?? null
|
|
21
|
+
const initialImageUrl = initialImage?.url ?? null
|
|
22
|
+
const initialPrice = initialOption?.price ?? product.variants[0]?.unitPrice
|
|
23
|
+
const initialCompareAt = initialOption?.compareAt ?? product.variants[0]?.compareAtPrice ?? null
|
|
24
|
+
const initialOnSale = initialPrice && initialCompareAt ? initialCompareAt.value > initialPrice.value : false
|
|
25
|
+
|
|
26
|
+
function getSrcset(imageUrl: string | null): string | undefined {
|
|
27
|
+
return imageUrl
|
|
28
|
+
? [320, 480, 640, 960]
|
|
29
|
+
.map((width) => `${optimizeFwImage(imageUrl, { width, enabled: imageTransformEnabled }) ?? imageUrl} ${width}w`)
|
|
30
|
+
.join(', ')
|
|
31
|
+
: undefined
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function getVariantHref(variantId: string): string {
|
|
35
|
+
return `${linkHref + (linkHref.includes('?') ? '&' : '?')}variant=${encodeURIComponent(variantId)}`
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const initialSrc = optimizeFwImage(initialImageUrl, { width: 480, enabled: imageTransformEnabled }) ?? initialImageUrl
|
|
39
|
+
const initialSrcset = getSrcset(initialImageUrl)
|
|
40
|
+
const cardColorOptions = colorOptions.map((option) => {
|
|
41
|
+
const imageUrl = option.image?.url ?? null
|
|
42
|
+
return {
|
|
43
|
+
variantId: option.variantId,
|
|
44
|
+
colorName: option.colorName,
|
|
45
|
+
swatch: option.swatch,
|
|
46
|
+
price: option.price,
|
|
47
|
+
compareAt: option.compareAt,
|
|
48
|
+
href: getVariantHref(option.variantId),
|
|
49
|
+
imageSrc: optimizeFwImage(imageUrl, { width: 480, enabled: imageTransformEnabled }) ?? imageUrl,
|
|
50
|
+
imageSrcset: getSrcset(imageUrl) ?? null,
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
// Product names frequently follow "Name — SKU" (em-dash). Split so the SKU
|
|
55
|
+
// reads as muted metadata beside the title rather than competing with it.
|
|
56
|
+
const emDashSplit = product.name.split(/\s+—\s+/)
|
|
57
|
+
const titleText = emDashSplit[0]
|
|
58
|
+
const skuText = emDashSplit.length > 1 ? emDashSplit.slice(1).join(' — ') : null
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
<article
|
|
62
|
+
class="group flex h-full flex-col overflow-hidden rounded-sm border border-[var(--border)] transition-colors hover:border-[var(--foreground)]"
|
|
63
|
+
data-product-card
|
|
64
|
+
data-card-color-options={JSON.stringify(cardColorOptions)}
|
|
65
|
+
>
|
|
66
|
+
<a
|
|
67
|
+
href={initialOption ? getVariantHref(initialOption.variantId) : linkHref}
|
|
68
|
+
data-card-link
|
|
69
|
+
class="block focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-[var(--ring,var(--foreground))]"
|
|
70
|
+
>
|
|
71
|
+
<div class="relative aspect-[4/5] overflow-hidden bg-[var(--muted)]">
|
|
72
|
+
{initialImageUrl ? (
|
|
73
|
+
<img
|
|
74
|
+
src={initialSrc ?? initialImageUrl}
|
|
75
|
+
srcset={initialSrcset}
|
|
76
|
+
sizes="(min-width: 1024px) 33vw, (min-width: 640px) 50vw, 100vw"
|
|
77
|
+
alt={product.name}
|
|
78
|
+
width={initialImage?.width ?? 480}
|
|
79
|
+
height={initialImage?.height ?? 600}
|
|
80
|
+
loading={priority ? 'eager' : 'lazy'}
|
|
81
|
+
decoding="async"
|
|
82
|
+
fetchpriority={priority ? 'high' : 'auto'}
|
|
83
|
+
class="h-full w-full object-cover transition-transform duration-500 group-hover:scale-[1.01]"
|
|
84
|
+
data-card-preview-image
|
|
85
|
+
/>
|
|
86
|
+
) : (
|
|
87
|
+
<div class="flex h-full w-full items-center justify-center text-[var(--muted-foreground)]">
|
|
88
|
+
<span class="text-xs font-semibold uppercase tracking-[0.14em]">Store</span>
|
|
89
|
+
</div>
|
|
90
|
+
)}
|
|
91
|
+
</div>
|
|
92
|
+
</a>
|
|
93
|
+
<div class="flex flex-1 flex-col gap-2 p-5">
|
|
94
|
+
<h3 class="mb-1 text-lg font-semibold text-[var(--foreground)] transition-colors">
|
|
95
|
+
<a
|
|
96
|
+
href={initialOption ? getVariantHref(initialOption.variantId) : linkHref}
|
|
97
|
+
data-card-link
|
|
98
|
+
class="focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-[var(--ring,var(--foreground))]"
|
|
99
|
+
>
|
|
100
|
+
<span>{titleText}</span>
|
|
101
|
+
{skuText && <span class="ml-1 font-normal text-[var(--muted-foreground)]">{skuText}</span>}
|
|
102
|
+
</a>
|
|
103
|
+
</h3>
|
|
104
|
+
{initialPrice && (
|
|
105
|
+
<span class="inline-flex items-baseline gap-2 text-base">
|
|
106
|
+
<span class="font-semibold text-[var(--foreground)]" data-card-price>
|
|
107
|
+
{formatStorePrice(initialPrice)}
|
|
108
|
+
</span>
|
|
109
|
+
<span class="text-sm text-[var(--muted-foreground)] line-through" data-card-compare-price hidden={!initialOnSale}>
|
|
110
|
+
{initialCompareAt && formatStorePrice(initialCompareAt)}
|
|
111
|
+
</span>
|
|
112
|
+
</span>
|
|
113
|
+
)}
|
|
114
|
+
{colorOptions.length > 1 && (
|
|
115
|
+
<div class="mt-1 space-y-1.5">
|
|
116
|
+
<p class="text-xs text-[var(--muted-foreground)]">
|
|
117
|
+
Color: <span class="font-medium text-[var(--foreground)]" data-card-color-name>{initialOption?.colorName}</span>
|
|
118
|
+
</p>
|
|
119
|
+
<div class="flex flex-wrap gap-2" role="group" aria-label="Available colors">
|
|
120
|
+
{colorOptions.map((option) => (
|
|
121
|
+
<button
|
|
122
|
+
type="button"
|
|
123
|
+
class="h-6 w-6 rounded-full border border-[var(--border)] transition focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--ring,var(--foreground))] focus-visible:ring-offset-2"
|
|
124
|
+
data-card-color-button
|
|
125
|
+
data-card-variant-id={option.variantId}
|
|
126
|
+
data-card-swatch={option.swatch ?? '#cccccc'}
|
|
127
|
+
aria-label={`Preview ${option.colorName}`}
|
|
128
|
+
aria-pressed={option.variantId === initialOption?.variantId}
|
|
129
|
+
></button>
|
|
130
|
+
))}
|
|
131
|
+
</div>
|
|
132
|
+
</div>
|
|
133
|
+
)}
|
|
134
|
+
</div>
|
|
135
|
+
</article>
|
|
136
|
+
|
|
137
|
+
<script>
|
|
138
|
+
interface CardPrice {
|
|
139
|
+
value: number
|
|
140
|
+
currency: string
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
interface CardColorOption {
|
|
144
|
+
variantId: string
|
|
145
|
+
colorName: string
|
|
146
|
+
href: string
|
|
147
|
+
imageSrc: string | null
|
|
148
|
+
imageSrcset: string | null
|
|
149
|
+
price: CardPrice
|
|
150
|
+
compareAt: CardPrice | null
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function formatStorePrice(price: CardPrice): string {
|
|
154
|
+
try {
|
|
155
|
+
return new Intl.NumberFormat('en-US', {
|
|
156
|
+
style: 'currency',
|
|
157
|
+
currency: price.currency,
|
|
158
|
+
maximumFractionDigits: Number.isInteger(price.value) ? 0 : 2,
|
|
159
|
+
}).format(price.value)
|
|
160
|
+
} catch {
|
|
161
|
+
return `${price.currency} ${price.value.toFixed(2)}`
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
document.querySelectorAll<HTMLElement>('[data-product-card]').forEach((card) => {
|
|
166
|
+
let options: CardColorOption[] = []
|
|
167
|
+
try {
|
|
168
|
+
options = JSON.parse(card.dataset.cardColorOptions ?? '[]')
|
|
169
|
+
} catch {
|
|
170
|
+
return
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const buttons = card.querySelectorAll<HTMLButtonElement>('[data-card-color-button]')
|
|
174
|
+
const preview = card.querySelector<HTMLImageElement>('[data-card-preview-image]')
|
|
175
|
+
const links = card.querySelectorAll<HTMLAnchorElement>('[data-card-link]')
|
|
176
|
+
const price = card.querySelector<HTMLElement>('[data-card-price]')
|
|
177
|
+
const comparePrice = card.querySelector<HTMLElement>('[data-card-compare-price]')
|
|
178
|
+
const colorName = card.querySelector<HTMLElement>('[data-card-color-name]')
|
|
179
|
+
|
|
180
|
+
function selectOption(option: CardColorOption) {
|
|
181
|
+
buttons.forEach((button) => {
|
|
182
|
+
button.setAttribute('aria-pressed', String(button.dataset.cardVariantId === option.variantId))
|
|
183
|
+
})
|
|
184
|
+
links.forEach((link) => {
|
|
185
|
+
link.href = option.href
|
|
186
|
+
})
|
|
187
|
+
if (preview && option.imageSrc) {
|
|
188
|
+
preview.src = option.imageSrc
|
|
189
|
+
if (option.imageSrcset) {
|
|
190
|
+
preview.srcset = option.imageSrcset
|
|
191
|
+
} else {
|
|
192
|
+
preview.removeAttribute('srcset')
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (price) price.textContent = formatStorePrice(option.price)
|
|
196
|
+
const onSale = option.compareAt && option.compareAt.value > option.price.value
|
|
197
|
+
if (comparePrice) {
|
|
198
|
+
comparePrice.hidden = !onSale
|
|
199
|
+
if (onSale && option.compareAt) {
|
|
200
|
+
comparePrice.textContent = formatStorePrice(option.compareAt)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (colorName) colorName.textContent = option.colorName
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
buttons.forEach((button) => {
|
|
207
|
+
button.style.backgroundColor = button.dataset.cardSwatch ?? '#cccccc'
|
|
208
|
+
button.addEventListener('click', () => {
|
|
209
|
+
const option = options.find((candidate) => candidate.variantId === button.dataset.cardVariantId)
|
|
210
|
+
if (option) selectOption(option)
|
|
211
|
+
})
|
|
212
|
+
})
|
|
213
|
+
})
|
|
214
|
+
</script>
|
|
215
|
+
|
|
216
|
+
<style>
|
|
217
|
+
[data-card-color-button] {
|
|
218
|
+
cursor: pointer;
|
|
219
|
+
}
|
|
220
|
+
[data-card-color-button][aria-pressed='true'] {
|
|
221
|
+
box-shadow:
|
|
222
|
+
0 0 0 2px var(--background),
|
|
223
|
+
0 0 0 4px var(--foreground);
|
|
224
|
+
}
|
|
225
|
+
</style>
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { env as cloudflareEnv } from 'cloudflare:workers'
|
|
3
|
+
import { getConfig } from '../state.js'
|
|
4
|
+
|
|
5
|
+
interface FwEnv {
|
|
6
|
+
PUBLIC_FW_STOREFRONT_TOKEN?: string
|
|
7
|
+
PUBLIC_FW_DEFAULT_CURRENCY?: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface Props {
|
|
11
|
+
storePath?: string
|
|
12
|
+
/** Fourthwall-hosted shop URL, rendered as a `noscript` fallback. */
|
|
13
|
+
fourthwallShopUrl?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const { storePath = '/store/', fourthwallShopUrl } = Astro.props
|
|
17
|
+
const config = getConfig()
|
|
18
|
+
const env = cloudflareEnv as FwEnv
|
|
19
|
+
const token = env?.PUBLIC_FW_STOREFRONT_TOKEN ?? ''
|
|
20
|
+
const currency = env?.PUBLIC_FW_DEFAULT_CURRENCY ?? config.defaultCurrency
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
<section class="mx-auto max-w-3xl space-y-8 px-4 pb-16 sm:px-6 lg:px-8 lg:pb-24">
|
|
24
|
+
<nav aria-label="Breadcrumb" class="text-sm uppercase tracking-[0.08em] text-[var(--muted-foreground)]">
|
|
25
|
+
<a href={storePath} class="underline-offset-2 transition-colors hover:text-[var(--foreground)] hover:underline">Store</a>
|
|
26
|
+
<span aria-hidden="true" class="mx-1.5">/</span>
|
|
27
|
+
<span class="text-[var(--foreground)]">Cart</span>
|
|
28
|
+
</nav>
|
|
29
|
+
|
|
30
|
+
<div class="space-y-2">
|
|
31
|
+
<h1 class="text-3xl font-semibold leading-tight">Review your order</h1>
|
|
32
|
+
<p class="text-[var(--muted-foreground)]">
|
|
33
|
+
Items are held in your browser. Sign in on Fourthwall's checkout to save shipping details for future orders.
|
|
34
|
+
</p>
|
|
35
|
+
</div>
|
|
36
|
+
|
|
37
|
+
{!config.checkoutEnabled ? (
|
|
38
|
+
<p role="status" class="text-[var(--muted-foreground)]">
|
|
39
|
+
Checkout isn't open for this store yet.
|
|
40
|
+
</p>
|
|
41
|
+
) : (
|
|
42
|
+
<div id="fw-cart-page" data-fw-token={token} data-fw-currency={currency} data-checkout-path={config.routes.checkoutPath}>
|
|
43
|
+
<p id="fw-cart-page-empty" class="text-[var(--muted-foreground)]">
|
|
44
|
+
Your cart is empty. <a class="underline" href={storePath}>Browse the store</a>.
|
|
45
|
+
</p>
|
|
46
|
+
|
|
47
|
+
<div id="fw-cart-page-contents" class="hidden space-y-6">
|
|
48
|
+
<ul id="fw-cart-page-lines" class="divide-y divide-[var(--border)]"></ul>
|
|
49
|
+
|
|
50
|
+
<div class="flex items-center justify-between border-t border-[var(--border)] pt-4 text-base">
|
|
51
|
+
<span class="text-[var(--muted-foreground)]">Subtotal</span>
|
|
52
|
+
<span id="fw-cart-page-subtotal" class="font-semibold">—</span>
|
|
53
|
+
</div>
|
|
54
|
+
|
|
55
|
+
<button
|
|
56
|
+
type="button"
|
|
57
|
+
id="fw-cart-page-checkout"
|
|
58
|
+
class="w-full cursor-pointer bg-[var(--primary,var(--foreground))] px-4 py-3 text-sm font-bold uppercase tracking-[0.14em] text-[var(--primary-foreground,var(--background))] transition hover:opacity-90 focus-visible:outline focus-visible:outline-2 focus-visible:outline-[var(--ring,var(--foreground))] disabled:cursor-not-allowed disabled:opacity-50"
|
|
59
|
+
disabled
|
|
60
|
+
>
|
|
61
|
+
Continue to Fourthwall checkout
|
|
62
|
+
</button>
|
|
63
|
+
<p class="text-xs text-[var(--muted-foreground)]">
|
|
64
|
+
You'll complete payment on Fourthwall. Sign in there for saved details.
|
|
65
|
+
</p>
|
|
66
|
+
{fourthwallShopUrl && (
|
|
67
|
+
<noscript>
|
|
68
|
+
<a href={fourthwallShopUrl} class="block w-full text-center text-sm underline">Continue on Fourthwall</a>
|
|
69
|
+
</noscript>
|
|
70
|
+
)}
|
|
71
|
+
</div>
|
|
72
|
+
</div>
|
|
73
|
+
)}
|
|
74
|
+
</section>
|
|
75
|
+
|
|
76
|
+
<script>
|
|
77
|
+
import { cart, type CartSnapshot } from '../cart-client.js'
|
|
78
|
+
import { initiateCheckout } from '../checkout-client.js'
|
|
79
|
+
import { optimizeFwImage } from '../image.js'
|
|
80
|
+
|
|
81
|
+
const page = document.getElementById('fw-cart-page')
|
|
82
|
+
const emptyEl = document.getElementById('fw-cart-page-empty')
|
|
83
|
+
const contents = document.getElementById('fw-cart-page-contents')
|
|
84
|
+
const linesEl = document.getElementById('fw-cart-page-lines')
|
|
85
|
+
const subtotalEl = document.getElementById('fw-cart-page-subtotal')
|
|
86
|
+
const checkoutBtn = document.getElementById('fw-cart-page-checkout') as HTMLButtonElement | null
|
|
87
|
+
|
|
88
|
+
const token = page?.getAttribute('data-fw-token') ?? ''
|
|
89
|
+
const currency = page?.getAttribute('data-fw-currency') ?? 'USD'
|
|
90
|
+
const checkoutPath = page?.getAttribute('data-checkout-path') ?? '/api/store/checkout'
|
|
91
|
+
if (token) cart.init({ token, currency })
|
|
92
|
+
|
|
93
|
+
function formatMoney(cents: number, cur: string): string {
|
|
94
|
+
try {
|
|
95
|
+
return new Intl.NumberFormat('en-US', { style: 'currency', currency: cur }).format(cents / 100)
|
|
96
|
+
} catch {
|
|
97
|
+
return `${cur} ${(cents / 100).toFixed(2)}`
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function escapeHtml(str: string): string {
|
|
102
|
+
return str
|
|
103
|
+
.replace(/&/g, '&')
|
|
104
|
+
.replace(/</g, '<')
|
|
105
|
+
.replace(/>/g, '>')
|
|
106
|
+
.replace(/"/g, '"')
|
|
107
|
+
.replace(/'/g, ''')
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function render(snap: CartSnapshot) {
|
|
111
|
+
if (!emptyEl || !contents || !linesEl || !subtotalEl || !checkoutBtn) return
|
|
112
|
+
if (snap.items.length === 0) {
|
|
113
|
+
emptyEl.classList.remove('hidden')
|
|
114
|
+
contents.classList.add('hidden')
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
emptyEl.classList.add('hidden')
|
|
118
|
+
contents.classList.remove('hidden')
|
|
119
|
+
linesEl.innerHTML = snap.items
|
|
120
|
+
.map((item) => {
|
|
121
|
+
const optSrc = optimizeFwImage(item.imageUrl, { width: 224 }) ?? item.imageUrl
|
|
122
|
+
const img = item.imageUrl
|
|
123
|
+
? `<img src="${optSrc}" alt="" loading="lazy" decoding="async" class="h-28 w-28 object-cover" />`
|
|
124
|
+
: `<div class="h-28 w-28 bg-[var(--muted)]"></div>`
|
|
125
|
+
const lineTotal = formatMoney(Math.round(item.priceValue * 100) * item.quantity, item.priceCurrency)
|
|
126
|
+
return `
|
|
127
|
+
<li class="flex items-center gap-5 py-6" data-line-id="${item.lineId}">
|
|
128
|
+
${img}
|
|
129
|
+
<div class="flex-1 space-y-2">
|
|
130
|
+
<p class="text-base font-semibold">${escapeHtml(item.name || 'Item')}</p>
|
|
131
|
+
<div class="flex items-center gap-2 text-[var(--muted-foreground)]">
|
|
132
|
+
<button type="button" aria-label="Decrease quantity" data-qty-down="${item.lineId}" class="cursor-pointer border border-[var(--border)] px-3 py-1 text-base leading-none transition-colors hover:border-[var(--foreground)] hover:text-[var(--foreground)]">-</button>
|
|
133
|
+
<span class="min-w-[1.5rem] text-center text-base">${item.quantity}</span>
|
|
134
|
+
<button type="button" aria-label="Increase quantity" data-qty-up="${item.lineId}" class="cursor-pointer border border-[var(--border)] px-3 py-1 text-base leading-none transition-colors hover:border-[var(--foreground)] hover:text-[var(--foreground)]">+</button>
|
|
135
|
+
</div>
|
|
136
|
+
</div>
|
|
137
|
+
<div class="flex flex-col items-end gap-1">
|
|
138
|
+
<span class="text-base font-semibold">${lineTotal}</span>
|
|
139
|
+
<button type="button" class="cursor-pointer text-sm text-[var(--muted-foreground)] underline transition-colors hover:text-[var(--foreground)]" data-remove="${item.lineId}">Remove</button>
|
|
140
|
+
</div>
|
|
141
|
+
</li>`
|
|
142
|
+
})
|
|
143
|
+
.join('')
|
|
144
|
+
subtotalEl.textContent = formatMoney(snap.totalCents, snap.currency)
|
|
145
|
+
checkoutBtn.disabled = false
|
|
146
|
+
wireLineControls(snap)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function wireLineControls(snap: CartSnapshot) {
|
|
150
|
+
linesEl?.querySelectorAll<HTMLButtonElement>('[data-qty-up]').forEach((btn) => {
|
|
151
|
+
btn.addEventListener('click', async () => {
|
|
152
|
+
const id = btn.getAttribute('data-qty-up')
|
|
153
|
+
const item = snap.items.find((i) => i.lineId === id)
|
|
154
|
+
if (!id || !item) return
|
|
155
|
+
btn.disabled = true
|
|
156
|
+
try {
|
|
157
|
+
await cart.update(id, item.quantity + 1)
|
|
158
|
+
} catch (err) {
|
|
159
|
+
console.error('cart update failed', err)
|
|
160
|
+
} finally {
|
|
161
|
+
btn.disabled = false
|
|
162
|
+
}
|
|
163
|
+
})
|
|
164
|
+
})
|
|
165
|
+
linesEl?.querySelectorAll<HTMLButtonElement>('[data-qty-down]').forEach((btn) => {
|
|
166
|
+
btn.addEventListener('click', async () => {
|
|
167
|
+
const id = btn.getAttribute('data-qty-down')
|
|
168
|
+
const item = snap.items.find((i) => i.lineId === id)
|
|
169
|
+
if (!id || !item) return
|
|
170
|
+
btn.disabled = true
|
|
171
|
+
try {
|
|
172
|
+
if (item.quantity <= 1) {
|
|
173
|
+
await cart.remove(id)
|
|
174
|
+
} else {
|
|
175
|
+
await cart.update(id, item.quantity - 1)
|
|
176
|
+
}
|
|
177
|
+
} catch (err) {
|
|
178
|
+
console.error('cart decrement failed', err)
|
|
179
|
+
} finally {
|
|
180
|
+
btn.disabled = false
|
|
181
|
+
}
|
|
182
|
+
})
|
|
183
|
+
})
|
|
184
|
+
linesEl?.querySelectorAll<HTMLButtonElement>('[data-remove]').forEach((btn) => {
|
|
185
|
+
btn.addEventListener('click', async () => {
|
|
186
|
+
const id = btn.getAttribute('data-remove')
|
|
187
|
+
if (!id) return
|
|
188
|
+
btn.disabled = true
|
|
189
|
+
try {
|
|
190
|
+
await cart.remove(id)
|
|
191
|
+
} catch (err) {
|
|
192
|
+
console.error('cart remove failed', err)
|
|
193
|
+
} finally {
|
|
194
|
+
btn.disabled = false
|
|
195
|
+
}
|
|
196
|
+
})
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
cart.on(render)
|
|
201
|
+
void cart.refresh().catch(() => {
|
|
202
|
+
/* noop — cached snapshot still visible */
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
checkoutBtn?.addEventListener('click', async () => {
|
|
206
|
+
const snap = cart.getSnapshot()
|
|
207
|
+
if (!snap.cartId) return
|
|
208
|
+
checkoutBtn.disabled = true
|
|
209
|
+
const original = checkoutBtn.textContent
|
|
210
|
+
checkoutBtn.textContent = 'Redirecting…'
|
|
211
|
+
try {
|
|
212
|
+
await initiateCheckout(snap, { source: 'cart-page', checkoutPath })
|
|
213
|
+
} catch (err) {
|
|
214
|
+
console.error(err)
|
|
215
|
+
checkoutBtn.textContent = original
|
|
216
|
+
checkoutBtn.disabled = false
|
|
217
|
+
}
|
|
218
|
+
})
|
|
219
|
+
</script>
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { env as cloudflareEnv } from 'cloudflare:workers'
|
|
3
|
+
import { cachedJson } from '../cache.js'
|
|
4
|
+
import { createFourthwallClient } from '../client.js'
|
|
5
|
+
import { filterPublishedCollections, getGroupedCollections, getStoreCollection } from '../collections.js'
|
|
6
|
+
import { getConfig } from '../state.js'
|
|
7
|
+
import type { FwProduct } from '../types.js'
|
|
8
|
+
import ProductCard from './ProductCard.astro'
|
|
9
|
+
|
|
10
|
+
interface FwEnv {
|
|
11
|
+
PUBLIC_FW_STOREFRONT_TOKEN?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface Props {
|
|
15
|
+
handle: string
|
|
16
|
+
basePath?: string
|
|
17
|
+
notFoundPath?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const { handle, basePath = '/store', notFoundPath = '/404/' } = Astro.props
|
|
21
|
+
const config = getConfig()
|
|
22
|
+
|
|
23
|
+
const collection = getStoreCollection(config.collections, handle)
|
|
24
|
+
if (!collection) {
|
|
25
|
+
return Astro.redirect(notFoundPath)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const env = cloudflareEnv as FwEnv
|
|
29
|
+
const token = env?.PUBLIC_FW_STOREFRONT_TOKEN ?? ''
|
|
30
|
+
const cookieCurrency = Astro.cookies.get('fw_currency')?.value
|
|
31
|
+
const currency = cookieCurrency && config.supportedCurrencies.includes(cookieCurrency) ? cookieCurrency : config.defaultCurrency
|
|
32
|
+
|
|
33
|
+
let products: FwProduct[] = []
|
|
34
|
+
let errorMessage: string | null = null
|
|
35
|
+
|
|
36
|
+
if (token) {
|
|
37
|
+
const client = createFourthwallClient({ token, defaultCurrency: currency })
|
|
38
|
+
try {
|
|
39
|
+
const PAGE_SIZE = 100
|
|
40
|
+
const firstPage = await cachedJson({
|
|
41
|
+
pathSegment: `/collections/${encodeURIComponent(collection.handle)}/products`,
|
|
42
|
+
query: { cur: currency, size: String(PAGE_SIZE) },
|
|
43
|
+
ttlSeconds: 300,
|
|
44
|
+
fetcher: () => client.getCollectionProducts({ handle: collection.handle, currency, size: PAGE_SIZE }),
|
|
45
|
+
})
|
|
46
|
+
products = [...firstPage.results]
|
|
47
|
+
|
|
48
|
+
// Fourthwall paginates collection results; keep pulling pages until the
|
|
49
|
+
// API says there are no more, with a hard ceiling of 10 pages.
|
|
50
|
+
let page = 2
|
|
51
|
+
let hasNextPage = firstPage.paging.hasNextPage
|
|
52
|
+
while (hasNextPage && page <= 10) {
|
|
53
|
+
const next = await cachedJson({
|
|
54
|
+
pathSegment: `/collections/${encodeURIComponent(collection.handle)}/products`,
|
|
55
|
+
query: { cur: currency, size: String(PAGE_SIZE), page: String(page) },
|
|
56
|
+
ttlSeconds: 300,
|
|
57
|
+
fetcher: () => client.getCollectionProducts({ handle: collection.handle, currency, size: PAGE_SIZE, page }),
|
|
58
|
+
})
|
|
59
|
+
products.push(...next.results)
|
|
60
|
+
hasNextPage = next.paging.hasNextPage
|
|
61
|
+
page += 1
|
|
62
|
+
}
|
|
63
|
+
} catch (err) {
|
|
64
|
+
errorMessage = err instanceof Error ? err.message : 'Could not load products.'
|
|
65
|
+
}
|
|
66
|
+
} else if (config.checkoutEnabled) {
|
|
67
|
+
errorMessage = "Store isn't configured yet."
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const getProductsForHandle = async (h: string) => {
|
|
71
|
+
if (!token) return []
|
|
72
|
+
const client = createFourthwallClient({ token, defaultCurrency: currency })
|
|
73
|
+
const result = await cachedJson({
|
|
74
|
+
pathSegment: `/collections/${encodeURIComponent(h)}/products`,
|
|
75
|
+
query: { cur: currency, nav: 'published' },
|
|
76
|
+
ttlSeconds: 300,
|
|
77
|
+
fetcher: () => client.getCollectionProducts({ handle: h, currency, size: 1 }),
|
|
78
|
+
})
|
|
79
|
+
return result.results
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const [divisionNav, typeNav] = await Promise.all([
|
|
83
|
+
filterPublishedCollections(getGroupedCollections(config.collections, 'division'), getProductsForHandle),
|
|
84
|
+
filterPublishedCollections(getGroupedCollections(config.collections, 'type'), getProductsForHandle),
|
|
85
|
+
])
|
|
86
|
+
const activeHandle = collection.handle
|
|
87
|
+
const useDenseGrid = products.length >= 6
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
<div class="lg:grid lg:grid-cols-[240px_minmax(0,1fr)] lg:gap-10">
|
|
91
|
+
<aside class="mb-10 lg:mb-0 lg:sticky lg:top-28 lg:self-start">
|
|
92
|
+
<nav class="space-y-10" aria-label="Store navigation">
|
|
93
|
+
{divisionNav.length > 0 && (
|
|
94
|
+
<div class="space-y-3">
|
|
95
|
+
<p class="text-xs font-semibold uppercase tracking-[0.14em] text-[var(--muted-foreground)]">Collections</p>
|
|
96
|
+
<ul class="space-y-1">
|
|
97
|
+
{divisionNav.map((c) => {
|
|
98
|
+
const active = c.handle === activeHandle
|
|
99
|
+
return (
|
|
100
|
+
<li>
|
|
101
|
+
<a
|
|
102
|
+
href={`${basePath}/collections/${c.handle}/`}
|
|
103
|
+
class:list={[
|
|
104
|
+
'block text-sm leading-snug transition-colors hover:text-[var(--foreground)]',
|
|
105
|
+
active
|
|
106
|
+
? 'border-l-[3px] border-l-[var(--foreground)] pl-[9px] py-1 font-semibold text-[var(--foreground)]'
|
|
107
|
+
: 'pl-3 py-1 font-normal text-[var(--muted-foreground)]',
|
|
108
|
+
]}
|
|
109
|
+
aria-current={active ? 'page' : undefined}
|
|
110
|
+
>
|
|
111
|
+
{c.name}
|
|
112
|
+
</a>
|
|
113
|
+
</li>
|
|
114
|
+
)
|
|
115
|
+
})}
|
|
116
|
+
</ul>
|
|
117
|
+
</div>
|
|
118
|
+
)}
|
|
119
|
+
|
|
120
|
+
{typeNav.length > 0 && (
|
|
121
|
+
<div class="space-y-3">
|
|
122
|
+
<p class="text-xs font-semibold uppercase tracking-[0.14em] text-[var(--muted-foreground)]">Shop by type</p>
|
|
123
|
+
<ul class="space-y-1">
|
|
124
|
+
{typeNav.map((c) => {
|
|
125
|
+
const active = c.handle === activeHandle
|
|
126
|
+
return (
|
|
127
|
+
<li>
|
|
128
|
+
<a
|
|
129
|
+
href={`${basePath}/collections/${c.handle}/`}
|
|
130
|
+
class:list={[
|
|
131
|
+
'block text-sm leading-snug transition-colors hover:text-[var(--foreground)]',
|
|
132
|
+
active
|
|
133
|
+
? 'border-l-[3px] border-l-[var(--foreground)] pl-[9px] py-1 font-semibold text-[var(--foreground)]'
|
|
134
|
+
: 'pl-3 py-1 font-normal text-[var(--muted-foreground)]',
|
|
135
|
+
]}
|
|
136
|
+
aria-current={active ? 'page' : undefined}
|
|
137
|
+
>
|
|
138
|
+
{c.name}
|
|
139
|
+
</a>
|
|
140
|
+
</li>
|
|
141
|
+
)
|
|
142
|
+
})}
|
|
143
|
+
</ul>
|
|
144
|
+
</div>
|
|
145
|
+
)}
|
|
146
|
+
</nav>
|
|
147
|
+
</aside>
|
|
148
|
+
|
|
149
|
+
<div class="space-y-8">
|
|
150
|
+
{errorMessage && (
|
|
151
|
+
<div role="alert" class="border border-[var(--border)] bg-[var(--card)] p-4 text-sm text-[var(--muted-foreground)]">
|
|
152
|
+
{errorMessage}
|
|
153
|
+
</div>
|
|
154
|
+
)}
|
|
155
|
+
|
|
156
|
+
{!errorMessage && products.length === 0 && <p class="text-[var(--muted-foreground)]">No products in this collection yet.</p>}
|
|
157
|
+
|
|
158
|
+
{products.length > 0 && (
|
|
159
|
+
<div class:list={['grid grid-cols-1 gap-x-8 gap-y-10 sm:grid-cols-2', useDenseGrid && 'lg:grid-cols-3']}>
|
|
160
|
+
{products.map((product, idx) => (
|
|
161
|
+
<ProductCard product={product} priority={idx < 3} imageTransformEnabled={config.imageTransform.enabled} basePath={basePath} />
|
|
162
|
+
))}
|
|
163
|
+
</div>
|
|
164
|
+
)}
|
|
165
|
+
</div>
|
|
166
|
+
</div>
|