create-nymbal-app 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/scaffold/templates/astro/package.json +25 -25
- package/scaffold/templates/astro/src/lib/api.ts +53 -21
- package/scaffold/templates/astro/src/pages/404.astro +3 -0
- package/scaffold/templates/astro/src/pages/account/orders/[orderNumber].astro +11 -3
- package/scaffold/templates/astro/src/pages/alerts/confirm.astro +59 -0
- package/scaffold/templates/astro/src/pages/alerts/unsubscribe.astro +53 -0
- package/scaffold/templates/astro/src/pages/category/[slug].astro +8 -6
- package/scaffold/templates/astro/src/pages/checkout.astro +2 -2
- package/scaffold/templates/astro/src/pages/index.astro +7 -0
- package/scaffold/templates/astro/src/pages/order/[orderNumber].astro +13 -2
- package/scaffold/templates/astro/src/pages/product/[slug].astro +20 -4
- package/scaffold/templates/astro/src/scripts/order-status.ts +3 -1
- package/scaffold/templates/astro/src/scripts/pdp.ts +5 -0
- package/scaffold/templates/astro/src/styles/pages/account.css +4 -0
- package/scaffold/templates/nextjs/app/(catalog)/home/[key]/page.tsx +6 -1
- package/scaffold/templates/nextjs/app/(catalog)/product/[slug]/page.tsx +22 -3
- package/scaffold/templates/nextjs/app/(shopper)/account/orders/[orderNumber]/page.tsx +10 -3
- package/scaffold/templates/nextjs/app/(shopper)/alerts/AlertLinkForm.tsx +90 -0
- package/scaffold/templates/nextjs/app/(shopper)/alerts/LinkInvalid.tsx +19 -0
- package/scaffold/templates/nextjs/app/(shopper)/alerts/actions.ts +33 -0
- package/scaffold/templates/nextjs/app/(shopper)/alerts/confirm/page.tsx +19 -0
- package/scaffold/templates/nextjs/app/(shopper)/alerts/unsubscribe/page.tsx +17 -0
- package/scaffold/templates/nextjs/app/(shopper)/checkout/CheckoutView.tsx +16 -9
- package/scaffold/templates/nextjs/app/(shopper)/not-found.tsx +6 -0
- package/scaffold/templates/nextjs/app/(shopper)/order/[orderNumber]/page.tsx +21 -5
- package/scaffold/templates/nextjs/app/not-found.tsx +1 -1
- package/scaffold/templates/nextjs/components/client/OrderStatusRefresh.tsx +8 -2
- package/scaffold/templates/nextjs/components/client/ProductInteractive.tsx +18 -1
- package/scaffold/templates/nextjs/components/client/elements.tsx +2 -0
- package/scaffold/templates/nextjs/lib/api.ts +55 -15
- package/scaffold/templates/nextjs/next.config.mjs +9 -0
- package/scaffold/templates/nextjs/package.json +26 -26
|
@@ -3,20 +3,22 @@ import '../../styles/pages/pdp.css'
|
|
|
3
3
|
import BaseLayout from '../../layouts/BaseLayout.astro'
|
|
4
4
|
import NymbalElement from '@nymbalhq/web-components/astro'
|
|
5
5
|
import { cardProductOf } from '@nymbalhq/web-components/ssr'
|
|
6
|
-
import {
|
|
6
|
+
import { loadProduct, fetchReviews, fetchProducts, fetchCapabilities } from '../../lib/api'
|
|
7
7
|
import { productJsonLd, breadcrumbJsonLd } from '../../lib/seo'
|
|
8
8
|
|
|
9
9
|
const { slug } = Astro.params
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
// Rejects when the API fails, so a failed read is a 500 and never a 404 for a product the store has.
|
|
12
|
+
const product = await loadProduct(slug!)
|
|
13
|
+
if (!product) return Astro.rewrite('/404')
|
|
13
14
|
|
|
14
|
-
const [reviewsResult, relatedResult] = await Promise.all([
|
|
15
|
+
const [reviewsResult, relatedResult, capabilities] = await Promise.all([
|
|
15
16
|
fetchReviews(product.id),
|
|
16
17
|
fetchProducts({
|
|
17
18
|
limit: 4,
|
|
18
19
|
category: product.categories?.[0]?.slug,
|
|
19
20
|
}),
|
|
21
|
+
fetchCapabilities(),
|
|
20
22
|
])
|
|
21
23
|
|
|
22
24
|
const reviews = reviewsResult.items ?? []
|
|
@@ -40,6 +42,10 @@ const breadcrumb = breadcrumbJsonLd([
|
|
|
40
42
|
{ name: product.name, url: `${Astro.url.origin}/product/${product.slug}` },
|
|
41
43
|
])
|
|
42
44
|
|
|
45
|
+
// The alert form only where the store's email reaches a mailbox (an API older than the report says nothing): back in
|
|
46
|
+
// stock for a variant out of stock, price drop for one in stock.
|
|
47
|
+
const offersAlerts = capabilities?.email?.delivers === true && defaultVariant !== undefined
|
|
48
|
+
|
|
43
49
|
// The selector derives its option groups from the product's variants.
|
|
44
50
|
const hasVariantChoice = (product.variants?.length ?? 0) > 1
|
|
45
51
|
---
|
|
@@ -93,6 +99,13 @@ const hasVariantChoice = (product.variants?.length ?? 0) > 1
|
|
|
93
99
|
</div>
|
|
94
100
|
</div>
|
|
95
101
|
|
|
102
|
+
{offersAlerts && (
|
|
103
|
+
<NymbalElement
|
|
104
|
+
tag="nymbal-notify-me"
|
|
105
|
+
attributes={{ 'product-id': product.id, 'variant-id': defaultVariant!.id, stock: String(stock) }}
|
|
106
|
+
/>
|
|
107
|
+
)}
|
|
108
|
+
|
|
96
109
|
{product.description && (
|
|
97
110
|
<div class="pdp-description" data-testid="product-description">
|
|
98
111
|
<Fragment set:html={product.description} />
|
|
@@ -116,6 +129,9 @@ const hasVariantChoice = (product.variants?.length ?? 0) > 1
|
|
|
116
129
|
/>
|
|
117
130
|
</section>
|
|
118
131
|
)}
|
|
132
|
+
|
|
133
|
+
<!-- The shopper's own list, read in the browser: a bare tag, so no shopper's list is in the page's HTML. -->
|
|
134
|
+
<nymbal-recently-viewed data-testid="recently-viewed" current={product.id}></nymbal-recently-viewed>
|
|
119
135
|
</div>
|
|
120
136
|
</BaseLayout>
|
|
121
137
|
|
|
@@ -8,12 +8,14 @@ const POLL_FOR_MS = 10_000
|
|
|
8
8
|
const root = document.querySelector<HTMLElement>('[data-order-number][data-order-status]')
|
|
9
9
|
const orderNumber = root?.dataset.orderNumber
|
|
10
10
|
const shown = root?.dataset.orderStatus
|
|
11
|
+
// The page's own key, from its URL: the API answers a guest's order to its key only.
|
|
12
|
+
const key = new URLSearchParams(window.location.search).get('key')
|
|
11
13
|
|
|
12
14
|
if (orderNumber && shown === 'pending') {
|
|
13
15
|
const until = Date.now() + POLL_FOR_MS
|
|
14
16
|
const poll = async (): Promise<void> => {
|
|
15
17
|
try {
|
|
16
|
-
const order = await client.customer.orders.get(orderNumber)
|
|
18
|
+
const order = await client.customer.orders.get(orderNumber, key ? { key } : {})
|
|
17
19
|
if (order.status !== shown) {
|
|
18
20
|
window.location.reload()
|
|
19
21
|
return
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { NymbalAddToCart } from '@nymbalhq/web-components/elements/nymbal-add-to-cart'
|
|
2
2
|
import { NymbalBreadcrumb } from '@nymbalhq/web-components/elements/nymbal-breadcrumb'
|
|
3
3
|
import { NymbalImageGallery } from '@nymbalhq/web-components/elements/nymbal-image-gallery'
|
|
4
|
+
import { NymbalNotifyMe } from '@nymbalhq/web-components/elements/nymbal-notify-me'
|
|
4
5
|
import { NymbalProductGrid } from '@nymbalhq/web-components/elements/nymbal-product-grid'
|
|
5
6
|
import { NymbalProductPrice } from '@nymbalhq/web-components/elements/nymbal-product-price'
|
|
6
7
|
import { NymbalQuantitySelector } from '@nymbalhq/web-components/elements/nymbal-quantity-selector'
|
|
8
|
+
import { NymbalRecentlyViewed } from '@nymbalhq/web-components/elements/nymbal-recently-viewed'
|
|
7
9
|
import { NymbalReviewForm } from '@nymbalhq/web-components/elements/nymbal-review-form'
|
|
8
10
|
import { NymbalReviewList } from '@nymbalhq/web-components/elements/nymbal-review-list'
|
|
9
11
|
import { NymbalStockBadge } from '@nymbalhq/web-components/elements/nymbal-stock-badge'
|
|
@@ -19,8 +21,11 @@ NymbalQuantitySelector.define()
|
|
|
19
21
|
NymbalAddToCart.define()
|
|
20
22
|
NymbalReviewList.define()
|
|
21
23
|
NymbalReviewForm.define()
|
|
24
|
+
NymbalRecentlyViewed.define()
|
|
22
25
|
// The related grid is there only when the product has relations.
|
|
23
26
|
if (document.querySelector('nymbal-product-grid') !== null) NymbalProductGrid.define()
|
|
27
|
+
// The back-in-stock form is there only when the store's email delivers.
|
|
28
|
+
if (document.querySelector('nymbal-notify-me') !== null) NymbalNotifyMe.define()
|
|
24
29
|
|
|
25
30
|
const slug = document.querySelector<HTMLElement>('[data-product-slug]')?.dataset.productSlug
|
|
26
31
|
const addRow = document.querySelector<HTMLElement>('.pdp-add-row')
|
|
@@ -33,7 +33,10 @@
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
/* A `1fr` track is at least its content's min-content width: without `min-width: 0` and `overflow-wrap`, a long
|
|
37
|
+
email (one unbreakable word) widens its card past a phone's viewport. */
|
|
36
38
|
.account-stat-card {
|
|
39
|
+
min-width: 0;
|
|
37
40
|
padding: var(--nymbal-spacing-md, 1rem);
|
|
38
41
|
background-color: #fafafa;
|
|
39
42
|
border: 1px solid var(--nymbal-color-border, #e4e4e7);
|
|
@@ -49,6 +52,7 @@
|
|
|
49
52
|
}
|
|
50
53
|
|
|
51
54
|
.account-stat-value {
|
|
55
|
+
overflow-wrap: anywhere;
|
|
52
56
|
font-size: 1.25rem;
|
|
53
57
|
font-weight: var(--nymbal-font-weight-bold, 700);
|
|
54
58
|
color: var(--nymbal-color-primary, #18181b);
|
|
@@ -5,7 +5,7 @@ import { fetchProducts, fetchCategories } from '@/lib/api'
|
|
|
5
5
|
import { cardImageOf, cardProductOf, renderNymbal } from '@/lib/render'
|
|
6
6
|
import { organizationJsonLd } from '@/lib/seo'
|
|
7
7
|
import { HeroSection } from '@/components/server/HeroSection'
|
|
8
|
-
import { CategoryNav, ProductGrid } from '@/components/client/elements'
|
|
8
|
+
import { CategoryNav, ProductGrid, RecentlyViewed } from '@/components/client/elements'
|
|
9
9
|
import { NewsletterSignup } from '@/components/client/NewsletterSignup'
|
|
10
10
|
import styles from '@/styles/pages/home.module.css'
|
|
11
11
|
|
|
@@ -75,6 +75,11 @@ export default async function HomePage({ params }: HomePageProps) {
|
|
|
75
75
|
</section>
|
|
76
76
|
|
|
77
77
|
<NewsletterSignup />
|
|
78
|
+
|
|
79
|
+
{/* The shopper's own list, read in the browser: never in this cached page's HTML. */}
|
|
80
|
+
<div className="container">
|
|
81
|
+
<RecentlyViewed data-testid="recently-viewed" />
|
|
82
|
+
</div>
|
|
78
83
|
</>
|
|
79
84
|
)
|
|
80
85
|
}
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import type { Metadata } from 'next'
|
|
2
2
|
import { notFound } from 'next/navigation'
|
|
3
3
|
import { preload } from 'react-dom'
|
|
4
|
-
import { fetchProduct, fetchProducts, fetchReviews } from '@/lib/api'
|
|
4
|
+
import { fetchCapabilities, fetchProduct, fetchProducts, fetchReviews } from '@/lib/api'
|
|
5
5
|
import { cardProductOf, renderNymbal } from '@/lib/render'
|
|
6
6
|
import { productJsonLd } from '@/lib/seo'
|
|
7
7
|
import {
|
|
8
8
|
Breadcrumb,
|
|
9
9
|
ImageGallery,
|
|
10
10
|
ProductGrid,
|
|
11
|
+
RecentlyViewed,
|
|
11
12
|
ReviewForm,
|
|
12
13
|
ReviewList,
|
|
13
14
|
} from '@/components/client/elements'
|
|
@@ -46,9 +47,10 @@ export default async function ProductPage({ params }: ProductPageProps) {
|
|
|
46
47
|
|
|
47
48
|
if (!product) notFound()
|
|
48
49
|
|
|
49
|
-
const [{ items: reviews }, { items: relatedProducts }] = await Promise.all([
|
|
50
|
+
const [{ items: reviews }, { items: relatedProducts }, capabilities] = await Promise.all([
|
|
50
51
|
fetchReviews(product.id),
|
|
51
52
|
fetchProducts({ limit: 4 }),
|
|
53
|
+
fetchCapabilities(),
|
|
52
54
|
])
|
|
53
55
|
|
|
54
56
|
const related = relatedProducts
|
|
@@ -58,6 +60,9 @@ export default async function ProductPage({ params }: ProductPageProps) {
|
|
|
58
60
|
|
|
59
61
|
// The product's elements as the server renders them for its first variant, which ProductInteractive shows first.
|
|
60
62
|
const variant = product.variants?.[0]
|
|
63
|
+
// The alert form only where the store's email reaches a mailbox (an API older than the report says nothing): back in
|
|
64
|
+
// stock for a variant out of stock, price drop for one in stock, following the selected variant once hydrated.
|
|
65
|
+
const offersAlerts = capabilities?.email?.delivers === true && variant !== undefined
|
|
61
66
|
const serverHtml = {
|
|
62
67
|
price: renderNymbal('nymbal-product-price', {
|
|
63
68
|
'product-id': product.id,
|
|
@@ -76,6 +81,13 @@ export default async function ProductPage({ params }: ProductPageProps) {
|
|
|
76
81
|
qty: '1',
|
|
77
82
|
...((variant?.stock ?? 0) <= 0 && { disabled: '' }),
|
|
78
83
|
}).__html,
|
|
84
|
+
notifyMe: offersAlerts
|
|
85
|
+
? renderNymbal('nymbal-notify-me', {
|
|
86
|
+
'product-id': product.id,
|
|
87
|
+
'variant-id': variant.id,
|
|
88
|
+
stock: String(variant.stock ?? 0),
|
|
89
|
+
}).__html
|
|
90
|
+
: undefined,
|
|
79
91
|
}
|
|
80
92
|
|
|
81
93
|
const baseUrl = process.env.NEXT_PUBLIC_SITE_URL ?? 'http://localhost:3000'
|
|
@@ -122,7 +134,11 @@ export default async function ProductPage({ params }: ProductPageProps) {
|
|
|
122
134
|
/>
|
|
123
135
|
|
|
124
136
|
<div className={styles.info}>
|
|
125
|
-
<ProductInteractive
|
|
137
|
+
<ProductInteractive
|
|
138
|
+
product={product}
|
|
139
|
+
offersAlerts={offersAlerts}
|
|
140
|
+
serverHtml={serverHtml}
|
|
141
|
+
/>
|
|
126
142
|
|
|
127
143
|
{product.description && (
|
|
128
144
|
<div className={styles.description}>
|
|
@@ -176,6 +192,9 @@ export default async function ProductPage({ params }: ProductPageProps) {
|
|
|
176
192
|
/>
|
|
177
193
|
</section>
|
|
178
194
|
)}
|
|
195
|
+
|
|
196
|
+
{/* The shopper's own list, read in the browser: never in this cached page's HTML. */}
|
|
197
|
+
<RecentlyViewed current={product.id} data-testid="recently-viewed" />
|
|
179
198
|
</div>
|
|
180
199
|
</>
|
|
181
200
|
)
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
import { useState, useEffect } from 'react'
|
|
4
4
|
import Link from 'next/link'
|
|
5
5
|
import { useParams } from 'next/navigation'
|
|
6
|
-
import { useNymbalClient } from '@nymbalhq/react'
|
|
7
|
-
import type {
|
|
6
|
+
import { OrderTimeline, useNymbalClient } from '@nymbalhq/react'
|
|
7
|
+
import type { ShopperOrder } from '@nymbalhq/types'
|
|
8
8
|
import { formatPrice, formatDate } from '@/lib/format'
|
|
9
9
|
import styles from '@/styles/pages/account.module.css'
|
|
10
10
|
import confirmStyles from '@/styles/pages/confirmation.module.css'
|
|
@@ -13,7 +13,7 @@ export default function OrderDetailPage() {
|
|
|
13
13
|
const params = useParams<{ orderNumber: string }>()
|
|
14
14
|
const orderNumber = decodeURIComponent(params.orderNumber ?? '')
|
|
15
15
|
const client = useNymbalClient()
|
|
16
|
-
const [order, setOrder] = useState<
|
|
16
|
+
const [order, setOrder] = useState<ShopperOrder | null>(null)
|
|
17
17
|
const [loading, setLoading] = useState(true)
|
|
18
18
|
|
|
19
19
|
useEffect(() => {
|
|
@@ -91,6 +91,13 @@ export default function OrderDetailPage() {
|
|
|
91
91
|
<p className="text-sm text-muted mb-md">Placed on {formatDate(order.createdAt)}</p>
|
|
92
92
|
)}
|
|
93
93
|
|
|
94
|
+
{order.timeline !== undefined && order.timeline.length > 0 && (
|
|
95
|
+
<OrderTimeline
|
|
96
|
+
entries={order.timeline}
|
|
97
|
+
style={{ marginBottom: 'var(--nymbal-spacing-lg)' }}
|
|
98
|
+
/>
|
|
99
|
+
)}
|
|
100
|
+
|
|
94
101
|
<div className={styles.formCard} style={{ marginBottom: 'var(--nymbal-spacing-lg)' }}>
|
|
95
102
|
<h3 className={styles.formCardTitle}>Items</h3>
|
|
96
103
|
<div className={confirmStyles.lineItems}>
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import Link from 'next/link'
|
|
4
|
+
import { useActionState } from 'react'
|
|
5
|
+
import styles from '@/styles/pages/error.module.css'
|
|
6
|
+
import { confirmAlertAction, unsubscribeAlertAction } from './actions'
|
|
7
|
+
import { LinkInvalid } from './LinkInvalid'
|
|
8
|
+
|
|
9
|
+
const PAGES = {
|
|
10
|
+
confirm: {
|
|
11
|
+
action: confirmAlertAction,
|
|
12
|
+
title: 'Confirm your email alert',
|
|
13
|
+
message: 'Press the button to start the alert this email asked for.',
|
|
14
|
+
label: 'Confirm alert',
|
|
15
|
+
invalid: 'It may be from an older email, or the alert has been stopped.',
|
|
16
|
+
},
|
|
17
|
+
unsubscribe: {
|
|
18
|
+
action: unsubscribeAlertAction,
|
|
19
|
+
title: 'Stop this email alert',
|
|
20
|
+
message: "Press the button and we won't email you about this product again.",
|
|
21
|
+
label: 'Unsubscribe',
|
|
22
|
+
invalid: 'Check that the whole link from the email was opened.',
|
|
23
|
+
},
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* An alert link's page: opening it changes nothing, so a mail scanner that fetches it, or runs its scripts, leaves
|
|
28
|
+
* the alert as it was. The button posts the token to a server action, with JavaScript on or off.
|
|
29
|
+
*/
|
|
30
|
+
export function AlertLinkForm({ kind, token }: { kind: keyof typeof PAGES; token: string }) {
|
|
31
|
+
const page = PAGES[kind]
|
|
32
|
+
const [state, formAction, pending] = useActionState(page.action, null)
|
|
33
|
+
|
|
34
|
+
if (state === null) {
|
|
35
|
+
return (
|
|
36
|
+
<form action={formAction} className={styles.page} data-testid={`alert-${kind}-form`}>
|
|
37
|
+
<div className={styles.wrapper}>
|
|
38
|
+
<h1 className={styles.title}>{page.title}</h1>
|
|
39
|
+
<p className={styles.message}>{page.message}</p>
|
|
40
|
+
<input type="hidden" name="token" value={token} />
|
|
41
|
+
<div className={styles.actions}>
|
|
42
|
+
<button
|
|
43
|
+
type="submit"
|
|
44
|
+
className={styles.homeLink}
|
|
45
|
+
data-testid={`alert-${kind}-submit`}
|
|
46
|
+
disabled={pending}
|
|
47
|
+
>
|
|
48
|
+
{page.label}
|
|
49
|
+
</button>
|
|
50
|
+
</div>
|
|
51
|
+
</div>
|
|
52
|
+
</form>
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
if (state.result === 'invalid') return <LinkInvalid message={page.invalid} />
|
|
56
|
+
if (state.result === 'unsubscribed') {
|
|
57
|
+
return (
|
|
58
|
+
<div className={styles.page} data-testid="alert-unsubscribed">
|
|
59
|
+
<div className={styles.wrapper}>
|
|
60
|
+
<h1 className={styles.title}>Your alert is off</h1>
|
|
61
|
+
<p className={styles.message}>We won't email you about this product again.</p>
|
|
62
|
+
<div className={styles.actions}>
|
|
63
|
+
<Link href="/products" className={styles.homeLink}>
|
|
64
|
+
Browse Products
|
|
65
|
+
</Link>
|
|
66
|
+
</div>
|
|
67
|
+
</div>
|
|
68
|
+
</div>
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
const { kind: alertKind, product } = state.confirmation
|
|
72
|
+
return (
|
|
73
|
+
<div className={styles.page} data-testid="alert-confirmed" data-kind={alertKind}>
|
|
74
|
+
<div className={styles.wrapper}>
|
|
75
|
+
<h1 className={styles.title}>Your alert is on</h1>
|
|
76
|
+
{alertKind === 'price_drop' ? (
|
|
77
|
+
<p className={styles.message}>
|
|
78
|
+
We'll email you when the price of{' '}
|
|
79
|
+
<Link href={`/product/${product.slug}`}>{product.name}</Link> drops.
|
|
80
|
+
</p>
|
|
81
|
+
) : (
|
|
82
|
+
<p className={styles.message}>
|
|
83
|
+
We'll email you once when <Link href={`/product/${product.slug}`}>{product.name}</Link>{' '}
|
|
84
|
+
is back in stock.
|
|
85
|
+
</p>
|
|
86
|
+
)}
|
|
87
|
+
</div>
|
|
88
|
+
</div>
|
|
89
|
+
)
|
|
90
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import Link from 'next/link'
|
|
2
|
+
import styles from '@/styles/pages/error.module.css'
|
|
3
|
+
|
|
4
|
+
/** What an alert link that the store refuses shows: an older email's confirmation, or a link cut short. */
|
|
5
|
+
export function LinkInvalid({ message }: { message: string }) {
|
|
6
|
+
return (
|
|
7
|
+
<div className={styles.page} data-testid="alert-link-invalid">
|
|
8
|
+
<div className={styles.wrapper}>
|
|
9
|
+
<h1 className={styles.title}>This link is not valid</h1>
|
|
10
|
+
<p className={styles.message}>{message}</p>
|
|
11
|
+
<div className={styles.actions}>
|
|
12
|
+
<Link href="/products" className={styles.homeLink}>
|
|
13
|
+
Browse Products
|
|
14
|
+
</Link>
|
|
15
|
+
</div>
|
|
16
|
+
</div>
|
|
17
|
+
</div>
|
|
18
|
+
)
|
|
19
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use server'
|
|
2
|
+
|
|
3
|
+
import { confirmAlert, unsubscribeAlert, type AlertConfirmation } from '@/lib/api'
|
|
4
|
+
|
|
5
|
+
/** What a press of an alert page's button shows. */
|
|
6
|
+
export type AlertLinkResult =
|
|
7
|
+
| { result: 'confirmed'; confirmation: AlertConfirmation }
|
|
8
|
+
| { result: 'unsubscribed' }
|
|
9
|
+
| { result: 'invalid' }
|
|
10
|
+
|
|
11
|
+
function tokenOf(form: FormData): string {
|
|
12
|
+
const token = form.get('token')
|
|
13
|
+
return typeof token === 'string' ? token : ''
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// The token is posted on from this server, so the browser needs no CSRF cookie.
|
|
17
|
+
export async function confirmAlertAction(
|
|
18
|
+
_previous: AlertLinkResult | null,
|
|
19
|
+
form: FormData,
|
|
20
|
+
): Promise<AlertLinkResult> {
|
|
21
|
+
const token = tokenOf(form)
|
|
22
|
+
const confirmation = token === '' ? null : await confirmAlert(token)
|
|
23
|
+
return confirmation ? { result: 'confirmed', confirmation } : { result: 'invalid' }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function unsubscribeAlertAction(
|
|
27
|
+
_previous: AlertLinkResult | null,
|
|
28
|
+
form: FormData,
|
|
29
|
+
): Promise<AlertLinkResult> {
|
|
30
|
+
const token = tokenOf(form)
|
|
31
|
+
const stopped = token === '' ? false : await unsubscribeAlert(token)
|
|
32
|
+
return stopped ? { result: 'unsubscribed' } : { result: 'invalid' }
|
|
33
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Metadata } from 'next'
|
|
2
|
+
import { AlertLinkForm } from '../AlertLinkForm'
|
|
3
|
+
import { LinkInvalid } from '../LinkInvalid'
|
|
4
|
+
|
|
5
|
+
export const metadata: Metadata = { title: 'Email alert' }
|
|
6
|
+
|
|
7
|
+
interface ConfirmPageProps {
|
|
8
|
+
searchParams: Promise<{ token?: string | string[] }>
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// The link in a confirmation email: it shows the button and calls no API until the button is pressed.
|
|
12
|
+
export default async function ConfirmAlertPage({ searchParams }: ConfirmPageProps) {
|
|
13
|
+
const { token } = await searchParams
|
|
14
|
+
const value = typeof token === 'string' ? token : ''
|
|
15
|
+
if (value === '') {
|
|
16
|
+
return <LinkInvalid message="It may be from an older email, or the alert has been stopped." />
|
|
17
|
+
}
|
|
18
|
+
return <AlertLinkForm kind="confirm" token={value} />
|
|
19
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Metadata } from 'next'
|
|
2
|
+
import { AlertLinkForm } from '../AlertLinkForm'
|
|
3
|
+
import { LinkInvalid } from '../LinkInvalid'
|
|
4
|
+
|
|
5
|
+
export const metadata: Metadata = { title: 'Back-in-stock alert' }
|
|
6
|
+
|
|
7
|
+
interface UnsubscribePageProps {
|
|
8
|
+
searchParams: Promise<{ token?: string | string[] }>
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// The unsubscribe link every alert email carries: it shows the button and calls no API until the button is pressed.
|
|
12
|
+
export default async function UnsubscribeAlertPage({ searchParams }: UnsubscribePageProps) {
|
|
13
|
+
const { token } = await searchParams
|
|
14
|
+
const value = typeof token === 'string' ? token : ''
|
|
15
|
+
if (value === '') return <LinkInvalid message="Check that the whole link from the email was opened." />
|
|
16
|
+
return <AlertLinkForm kind="unsubscribe" token={value} />
|
|
17
|
+
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
|
|
3
3
|
import { useCallback, useEffect } from 'react'
|
|
4
|
-
import { useRouter } from 'next/navigation'
|
|
5
4
|
import { Checkout } from '@nymbalhq/react'
|
|
6
5
|
import { NymbalPaymentElement } from '@nymbalhq/web-components/elements/nymbal-payment-element'
|
|
7
6
|
import type { CartProjection } from '@/lib/api'
|
|
@@ -10,14 +9,22 @@ import type { CartProjection } from '@/lib/api'
|
|
|
10
9
|
* The element places the order; the page only goes to it. The server rendered the checkout with its payment element
|
|
11
10
|
* as a light-DOM child (`serverHtml`), which is defined here: it stays mounted by its driver in the browser.
|
|
12
11
|
*/
|
|
13
|
-
export function CheckoutView({
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
12
|
+
export function CheckoutView({
|
|
13
|
+
cart,
|
|
14
|
+
serverHtml,
|
|
15
|
+
}: {
|
|
16
|
+
cart: CartProjection | null
|
|
17
|
+
serverHtml: string
|
|
18
|
+
}) {
|
|
19
|
+
// A full load, not router.push: the order page's URL carries its key, and only a document of its own takes the
|
|
20
|
+
// page's Referrer-Policy header. A soft navigation leaves it in the checkout's document, whose requests (its
|
|
21
|
+
// script chunks among them) refer the keyed URL on.
|
|
22
|
+
const onPlaced = useCallback((event: CustomEvent<{ orderNumber: string; orderKey?: string }>) => {
|
|
23
|
+
const { orderNumber, orderKey } = event.detail
|
|
24
|
+
window.location.assign(
|
|
25
|
+
`/order/${encodeURIComponent(orderNumber)}${orderKey ? `?key=${encodeURIComponent(orderKey)}` : ''}`,
|
|
26
|
+
)
|
|
27
|
+
}, [])
|
|
21
28
|
|
|
22
29
|
useEffect(() => {
|
|
23
30
|
NymbalPaymentElement.define()
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { Metadata } from 'next'
|
|
2
2
|
import { notFound } from 'next/navigation'
|
|
3
3
|
import Link from 'next/link'
|
|
4
|
-
import {
|
|
5
|
-
import { OrderSummary } from '@/components/client/elements'
|
|
4
|
+
import { loadOrder } from '@/lib/api'
|
|
5
|
+
import { OrderSummary, OrderTimeline } from '@/components/client/elements'
|
|
6
6
|
import { renderNymbal } from '@/lib/render'
|
|
7
7
|
import { OrderStatusRefresh } from '@/components/client/OrderStatusRefresh'
|
|
8
8
|
import { isPaid, orderHeading } from '@/lib/order-status'
|
|
@@ -10,12 +10,16 @@ import styles from '@/styles/pages/confirmation.module.css'
|
|
|
10
10
|
|
|
11
11
|
interface OrderPageProps {
|
|
12
12
|
params: Promise<{ orderNumber: string }>
|
|
13
|
+
searchParams: Promise<{ key?: string | string[] }>
|
|
13
14
|
}
|
|
14
15
|
|
|
16
|
+
// The URL carries the order's key (`?key=`), so nothing this page links to or loads may be told the URL: the meta
|
|
17
|
+
// tag here, and the header next.config.mjs sets on every /order/<n> response, the not-found render's too.
|
|
15
18
|
export async function generateMetadata({ params }: OrderPageProps): Promise<Metadata> {
|
|
16
19
|
const { orderNumber } = await params
|
|
17
20
|
return {
|
|
18
21
|
title: `Order ${orderNumber}`,
|
|
22
|
+
referrer: 'no-referrer',
|
|
19
23
|
}
|
|
20
24
|
}
|
|
21
25
|
|
|
@@ -54,9 +58,12 @@ function AddressBlock({
|
|
|
54
58
|
)
|
|
55
59
|
}
|
|
56
60
|
|
|
57
|
-
export default async function OrderPage({ params }: OrderPageProps) {
|
|
61
|
+
export default async function OrderPage({ params, searchParams }: OrderPageProps) {
|
|
58
62
|
const { orderNumber } = await params
|
|
59
|
-
const
|
|
63
|
+
const { key: keyParam } = await searchParams
|
|
64
|
+
const key = Array.isArray(keyParam) ? keyParam[0] : keyParam
|
|
65
|
+
// Rejects when the API fails, so a failed read is a 500 and never a 404 for an order the shopper has.
|
|
66
|
+
const order = await loadOrder(orderNumber, key)
|
|
60
67
|
|
|
61
68
|
if (!order) notFound()
|
|
62
69
|
const paid = isPaid(order.status)
|
|
@@ -75,10 +82,11 @@ export default async function OrderPage({ params }: OrderPageProps) {
|
|
|
75
82
|
'total-minor': String(order.totalMinor),
|
|
76
83
|
currency: order.currency,
|
|
77
84
|
}
|
|
85
|
+
const timeline = order.timeline ?? []
|
|
78
86
|
|
|
79
87
|
return (
|
|
80
88
|
<div className={`container ${styles.page}`}>
|
|
81
|
-
<OrderStatusRefresh orderNumber={order.orderNumber} status={order.status} />
|
|
89
|
+
<OrderStatusRefresh orderNumber={order.orderNumber} orderKey={key} status={order.status} />
|
|
82
90
|
<div className={styles.wrapper}>
|
|
83
91
|
<div className={styles.successHeader}>
|
|
84
92
|
{paid && (
|
|
@@ -112,6 +120,14 @@ export default async function OrderPage({ params }: OrderPageProps) {
|
|
|
112
120
|
</p>
|
|
113
121
|
</div>
|
|
114
122
|
|
|
123
|
+
<OrderTimeline
|
|
124
|
+
className={styles.summary}
|
|
125
|
+
entries={timeline}
|
|
126
|
+
serverHtml={
|
|
127
|
+
renderNymbal('nymbal-order-timeline', { entries: JSON.stringify(timeline) }).__html
|
|
128
|
+
}
|
|
129
|
+
/>
|
|
130
|
+
|
|
115
131
|
<OrderSummary
|
|
116
132
|
className={styles.summary}
|
|
117
133
|
items={items}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { PlainHeader } from '@/components/server/Header'
|
|
2
2
|
import { NotFoundView } from '@/components/server/NotFoundView'
|
|
3
3
|
|
|
4
|
-
// A URL no route matches
|
|
4
|
+
// A URL no route matches.
|
|
5
5
|
export default function NotFound() {
|
|
6
6
|
return (
|
|
7
7
|
<>
|
|
@@ -12,9 +12,12 @@ const POLL_FOR_MS = 10_000
|
|
|
12
12
|
|
|
13
13
|
export function OrderStatusRefresh({
|
|
14
14
|
orderNumber,
|
|
15
|
+
orderKey,
|
|
15
16
|
status,
|
|
16
17
|
}: {
|
|
17
18
|
orderNumber: string
|
|
19
|
+
/** The page's `?key=`: the API answers a guest's order to its key only. */
|
|
20
|
+
orderKey?: string | undefined
|
|
18
21
|
status: OrderStatus
|
|
19
22
|
}) {
|
|
20
23
|
const router = useRouter()
|
|
@@ -26,7 +29,10 @@ export function OrderStatusRefresh({
|
|
|
26
29
|
let stopped = false
|
|
27
30
|
const poll = async () => {
|
|
28
31
|
try {
|
|
29
|
-
const order = await getNymbalClient().customer.orders.get(
|
|
32
|
+
const order = await getNymbalClient().customer.orders.get(
|
|
33
|
+
orderNumber,
|
|
34
|
+
orderKey ? { key: orderKey } : {},
|
|
35
|
+
)
|
|
30
36
|
if (stopped) return
|
|
31
37
|
if (order.status !== status) {
|
|
32
38
|
router.refresh()
|
|
@@ -42,7 +48,7 @@ export function OrderStatusRefresh({
|
|
|
42
48
|
stopped = true
|
|
43
49
|
clearTimeout(timer)
|
|
44
50
|
}
|
|
45
|
-
}, [orderNumber, status, router])
|
|
51
|
+
}, [orderNumber, orderKey, status, router])
|
|
46
52
|
|
|
47
53
|
return null
|
|
48
54
|
}
|
|
@@ -9,11 +9,14 @@ import {
|
|
|
9
9
|
ProductPrice,
|
|
10
10
|
QuantitySelector,
|
|
11
11
|
StockBadge,
|
|
12
|
+
NotifyMe,
|
|
12
13
|
} from '@nymbalhq/react'
|
|
13
14
|
import styles from '@/styles/pages/pdp.module.css'
|
|
14
15
|
|
|
15
16
|
interface ProductInteractiveProps {
|
|
16
17
|
product: DenormalisedProduct
|
|
18
|
+
/** The store's email delivers, so the page offers a back-in-stock alert (GET /api/capabilities). */
|
|
19
|
+
offersAlerts?: boolean
|
|
17
20
|
/** The server's render of each element, for the product's first variant (app/product/[slug]/page.tsx). */
|
|
18
21
|
serverHtml?: {
|
|
19
22
|
price: string
|
|
@@ -21,10 +24,15 @@ interface ProductInteractiveProps {
|
|
|
21
24
|
variantSelector: string
|
|
22
25
|
quantity: string
|
|
23
26
|
addToCart: string
|
|
27
|
+
notifyMe?: string | undefined
|
|
24
28
|
}
|
|
25
29
|
}
|
|
26
30
|
|
|
27
|
-
export function ProductInteractive({
|
|
31
|
+
export function ProductInteractive({
|
|
32
|
+
product,
|
|
33
|
+
offersAlerts = false,
|
|
34
|
+
serverHtml,
|
|
35
|
+
}: ProductInteractiveProps) {
|
|
28
36
|
const { selectedVariant } = useProduct(product.slug)
|
|
29
37
|
|
|
30
38
|
const variant = selectedVariant ?? product.variants?.[0]
|
|
@@ -92,6 +100,15 @@ export function ProductInteractive({ product, serverHtml }: ProductInteractivePr
|
|
|
92
100
|
serverHtml={serverHtml?.addToCart}
|
|
93
101
|
/>
|
|
94
102
|
</div>
|
|
103
|
+
|
|
104
|
+
{offersAlerts && first !== undefined && (
|
|
105
|
+
<NotifyMe
|
|
106
|
+
productId={product.id}
|
|
107
|
+
variantId={first.id}
|
|
108
|
+
stock={first.stock ?? 0}
|
|
109
|
+
serverHtml={serverHtml?.notifyMe}
|
|
110
|
+
/>
|
|
111
|
+
)}
|
|
95
112
|
</div>
|
|
96
113
|
)
|
|
97
114
|
}
|