create-nymbal-app 0.5.0 → 0.7.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 +12 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/scaffold/templates/astro/astro.config.mjs +3 -0
- package/scaffold/templates/astro/package.json +25 -25
- package/scaffold/templates/astro/src/layouts/AccountLayout.astro +1 -0
- package/scaffold/templates/astro/src/layouts/BaseLayout.astro +3 -1
- package/scaffold/templates/astro/src/lib/api.ts +64 -22
- package/scaffold/templates/astro/src/lib/client.ts +5 -2
- package/scaffold/templates/astro/src/pages/404.astro +3 -0
- package/scaffold/templates/astro/src/pages/account/index.astro +1 -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/cart.astro +3 -0
- package/scaffold/templates/astro/src/pages/category/[slug].astro +8 -6
- package/scaffold/templates/astro/src/pages/checkout.astro +10 -3
- package/scaffold/templates/astro/src/pages/index.astro +7 -0
- package/scaffold/templates/astro/src/pages/order/[orderNumber].astro +158 -54
- package/scaffold/templates/astro/src/pages/product/[slug].astro +20 -4
- package/scaffold/templates/astro/src/scripts/account-guard.ts +1 -0
- package/scaffold/templates/astro/src/scripts/order-access.ts +154 -0
- package/scaffold/templates/astro/src/scripts/order-status.ts +3 -1
- package/scaffold/templates/astro/src/scripts/pdp.ts +6 -0
- package/scaffold/templates/astro/src/scripts/plp.ts +1 -0
- package/scaffold/templates/astro/src/scripts/search.ts +1 -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 +20 -9
- package/scaffold/templates/nextjs/app/(shopper)/not-found.tsx +6 -0
- package/scaffold/templates/nextjs/app/(shopper)/order/[orderNumber]/OrderDetails.tsx +23 -0
- package/scaffold/templates/nextjs/app/(shopper)/order/[orderNumber]/OrderEmailPrompt.tsx +90 -0
- package/scaffold/templates/nextjs/app/(shopper)/order/[orderNumber]/OrderLinkForm.tsx +73 -0
- package/scaffold/templates/nextjs/app/(shopper)/order/[orderNumber]/OrderNotFound.tsx +25 -0
- package/scaffold/templates/nextjs/app/(shopper)/order/[orderNumber]/OrderView.tsx +140 -0
- package/scaffold/templates/nextjs/app/(shopper)/order/[orderNumber]/not-found.tsx +6 -0
- package/scaffold/templates/nextjs/app/(shopper)/order/[orderNumber]/page.tsx +17 -129
- 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 +62 -14
- package/scaffold/templates/nextjs/next.config.mjs +9 -0
- package/scaffold/templates/nextjs/package.json +26 -26
|
@@ -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,26 @@ 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. A signed-in shopper's checkout answers no key: their account is
|
|
22
|
+
// the order's home.
|
|
23
|
+
const onPlaced = useCallback((event: CustomEvent<{ orderNumber: string; orderKey?: string }>) => {
|
|
24
|
+
const { orderNumber, orderKey } = event.detail
|
|
25
|
+
const number = encodeURIComponent(orderNumber)
|
|
26
|
+
window.location.assign(
|
|
27
|
+
orderKey
|
|
28
|
+
? `/order/${number}?key=${encodeURIComponent(orderKey)}`
|
|
29
|
+
: `/account/orders/${number}`,
|
|
30
|
+
)
|
|
31
|
+
}, [])
|
|
21
32
|
|
|
22
33
|
useEffect(() => {
|
|
23
34
|
NymbalPaymentElement.define()
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ShopperOrder } from '@nymbalhq/types'
|
|
2
|
+
import { OrderStatusRefresh } from '@/components/client/OrderStatusRefresh'
|
|
3
|
+
import { renderNymbal } from '@/lib/render'
|
|
4
|
+
import { OrderView, summaryAttributes } from './OrderView'
|
|
5
|
+
|
|
6
|
+
/** The order its key opens, rendered on the server with the elements' server HTML. */
|
|
7
|
+
export function OrderDetails({ order, orderKey }: { order: ShopperOrder; orderKey?: string }) {
|
|
8
|
+
const timeline = JSON.stringify(order.timeline ?? [])
|
|
9
|
+
return (
|
|
10
|
+
<OrderView
|
|
11
|
+
order={order}
|
|
12
|
+
timelineHtml={renderNymbal('nymbal-order-timeline', { entries: timeline }).__html}
|
|
13
|
+
summaryHtml={renderNymbal('nymbal-order-summary', summaryAttributes(order)).__html}
|
|
14
|
+
refresh={
|
|
15
|
+
<OrderStatusRefresh
|
|
16
|
+
orderNumber={order.orderNumber}
|
|
17
|
+
orderKey={orderKey}
|
|
18
|
+
status={order.status}
|
|
19
|
+
/>
|
|
20
|
+
}
|
|
21
|
+
/>
|
|
22
|
+
)
|
|
23
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useRef, useState, type FormEvent } from 'react'
|
|
4
|
+
import { NymbalApiError } from '@nymbalhq/sdk'
|
|
5
|
+
import type { ShopperOrder } from '@nymbalhq/types'
|
|
6
|
+
import { getNymbalClient } from '@/lib/client'
|
|
7
|
+
import styles from '@/styles/pages/error.module.css'
|
|
8
|
+
import { OrderNotFound } from './OrderNotFound'
|
|
9
|
+
import { OrderView } from './OrderView'
|
|
10
|
+
|
|
11
|
+
type State =
|
|
12
|
+
| { kind: 'idle' | 'sending' | 'limited' | 'failed' | 'not-found' }
|
|
13
|
+
| { kind: 'opened'; order: ShopperOrder }
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The order's key has expired: the shopper gives the order's email to open it. The browser reads the order with the
|
|
17
|
+
* email in a header, so it never lands in a page URL and each shopper's tries count against their own address, not
|
|
18
|
+
* the storefront server's. One request per click: a second submit while one is out is ignored.
|
|
19
|
+
*/
|
|
20
|
+
export function OrderEmailPrompt({
|
|
21
|
+
orderNumber,
|
|
22
|
+
orderKey,
|
|
23
|
+
}: {
|
|
24
|
+
orderNumber: string
|
|
25
|
+
orderKey: string
|
|
26
|
+
}) {
|
|
27
|
+
const [state, setState] = useState<State>({ kind: 'idle' })
|
|
28
|
+
const inFlight = useRef(false)
|
|
29
|
+
|
|
30
|
+
async function onSubmit(event: FormEvent<HTMLFormElement>) {
|
|
31
|
+
event.preventDefault()
|
|
32
|
+
if (inFlight.current) return
|
|
33
|
+
inFlight.current = true
|
|
34
|
+
setState({ kind: 'sending' })
|
|
35
|
+
const email = String(new FormData(event.currentTarget).get('email') ?? '').trim()
|
|
36
|
+
try {
|
|
37
|
+
const order = await getNymbalClient().orders.get(orderNumber, { key: orderKey, email })
|
|
38
|
+
setState({ kind: 'opened', order })
|
|
39
|
+
} catch (err) {
|
|
40
|
+
// A 404 is the answer an unknown number gets: the email did not open the order.
|
|
41
|
+
if (err instanceof NymbalApiError && err.status === 404) setState({ kind: 'not-found' })
|
|
42
|
+
else if (err instanceof NymbalApiError && err.status === 429) setState({ kind: 'limited' })
|
|
43
|
+
else setState({ kind: 'failed' })
|
|
44
|
+
} finally {
|
|
45
|
+
inFlight.current = false
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (state.kind === 'opened') return <OrderView order={state.order} />
|
|
50
|
+
if (state.kind === 'not-found') return <OrderNotFound />
|
|
51
|
+
return (
|
|
52
|
+
<div className={styles.page}>
|
|
53
|
+
<form method="post" onSubmit={onSubmit} className={styles.wrapper}>
|
|
54
|
+
<h1 className={styles.title}>Open your order</h1>
|
|
55
|
+
<p className={styles.message}>
|
|
56
|
+
This order link has expired. Enter the email you gave at checkout to open the order.
|
|
57
|
+
</p>
|
|
58
|
+
<div className={styles.searchPromptInput}>
|
|
59
|
+
<label htmlFor="order-email">Email</label>
|
|
60
|
+
<input
|
|
61
|
+
id="order-email"
|
|
62
|
+
type="email"
|
|
63
|
+
name="email"
|
|
64
|
+
autoComplete="email"
|
|
65
|
+
required
|
|
66
|
+
data-testid="order-email-input"
|
|
67
|
+
/>
|
|
68
|
+
</div>
|
|
69
|
+
{state.kind === 'limited' && (
|
|
70
|
+
<p className={styles.message} data-testid="order-email-limited">
|
|
71
|
+
Too many tries for now. Please try again in a while.
|
|
72
|
+
</p>
|
|
73
|
+
)}
|
|
74
|
+
{state.kind === 'failed' && (
|
|
75
|
+
<p className={styles.message}>We could not open the order just now. Please try again.</p>
|
|
76
|
+
)}
|
|
77
|
+
<div className={styles.actions}>
|
|
78
|
+
<button
|
|
79
|
+
type="submit"
|
|
80
|
+
className={styles.homeLink}
|
|
81
|
+
disabled={state.kind === 'sending'}
|
|
82
|
+
data-testid="order-email-submit"
|
|
83
|
+
>
|
|
84
|
+
Open order
|
|
85
|
+
</button>
|
|
86
|
+
</div>
|
|
87
|
+
</form>
|
|
88
|
+
</div>
|
|
89
|
+
)
|
|
90
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useRef, useState, type FormEvent } from 'react'
|
|
4
|
+
import { getNymbalClient } from '@/lib/client'
|
|
5
|
+
import styles from '@/styles/pages/error.module.css'
|
|
6
|
+
|
|
7
|
+
type State = 'idle' | 'sending' | 'sent' | 'failed'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* "Send me my order link": the store emails the order's link to the order's own email, and answers the same whatever
|
|
11
|
+
* matched, so the page says the same either way. One request per click: a second submit while one is out is ignored.
|
|
12
|
+
*/
|
|
13
|
+
export function OrderLinkForm() {
|
|
14
|
+
const [state, setState] = useState<State>('idle')
|
|
15
|
+
const inFlight = useRef(false)
|
|
16
|
+
|
|
17
|
+
async function onSubmit(event: FormEvent<HTMLFormElement>) {
|
|
18
|
+
event.preventDefault()
|
|
19
|
+
if (inFlight.current) return
|
|
20
|
+
inFlight.current = true
|
|
21
|
+
setState('sending')
|
|
22
|
+
const data = new FormData(event.currentTarget)
|
|
23
|
+
try {
|
|
24
|
+
await getNymbalClient().orders.requestLink({
|
|
25
|
+
orderNumber: String(data.get('orderNumber') ?? '').trim(),
|
|
26
|
+
email: String(data.get('email') ?? '').trim(),
|
|
27
|
+
})
|
|
28
|
+
setState('sent')
|
|
29
|
+
} catch {
|
|
30
|
+
setState('failed')
|
|
31
|
+
} finally {
|
|
32
|
+
inFlight.current = false
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (state === 'sent') {
|
|
37
|
+
return (
|
|
38
|
+
<p className={styles.message} data-testid="order-link-sent">
|
|
39
|
+
If that number and email match an order, its link is on its way to that email.
|
|
40
|
+
</p>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
return (
|
|
44
|
+
<form method="post" className={styles.searchPrompt} onSubmit={onSubmit}>
|
|
45
|
+
<div className={styles.searchPromptInput}>
|
|
46
|
+
<label htmlFor="order-link-number">Order number</label>
|
|
47
|
+
<input id="order-link-number" name="orderNumber" required data-testid="order-link-number" />
|
|
48
|
+
<label htmlFor="order-link-email">Email</label>
|
|
49
|
+
<input
|
|
50
|
+
id="order-link-email"
|
|
51
|
+
type="email"
|
|
52
|
+
name="email"
|
|
53
|
+
autoComplete="email"
|
|
54
|
+
required
|
|
55
|
+
data-testid="order-link-email"
|
|
56
|
+
/>
|
|
57
|
+
</div>
|
|
58
|
+
{state === 'failed' && (
|
|
59
|
+
<p className={styles.message}>We could not send the link just now. Please try again.</p>
|
|
60
|
+
)}
|
|
61
|
+
<div className={styles.actions}>
|
|
62
|
+
<button
|
|
63
|
+
type="submit"
|
|
64
|
+
className={styles.homeLink}
|
|
65
|
+
disabled={state === 'sending'}
|
|
66
|
+
data-testid="order-link-submit"
|
|
67
|
+
>
|
|
68
|
+
Send me my order link
|
|
69
|
+
</button>
|
|
70
|
+
</div>
|
|
71
|
+
</form>
|
|
72
|
+
)
|
|
73
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import Link from 'next/link'
|
|
2
|
+
import styles from '@/styles/pages/error.module.css'
|
|
3
|
+
import { OrderLinkForm } from './OrderLinkForm'
|
|
4
|
+
|
|
5
|
+
/** The order page's not-found state: what the site's 404 says, and a way to ask for a fresh order link. */
|
|
6
|
+
export function OrderNotFound() {
|
|
7
|
+
return (
|
|
8
|
+
<div className={styles.page}>
|
|
9
|
+
<div className={styles.wrapper}>
|
|
10
|
+
<p className={styles.statusCode}>404</p>
|
|
11
|
+
<h1 className={styles.title}>Order not found</h1>
|
|
12
|
+
<p className={styles.message}>
|
|
13
|
+
This link does not open an order. Give its order number and the email you gave at
|
|
14
|
+
checkout, and we'll email you a fresh link if they match.
|
|
15
|
+
</p>
|
|
16
|
+
<OrderLinkForm />
|
|
17
|
+
<div className={styles.actions}>
|
|
18
|
+
<Link href="/products" className={styles.retryButton}>
|
|
19
|
+
Browse Products
|
|
20
|
+
</Link>
|
|
21
|
+
</div>
|
|
22
|
+
</div>
|
|
23
|
+
</div>
|
|
24
|
+
)
|
|
25
|
+
}
|