@canonical/summon-application 0.33.0 → 0.35.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 +9 -9
- package/dist/esm/application/react/index.js +403 -277
- package/dist/esm/application/react/index.js.map +1 -1
- package/dist/esm/application/react/templates/.storybook/decorators/index.ts.ejs +4 -0
- package/dist/esm/application/react/templates/.storybook/decorators/withI18n.tsx +35 -0
- package/dist/esm/application/react/templates/.storybook/decorators/withRouter.tsx +18 -20
- package/dist/esm/application/react/templates/.storybook/preview.ts.ejs +35 -0
- package/dist/esm/application/react/templates/index.html.ejs +2 -1
- package/dist/esm/application/react/templates/package.json.ejs +34 -3
- package/dist/esm/application/react/templates/src/client/entry.tsx.ejs +109 -10
- package/dist/esm/application/react/templates/src/domains/account/{AccountPage.tsx → AccountPage.tsx.ejs} +13 -0
- package/dist/esm/application/react/templates/src/domains/account/LoginPage.tsx.ejs +51 -0
- package/dist/esm/application/react/templates/src/domains/account/routes.ts +21 -8
- package/dist/esm/application/react/templates/src/domains/catalog/CatalogPage.tsx.ejs +97 -0
- package/dist/esm/application/react/templates/src/domains/catalog/{ErrorBoundary.tests.tsx → ErrorBoundary.tests.tsx.ejs} +24 -0
- package/dist/esm/application/react/templates/src/domains/catalog/{ProductCard.tsx → ProductCard.tsx.ejs} +31 -0
- package/dist/esm/application/react/templates/src/domains/catalog/{ProductList.stories.tsx → ProductList.stories.tsx.ejs} +63 -4
- package/dist/esm/application/react/templates/src/domains/catalog/{ProductList.tests.tsx → ProductList.tests.tsx.ejs} +44 -0
- package/dist/esm/application/react/templates/src/domains/catalog/{ProductList.tsx → ProductList.tsx.ejs} +30 -2
- package/dist/esm/application/react/templates/src/domains/catalog/routes.ts +25 -0
- package/dist/esm/application/react/templates/src/domains/contact/{ContactPage.tsx → ContactPage.tsx.ejs} +40 -0
- package/dist/esm/application/react/templates/src/domains/marketing/GuidePage.tsx.ejs +33 -0
- package/dist/esm/application/react/templates/src/domains/marketing/{HomePage.tsx → HomePage.tsx.ejs} +30 -0
- package/dist/esm/application/react/templates/src/domains/marketing/routes.ts +25 -0
- package/dist/esm/application/react/templates/src/i18n/ar.ts +74 -0
- package/dist/esm/application/react/templates/src/i18n/catalogs.ts +14 -0
- package/dist/esm/application/react/templates/src/i18n/config.ts +19 -0
- package/dist/esm/application/react/templates/src/i18n/en.ts +73 -0
- package/dist/esm/application/react/templates/src/i18n/fr.ts +70 -0
- package/dist/esm/application/react/templates/src/i18n/index.ts +5 -0
- package/dist/esm/application/react/templates/src/i18n/negotiation.tests.ts +92 -0
- package/dist/esm/application/react/templates/src/lib/LocaleSelector/LocaleSelector.tests.tsx +60 -0
- package/dist/esm/application/react/templates/src/lib/LocaleSelector/LocaleSelector.tsx +44 -0
- package/dist/esm/application/react/templates/src/lib/LocaleSelector/index.ts +1 -0
- package/dist/esm/application/react/templates/src/lib/Navigation/Navigation.tsx.ejs +34 -0
- package/dist/esm/application/react/templates/src/lib/ThemeSelector/{ThemeSelector.tsx → ThemeSelector.tsx.ejs} +16 -0
- package/dist/esm/application/react/templates/src/lib/index.ts.ejs +3 -0
- package/dist/esm/application/react/templates/src/relay/environment.ts +153 -5
- package/dist/esm/application/react/templates/src/routes.tsx.ejs +137 -33
- package/dist/esm/application/react/templates/src/server/entry.tsx.ejs +293 -11
- package/dist/esm/application/react/templates/src/server/preview.bun.ts +30 -7
- package/dist/esm/application/react/templates/src/server/preview.express.ts +19 -4
- package/dist/esm/application/react/templates/src/server/renderer.tsx.ejs +125 -0
- package/dist/esm/application/react/templates/src/server/{server.bun.ts → server.bun.ts.ejs} +56 -4
- package/dist/esm/application/react/templates/src/server/{server.express.ts → server.express.ts.ejs} +48 -4
- package/dist/esm/application/react/templates/test/e2e/servers.e2e.ts.ejs +197 -0
- package/dist/esm/application/react/templates/tsconfig.json.ejs +3 -0
- package/dist/esm/application/react/templates/vite.config.ts.ejs +5 -0
- package/dist/esm/domain/index.js +8 -1
- package/dist/esm/domain/index.js.map +1 -1
- package/dist/esm/route/index.js +12 -2
- package/dist/esm/route/index.js.map +1 -1
- package/dist/esm/route/insertRoute.js +39 -11
- package/dist/esm/route/insertRoute.js.map +1 -1
- package/dist/esm/shared/packageName.js +7 -0
- package/dist/esm/shared/packageName.js.map +1 -0
- package/dist/esm/shared/packageVersion.js +50 -0
- package/dist/esm/shared/packageVersion.js.map +1 -0
- package/dist/esm/shared/validators.js +61 -0
- package/dist/esm/shared/validators.js.map +1 -0
- package/dist/esm/shared/versions.js +87 -55
- package/dist/esm/shared/versions.js.map +1 -1
- package/dist/esm/shared/workspace.js +57 -0
- package/dist/esm/shared/workspace.js.map +1 -1
- package/dist/esm/wrapper/index.js +8 -1
- package/dist/esm/wrapper/index.js.map +1 -1
- package/dist/types/application/react/index.d.ts +1 -2
- package/dist/types/application/react/index.d.ts.map +1 -1
- package/dist/types/domain/index.d.ts.map +1 -1
- package/dist/types/route/index.d.ts.map +1 -1
- package/dist/types/shared/packageName.d.ts +7 -0
- package/dist/types/shared/packageName.d.ts.map +1 -0
- package/dist/types/shared/packageVersion.d.ts +15 -0
- package/dist/types/shared/packageVersion.d.ts.map +1 -0
- package/dist/types/shared/validators.d.ts +21 -0
- package/dist/types/shared/validators.d.ts.map +1 -0
- package/dist/types/shared/versions.d.ts +14 -5
- package/dist/types/shared/versions.d.ts.map +1 -1
- package/dist/types/shared/workspace.d.ts.map +1 -1
- package/dist/types/wrapper/index.d.ts.map +1 -1
- package/package.json +8 -8
- package/dist/esm/application/react/templates/.storybook/decorators/index.ts +0 -1
- package/dist/esm/application/react/templates/.storybook/preview.ts +0 -10
- package/dist/esm/application/react/templates/src/domains/account/LoginPage.tsx +0 -27
- package/dist/esm/application/react/templates/src/domains/catalog/CatalogPage.tsx +0 -50
- package/dist/esm/application/react/templates/src/domains/marketing/GuidePage.tsx +0 -17
- package/dist/esm/application/react/templates/src/server/renderer.tsx +0 -50
- package/dist/esm/application/react/templates/test/e2e/servers.e2e.ts +0 -99
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
<% if (intl) { -%>
|
|
2
|
+
import { useTranslation } from "@canonical/i18n-react";
|
|
3
|
+
<% } -%>
|
|
4
|
+
import { useHead } from "@canonical/react-head";
|
|
5
|
+
import { type ReactElement, Suspense } from "react";
|
|
6
|
+
import { useRelayEnvironment } from "react-relay";
|
|
7
|
+
import { createOperationDescriptor, getRequest } from "relay-runtime";
|
|
8
|
+
import ErrorBoundary from "./ErrorBoundary.js";
|
|
9
|
+
import ProductList, { PAGE_SIZE, productListQuery } from "./ProductList.js";
|
|
10
|
+
|
|
11
|
+
export default function CatalogPage(): ReactElement {
|
|
12
|
+
<% if (intl) { -%>
|
|
13
|
+
const { t } = useTranslation();
|
|
14
|
+
useHead({ title: t("catalog.title") });
|
|
15
|
+
<% } else { -%>
|
|
16
|
+
useHead({ title: "Catalog — Boilerplate" });
|
|
17
|
+
<% } -%>
|
|
18
|
+
|
|
19
|
+
const environment = useRelayEnvironment();
|
|
20
|
+
const canRenderQuery =
|
|
21
|
+
typeof document !== "undefined" ||
|
|
22
|
+
environment.check(
|
|
23
|
+
createOperationDescriptor(getRequest(productListQuery), {
|
|
24
|
+
count: PAGE_SIZE,
|
|
25
|
+
}),
|
|
26
|
+
).status === "available";
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<section aria-labelledby="catalog-title">
|
|
30
|
+
<% if (intl) { -%>
|
|
31
|
+
<h1 id="catalog-title">{t("catalog.heading")}</h1>
|
|
32
|
+
{/*
|
|
33
|
+
Developer documentation rather than user-facing copy (file names,
|
|
34
|
+
APIs), so it deliberately stays out of the message catalogs.
|
|
35
|
+
*/}
|
|
36
|
+
<% } else { -%>
|
|
37
|
+
<h1 id="catalog-title">Catalog</h1>
|
|
38
|
+
<% } -%>
|
|
39
|
+
<p>
|
|
40
|
+
This page demonstrates the Relay data layer. <code>ProductList</code>{" "}
|
|
41
|
+
issues a <code>useLazyLoadQuery</code>, each <code>ProductCard</code>{" "}
|
|
42
|
+
reads its own <code>useFragment</code>, and the environment resolves
|
|
43
|
+
operations against the local mock schema in{" "}
|
|
44
|
+
<code>src/relay/schema.ts</code> — set <code>VITE_GRAPHQL_URL</code> to
|
|
45
|
+
point it at a real endpoint instead.
|
|
46
|
+
</p>
|
|
47
|
+
{/*
|
|
48
|
+
SSR: the server prefetches this route's declared query (see
|
|
49
|
+
`serverQueries` in src/routes.tsx) and seeds both render environments
|
|
50
|
+
from the captured payloads, so `useLazyLoadQuery` (store-or-network)
|
|
51
|
+
renders synchronously on the server and on first client render — the
|
|
52
|
+
product list is real server-rendered HTML and the client issues no
|
|
53
|
+
second fetch. If the prefetch failed, the query suspends and fetches
|
|
54
|
+
through the active environment instead.
|
|
55
|
+
*/}
|
|
56
|
+
{/*
|
|
57
|
+
The canonical Relay pairing: Suspense renders the pending state while
|
|
58
|
+
`useLazyLoadQuery` is in flight, and the ErrorBoundary renders the
|
|
59
|
+
failure state when the query errors (e.g. an unreachable endpoint in
|
|
60
|
+
`VITE_GRAPHQL_URL` mode) — without it a thrown query error would
|
|
61
|
+
unmount the whole tree to a blank page.
|
|
62
|
+
*/}
|
|
63
|
+
{/*
|
|
64
|
+
On the server, the query renders only when the SSR prefetch already
|
|
65
|
+
seeded the store — after a failed or timed-out prefetch, querying here
|
|
66
|
+
would re-issue the same doomed request mid-stream. The loading
|
|
67
|
+
fallback is rendered instead and the client fetches after hydration;
|
|
68
|
+
markup matches, since a client without payloads paints the same
|
|
69
|
+
fallback via Suspense first.
|
|
70
|
+
*/}
|
|
71
|
+
{canRenderQuery ? (
|
|
72
|
+
<% if (intl) { -%>
|
|
73
|
+
<ErrorBoundary fallback={<p role="alert">{t("catalog.error")}</p>}>
|
|
74
|
+
<Suspense fallback={<p>{t("catalog.loading")}</p>}>
|
|
75
|
+
<% } else { -%>
|
|
76
|
+
<ErrorBoundary
|
|
77
|
+
fallback={
|
|
78
|
+
<p role="alert">
|
|
79
|
+
The catalog failed to load. Reload the page to try again.
|
|
80
|
+
</p>
|
|
81
|
+
}
|
|
82
|
+
>
|
|
83
|
+
<Suspense fallback={<p>Loading catalog…</p>}>
|
|
84
|
+
<% } -%>
|
|
85
|
+
<ProductList />
|
|
86
|
+
</Suspense>
|
|
87
|
+
</ErrorBoundary>
|
|
88
|
+
) : (
|
|
89
|
+
<% if (intl) { -%>
|
|
90
|
+
<p>{t("catalog.loading")}</p>
|
|
91
|
+
<% } else { -%>
|
|
92
|
+
<p>Loading catalog…</p>
|
|
93
|
+
<% } -%>
|
|
94
|
+
)}
|
|
95
|
+
</section>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
+
<% if (intl) { -%>
|
|
2
|
+
import { I18nProvider } from "@canonical/i18n-react";
|
|
3
|
+
<% } -%>
|
|
1
4
|
import { render, screen } from "@testing-library/react";
|
|
2
5
|
import { Suspense } from "react";
|
|
3
6
|
import { RelayEnvironmentProvider } from "react-relay";
|
|
4
7
|
import { createMockEnvironment } from "relay-test-utils";
|
|
5
8
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
9
|
+
<% if (intl) { -%>
|
|
10
|
+
import { catalogs, i18nConfig } from "#i18n/index.js";
|
|
11
|
+
<% } -%>
|
|
6
12
|
import ErrorBoundary from "./ErrorBoundary.js";
|
|
7
13
|
import ProductList from "./ProductList.js";
|
|
8
14
|
|
|
@@ -35,7 +41,24 @@ describe("ErrorBoundary component", () => {
|
|
|
35
41
|
() => new Error("backend unreachable"),
|
|
36
42
|
);
|
|
37
43
|
|
|
44
|
+
<% if (intl) { -%>
|
|
45
|
+
// ProductList translates its chrome, so it renders inside the provider —
|
|
46
|
+
// the same shape CatalogPage produces.
|
|
47
|
+
<% } -%>
|
|
38
48
|
render(
|
|
49
|
+
<% if (intl) { -%>
|
|
50
|
+
<I18nProvider config={i18nConfig} catalogs={catalogs}>
|
|
51
|
+
<RelayEnvironmentProvider environment={environment}>
|
|
52
|
+
<ErrorBoundary
|
|
53
|
+
fallback={<p role="alert">The catalog failed to load.</p>}
|
|
54
|
+
>
|
|
55
|
+
<Suspense fallback={<p>Loading catalog…</p>}>
|
|
56
|
+
<ProductList />
|
|
57
|
+
</Suspense>
|
|
58
|
+
</ErrorBoundary>
|
|
59
|
+
</RelayEnvironmentProvider>
|
|
60
|
+
</I18nProvider>,
|
|
61
|
+
<% } else { -%>
|
|
39
62
|
<RelayEnvironmentProvider environment={environment}>
|
|
40
63
|
<ErrorBoundary
|
|
41
64
|
fallback={<p role="alert">The catalog failed to load.</p>}
|
|
@@ -45,6 +68,7 @@ describe("ErrorBoundary component", () => {
|
|
|
45
68
|
</Suspense>
|
|
46
69
|
</ErrorBoundary>
|
|
47
70
|
</RelayEnvironmentProvider>,
|
|
71
|
+
<% } -%>
|
|
48
72
|
);
|
|
49
73
|
|
|
50
74
|
// The fallback replaces the subtree instead of white-screening.
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
<% if (intl) { -%>
|
|
2
|
+
import { useFormatters, useTranslation } from "@canonical/i18n-react";
|
|
3
|
+
<% } -%>
|
|
1
4
|
import type { ReactElement } from "react";
|
|
2
5
|
import { graphql, useFragment } from "react-relay";
|
|
3
6
|
import type { ProductCard_product$key } from "#relay/__generated__/ProductCard_product.graphql.js";
|
|
@@ -16,12 +19,14 @@ const productCardFragment = graphql`
|
|
|
16
19
|
}
|
|
17
20
|
`;
|
|
18
21
|
|
|
22
|
+
<% if (!intl) { -%>
|
|
19
23
|
/** Formats a minor-unit price deterministically (fixed locale, real currency). */
|
|
20
24
|
const formatPrice = (priceCents: number, currency: string): string =>
|
|
21
25
|
new Intl.NumberFormat("en-US", { style: "currency", currency }).format(
|
|
22
26
|
priceCents / 100,
|
|
23
27
|
);
|
|
24
28
|
|
|
29
|
+
<% } -%>
|
|
25
30
|
export interface ProductCardProps {
|
|
26
31
|
/** Fragment reference to the product this card renders. */
|
|
27
32
|
readonly product: ProductCard_product$key;
|
|
@@ -29,20 +34,46 @@ export interface ProductCardProps {
|
|
|
29
34
|
|
|
30
35
|
/**
|
|
31
36
|
* Renders one catalog product from a Relay fragment reference.
|
|
37
|
+
<% if (intl) { -%>
|
|
38
|
+
*
|
|
39
|
+
* Price and rating go through `useFormatters` (memoized `Intl` instances
|
|
40
|
+
* bound to the active locale), so "$125.00" becomes "125,00 $US" in French
|
|
41
|
+
* without touching the data: the currency code stays whatever the product
|
|
42
|
+
* says, only its presentation is localized. The name and tagline are data,
|
|
43
|
+
* not chrome, and render untranslated.
|
|
44
|
+
<% } -%>
|
|
32
45
|
*/
|
|
33
46
|
export default function ProductCard({
|
|
34
47
|
product,
|
|
35
48
|
}: ProductCardProps): ReactElement {
|
|
36
49
|
const data = useFragment(productCardFragment, product);
|
|
50
|
+
<% if (intl) { -%>
|
|
51
|
+
const { t } = useTranslation();
|
|
52
|
+
const formatters = useFormatters();
|
|
53
|
+
<% } -%>
|
|
37
54
|
|
|
38
55
|
return (
|
|
39
56
|
<article aria-label={data.name}>
|
|
40
57
|
<h3>{data.name}</h3>
|
|
41
58
|
<p>{data.tagline}</p>
|
|
42
59
|
<p>
|
|
60
|
+
<% if (intl) { -%>
|
|
61
|
+
<strong>
|
|
62
|
+
{formatters.currency(data.priceCents / 100, data.currency)}
|
|
63
|
+
</strong>{" "}
|
|
64
|
+
·{" "}
|
|
65
|
+
{t("catalog.rating", {
|
|
66
|
+
rating: formatters.number(data.rating, {
|
|
67
|
+
minimumFractionDigits: 1,
|
|
68
|
+
maximumFractionDigits: 1,
|
|
69
|
+
}),
|
|
70
|
+
})}{" "}
|
|
71
|
+
· {data.inStock ? t("catalog.inStock") : t("catalog.outOfStock")}
|
|
72
|
+
<% } else { -%>
|
|
43
73
|
<strong>{formatPrice(data.priceCents, data.currency)}</strong> · rated{" "}
|
|
44
74
|
{data.rating.toFixed(1)} / 5 ·{" "}
|
|
45
75
|
{data.inStock ? "in stock" : "out of stock"}
|
|
76
|
+
<% } -%>
|
|
46
77
|
</p>
|
|
47
78
|
</article>
|
|
48
79
|
);
|
|
@@ -2,7 +2,25 @@ import type { RelayParameters } from "@canonical/storybook-addon-relay";
|
|
|
2
2
|
import type { Meta, StoryObj } from "@storybook/react-vite";
|
|
3
3
|
import { Suspense } from "react";
|
|
4
4
|
import { expect } from "storybook/test";
|
|
5
|
+
<% if (intl) { -%>
|
|
6
|
+
import { catalogs } from "#i18n/index.js";
|
|
7
|
+
<% } -%>
|
|
5
8
|
import Component from "./ProductList.js";
|
|
9
|
+
<% if (intl) { -%>
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The locale toolbar rerenders every story in the selected locale, so play
|
|
13
|
+
* assertions must never hard-code English copy. Data values (product and
|
|
14
|
+
* viewer names) are locale-independent; translated chrome is asserted
|
|
15
|
+
* against the active locale's own catalog, and counts structurally.
|
|
16
|
+
*/
|
|
17
|
+
const messagesFor = (locale: unknown): Record<string, unknown> =>
|
|
18
|
+
catalogs[
|
|
19
|
+
(typeof locale === "string" && locale in catalogs
|
|
20
|
+
? locale
|
|
21
|
+
: "en") as keyof typeof catalogs
|
|
22
|
+
];
|
|
23
|
+
<% } -%>
|
|
6
24
|
|
|
7
25
|
const meta = {
|
|
8
26
|
title: "Catalog/ProductList",
|
|
@@ -65,18 +83,40 @@ export const Default: Story = {
|
|
|
65
83
|
},
|
|
66
84
|
} satisfies RelayParameters,
|
|
67
85
|
},
|
|
86
|
+
<% if (intl) { -%>
|
|
87
|
+
play: async ({ canvas, globals }) => {
|
|
88
|
+
const messages = messagesFor(globals.locale);
|
|
89
|
+
|
|
90
|
+
<% } else { -%>
|
|
68
91
|
play: async ({ canvas }) => {
|
|
92
|
+
<% } -%>
|
|
69
93
|
await expect(
|
|
70
94
|
await canvas.findByText("Aurora Dev Board"),
|
|
71
95
|
).toBeInTheDocument();
|
|
72
96
|
await expect(canvas.getByText("Ada Lovelace")).toBeInTheDocument();
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
97
|
+
<% if (intl) { -%>
|
|
98
|
+
// Both resolved products render as cards; the summary line is plural-
|
|
99
|
+
// formatted per locale, so the count is asserted structurally.
|
|
100
|
+
<% } else { -%>
|
|
101
|
+
// Both resolved products render as cards; the count is asserted
|
|
102
|
+
// structurally so it cannot drift from the summary copy.
|
|
103
|
+
<% } -%>
|
|
104
|
+
await expect(canvas.getAllByRole("article")).toHaveLength(2);
|
|
76
105
|
// The Polar Sensor Kit is the story's out-of-stock product.
|
|
106
|
+
<% if (!intl) { -%>
|
|
77
107
|
await expect(canvas.getByText(/out of stock/)).toBeInTheDocument();
|
|
108
|
+
<% } -%>
|
|
109
|
+
await expect(
|
|
110
|
+
<% if (intl) { -%>
|
|
111
|
+
canvas.getByText(String(messages["catalog.outOfStock"]), {
|
|
112
|
+
exact: false,
|
|
113
|
+
}),
|
|
114
|
+
).toBeInTheDocument();
|
|
78
115
|
await expect(
|
|
116
|
+
canvas.getByText(String(messages["catalog.more"])),
|
|
117
|
+
<% } else { -%>
|
|
79
118
|
canvas.getByText("More products are available."),
|
|
119
|
+
<% } -%>
|
|
80
120
|
).toBeInTheDocument();
|
|
81
121
|
},
|
|
82
122
|
};
|
|
@@ -94,10 +134,29 @@ export const EmptyCatalog: Story = {
|
|
|
94
134
|
},
|
|
95
135
|
} satisfies RelayParameters,
|
|
96
136
|
},
|
|
137
|
+
<% if (intl) { -%>
|
|
138
|
+
play: async ({ canvas, globals }) => {
|
|
139
|
+
const messages = messagesFor(globals.locale);
|
|
140
|
+
|
|
141
|
+
// The list section rendered (its accessible label is locale-scoped)…
|
|
142
|
+
<% } else { -%>
|
|
97
143
|
play: async ({ canvas }) => {
|
|
144
|
+
// The list section rendered…
|
|
145
|
+
<% } -%>
|
|
98
146
|
await expect(
|
|
99
|
-
|
|
147
|
+
<% if (intl) { -%>
|
|
148
|
+
await canvas.findByLabelText(String(messages["catalog.listLabel"])),
|
|
149
|
+
<% } else { -%>
|
|
150
|
+
await canvas.findByLabelText("Product catalog"),
|
|
151
|
+
<% } -%>
|
|
100
152
|
).toBeInTheDocument();
|
|
153
|
+
<% if (intl) { -%>
|
|
154
|
+
// …with no product cards; the summary line is plural-formatted per
|
|
155
|
+
// locale, so emptiness is asserted structurally.
|
|
156
|
+
<% } else { -%>
|
|
157
|
+
// …with no product cards; emptiness is asserted structurally so it
|
|
158
|
+
// cannot drift from the summary copy.
|
|
159
|
+
<% } -%>
|
|
101
160
|
await expect(canvas.queryByRole("article")).not.toBeInTheDocument();
|
|
102
161
|
},
|
|
103
162
|
};
|
|
@@ -1,20 +1,41 @@
|
|
|
1
|
+
<% if (intl) { -%>
|
|
2
|
+
import type { Locale } from "@canonical/i18n-core";
|
|
3
|
+
import { I18nProvider } from "@canonical/i18n-react";
|
|
4
|
+
<% } -%>
|
|
1
5
|
import { render, screen } from "@testing-library/react";
|
|
2
6
|
import { Suspense } from "react";
|
|
3
7
|
import { RelayEnvironmentProvider } from "react-relay";
|
|
4
8
|
import type { IEnvironment } from "relay-runtime";
|
|
5
9
|
import { createMockEnvironment, MockPayloadGenerator } from "relay-test-utils";
|
|
6
10
|
import { describe, expect, it } from "vitest";
|
|
11
|
+
<% if (intl) { -%>
|
|
12
|
+
import { catalogs, i18nConfig } from "#i18n/index.js";
|
|
13
|
+
<% } -%>
|
|
7
14
|
import { createEnvironment } from "#relay/environment.js";
|
|
8
15
|
import { CATALOG_PRODUCTS } from "#relay/schema.js";
|
|
9
16
|
import ProductList from "./ProductList.js";
|
|
10
17
|
|
|
18
|
+
<% if (intl) { -%>
|
|
19
|
+
const renderProductList = (environment: IEnvironment, locale: Locale = "en") =>
|
|
20
|
+
<% } else { -%>
|
|
11
21
|
const renderProductList = (environment: IEnvironment) =>
|
|
22
|
+
<% } -%>
|
|
12
23
|
render(
|
|
24
|
+
<% if (intl) { -%>
|
|
25
|
+
<I18nProvider config={i18nConfig} catalogs={catalogs} locale={locale}>
|
|
26
|
+
<RelayEnvironmentProvider environment={environment}>
|
|
27
|
+
<Suspense fallback={<p>Loading catalog…</p>}>
|
|
28
|
+
<ProductList />
|
|
29
|
+
</Suspense>
|
|
30
|
+
</RelayEnvironmentProvider>
|
|
31
|
+
</I18nProvider>,
|
|
32
|
+
<% } else { -%>
|
|
13
33
|
<RelayEnvironmentProvider environment={environment}>
|
|
14
34
|
<Suspense fallback={<p>Loading catalog…</p>}>
|
|
15
35
|
<ProductList />
|
|
16
36
|
</Suspense>
|
|
17
37
|
</RelayEnvironmentProvider>,
|
|
38
|
+
<% } -%>
|
|
18
39
|
);
|
|
19
40
|
|
|
20
41
|
describe("ProductList component", () => {
|
|
@@ -77,4 +98,27 @@ describe("ProductList component", () => {
|
|
|
77
98
|
screen.queryByText("More products are available."),
|
|
78
99
|
).not.toBeInTheDocument();
|
|
79
100
|
});
|
|
101
|
+
<% if (intl) { -%>
|
|
102
|
+
|
|
103
|
+
it("renders French copy and localized formatting for locale fr", async () => {
|
|
104
|
+
renderProductList(createEnvironment(), "fr");
|
|
105
|
+
|
|
106
|
+
const firstProduct = CATALOG_PRODUCTS[0];
|
|
107
|
+
expect(
|
|
108
|
+
await screen.findByRole("article", { name: firstProduct?.name }),
|
|
109
|
+
).toBeInTheDocument();
|
|
110
|
+
// Chrome is translated; product data (names, taglines) is not.
|
|
111
|
+
expect(
|
|
112
|
+
screen.getByText(
|
|
113
|
+
new RegExp(`affichage de 4 sur ${CATALOG_PRODUCTS.length} produits`),
|
|
114
|
+
),
|
|
115
|
+
).toBeInTheDocument();
|
|
116
|
+
expect(screen.getByText(/en rupture de stock/)).toBeInTheDocument();
|
|
117
|
+
// French decimal comma via useFormatters — same data, localized rendering.
|
|
118
|
+
expect(screen.getByText(/noté 4,2 \/ 5/)).toBeInTheDocument();
|
|
119
|
+
expect(
|
|
120
|
+
screen.getByText("D'autres produits sont disponibles."),
|
|
121
|
+
).toBeInTheDocument();
|
|
122
|
+
});
|
|
123
|
+
<% } -%>
|
|
80
124
|
});
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
+
<% if (intl) { -%>
|
|
2
|
+
import { useTranslation } from "@canonical/i18n-react";
|
|
3
|
+
<% } -%>
|
|
1
4
|
import type { ReactElement } from "react";
|
|
2
5
|
import { graphql, useLazyLoadQuery } from "react-relay";
|
|
3
6
|
import type { ProductListQuery } from "#relay/__generated__/ProductListQuery.graphql.js";
|
|
4
7
|
import ProductCard from "./ProductCard.js";
|
|
5
8
|
|
|
6
9
|
/** How many products the list requests from the connection. */
|
|
7
|
-
const PAGE_SIZE = 4;
|
|
10
|
+
export const PAGE_SIZE = 4;
|
|
8
11
|
|
|
9
|
-
const productListQuery = graphql`
|
|
12
|
+
export const productListQuery = graphql`
|
|
10
13
|
query ProductListQuery($count: Int!, $cursor: String) {
|
|
11
14
|
viewer {
|
|
12
15
|
name
|
|
@@ -35,16 +38,37 @@ const productListQuery = graphql`
|
|
|
35
38
|
* lands, only on the client (see `CatalogPage`).
|
|
36
39
|
*/
|
|
37
40
|
export default function ProductList(): ReactElement {
|
|
41
|
+
<% if (intl) { -%>
|
|
42
|
+
const { t } = useTranslation();
|
|
43
|
+
<% } -%>
|
|
38
44
|
const data = useLazyLoadQuery<ProductListQuery>(productListQuery, {
|
|
39
45
|
count: PAGE_SIZE,
|
|
40
46
|
});
|
|
41
47
|
const { products } = data.viewer;
|
|
42
48
|
|
|
43
49
|
return (
|
|
50
|
+
<% if (intl) { -%>
|
|
51
|
+
<section aria-label={t("catalog.listLabel")}>
|
|
52
|
+
<% } else { -%>
|
|
44
53
|
<section aria-label="Product catalog">
|
|
54
|
+
<% } -%>
|
|
45
55
|
<p>
|
|
56
|
+
<% if (intl) { -%>
|
|
57
|
+
{/*
|
|
58
|
+
`catalog.showing` is a plural entry: `count` (the total) selects the
|
|
59
|
+
CLDR category via Intl.PluralRules — English has one/other, Arabic
|
|
60
|
+
all six. The viewer name stays a separate element so it can carry
|
|
61
|
+
emphasis, which a plain message string cannot.
|
|
62
|
+
*/}
|
|
63
|
+
{t("catalog.signedInAs")} <strong>{data.viewer.name}</strong>{" "}
|
|
64
|
+
{t("catalog.showing", {
|
|
65
|
+
shown: products.edges.length,
|
|
66
|
+
count: products.totalCount,
|
|
67
|
+
})}
|
|
68
|
+
<% } else { -%>
|
|
46
69
|
Signed in as <strong>{data.viewer.name}</strong> — showing{" "}
|
|
47
70
|
{products.edges.length} of {products.totalCount} products.
|
|
71
|
+
<% } -%>
|
|
48
72
|
</p>
|
|
49
73
|
<ul>
|
|
50
74
|
{products.edges.map(({ node }) => (
|
|
@@ -53,7 +77,11 @@ export default function ProductList(): ReactElement {
|
|
|
53
77
|
</li>
|
|
54
78
|
))}
|
|
55
79
|
</ul>
|
|
80
|
+
<% if (intl) { -%>
|
|
81
|
+
{products.pageInfo.hasNextPage && <p>{t("catalog.more")}</p>}
|
|
82
|
+
<% } else { -%>
|
|
56
83
|
{products.pageInfo.hasNextPage && <p>More products are available.</p>}
|
|
84
|
+
<% } -%>
|
|
57
85
|
</section>
|
|
58
86
|
);
|
|
59
87
|
}
|
|
@@ -1,9 +1,34 @@
|
|
|
1
1
|
import { route } from "@canonical/router-core";
|
|
2
|
+
import { fetchQuery } from "react-relay";
|
|
3
|
+
import { getBrowserEnvironment } from "#relay/environment.js";
|
|
2
4
|
import CatalogPage from "./CatalogPage.js";
|
|
5
|
+
import { PAGE_SIZE, productListQuery } from "./ProductList.js";
|
|
3
6
|
|
|
4
7
|
const routes = {
|
|
5
8
|
catalog: route({
|
|
6
9
|
url: "/catalog",
|
|
10
|
+
// Navigation-time cache warm: fetch the first catalog page into the
|
|
11
|
+
// browser session's Relay store so `useLazyLoadQuery` (store-or-network)
|
|
12
|
+
// reads it without a second request. Fire-and-forget by design — the
|
|
13
|
+
// route never blocks on it and no data is passed to the component; a
|
|
14
|
+
// failed warm just falls back to the component's own fetch. `fetchQuery`
|
|
15
|
+
// rather than `loadQuery`: the latter returns a reference that must be
|
|
16
|
+
// disposed, which has no owner in a fire-and-forget hook (unretained data
|
|
17
|
+
// sits in the store's release buffer and may eventually be GC'd —
|
|
18
|
+
// harmless, it re-fetches).
|
|
19
|
+
warm: () => {
|
|
20
|
+
if (typeof document === "undefined") {
|
|
21
|
+
// SSR data comes from the renderer's awaited prefetch of the same
|
|
22
|
+
// query this route declares in `serverQueries` — nothing to warm.
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
void fetchQuery(getBrowserEnvironment(), productListQuery, {
|
|
27
|
+
count: PAGE_SIZE,
|
|
28
|
+
})
|
|
29
|
+
.toPromise()
|
|
30
|
+
.catch(() => {});
|
|
31
|
+
},
|
|
7
32
|
content: CatalogPage,
|
|
8
33
|
}),
|
|
9
34
|
} as const;
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
<% if (intl) { -%>
|
|
2
|
+
import { useTranslation } from "@canonical/i18n-react";
|
|
3
|
+
<% } -%>
|
|
1
4
|
import { Button } from "@canonical/react-ds-global";
|
|
2
5
|
import { Field, Form } from "@canonical/react-ds-global-form";
|
|
3
6
|
import { useHead } from "@canonical/react-head";
|
|
@@ -8,36 +11,73 @@ function handleSubmit(data: Record<string, unknown>) {
|
|
|
8
11
|
}
|
|
9
12
|
|
|
10
13
|
export default function ContactPage(): ReactElement {
|
|
14
|
+
<% if (intl) { -%>
|
|
15
|
+
const { t } = useTranslation();
|
|
16
|
+
useHead({ title: t("contact.title") });
|
|
17
|
+
<% } else { -%>
|
|
11
18
|
useHead({ title: "Contact" });
|
|
19
|
+
<% } -%>
|
|
12
20
|
|
|
13
21
|
return (
|
|
14
22
|
<section aria-labelledby="contact-title">
|
|
23
|
+
<% if (intl) { -%>
|
|
24
|
+
<h1 id="contact-title">{t("contact.heading")}</h1>
|
|
25
|
+
<% } else { -%>
|
|
15
26
|
<h1 id="contact-title">Contact</h1>
|
|
27
|
+
<% } -%>
|
|
16
28
|
<Form onSubmit={handleSubmit}>
|
|
29
|
+
<% if (intl) { -%>
|
|
30
|
+
<Field name="name" inputType="text" label={t("contact.name")} />
|
|
31
|
+
<% } else { -%>
|
|
17
32
|
<Field name="name" inputType="text" label="Full name" />
|
|
33
|
+
<% } -%>
|
|
18
34
|
<Field
|
|
19
35
|
name="email"
|
|
20
36
|
inputType="text"
|
|
37
|
+
<% if (intl) { -%>
|
|
38
|
+
label={t("contact.email")}
|
|
39
|
+
registerProps={{ required: t("contact.emailRequired") }}
|
|
40
|
+
<% } else { -%>
|
|
21
41
|
label="Email address"
|
|
22
42
|
registerProps={{ required: "Email is required" }}
|
|
43
|
+
<% } -%>
|
|
23
44
|
/>
|
|
24
45
|
<Field
|
|
25
46
|
name="subject"
|
|
26
47
|
inputType="select"
|
|
48
|
+
<% if (intl) { -%>
|
|
49
|
+
label={t("contact.subject")}
|
|
50
|
+
<% } else { -%>
|
|
27
51
|
label="Subject"
|
|
52
|
+
<% } -%>
|
|
28
53
|
options={[
|
|
54
|
+
<% if (intl) { -%>
|
|
55
|
+
{ value: "general", label: t("contact.subjectGeneral") },
|
|
56
|
+
{ value: "support", label: t("contact.subjectSupport") },
|
|
57
|
+
{ value: "feedback", label: t("contact.subjectFeedback") },
|
|
58
|
+
<% } else { -%>
|
|
29
59
|
{ value: "general", label: "General enquiry" },
|
|
30
60
|
{ value: "support", label: "Support" },
|
|
31
61
|
{ value: "feedback", label: "Feedback" },
|
|
62
|
+
<% } -%>
|
|
32
63
|
]}
|
|
33
64
|
/>
|
|
34
65
|
<Field
|
|
35
66
|
name="message"
|
|
36
67
|
inputType="textarea"
|
|
68
|
+
<% if (intl) { -%>
|
|
69
|
+
label={t("contact.message")}
|
|
70
|
+
description={t("contact.messageHint")}
|
|
71
|
+
<% } else { -%>
|
|
37
72
|
label="Message"
|
|
38
73
|
description="Maximum 500 characters"
|
|
74
|
+
<% } -%>
|
|
39
75
|
/>
|
|
76
|
+
<% if (intl) { -%>
|
|
77
|
+
<Button type="submit">{t("contact.send")}</Button>
|
|
78
|
+
<% } else { -%>
|
|
40
79
|
<Button type="submit">Send message</Button>
|
|
80
|
+
<% } -%>
|
|
41
81
|
</Form>
|
|
42
82
|
</section>
|
|
43
83
|
);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
<% if (intl) { -%>
|
|
2
|
+
import { useTranslation } from "@canonical/i18n-react";
|
|
3
|
+
<% } -%>
|
|
4
|
+
import { useHead } from "@canonical/react-head";
|
|
5
|
+
import type { ReactElement } from "react";
|
|
6
|
+
|
|
7
|
+
export default function GuidePage({
|
|
8
|
+
params,
|
|
9
|
+
}: {
|
|
10
|
+
params: { slug: string };
|
|
11
|
+
}): ReactElement {
|
|
12
|
+
<% if (intl) { -%>
|
|
13
|
+
const { t, locale } = useTranslation();
|
|
14
|
+
// `locale` is a dependency: switching languages must retranslate the title.
|
|
15
|
+
useHead({ title: t("guide.title", { slug: params.slug }) }, [
|
|
16
|
+
params.slug,
|
|
17
|
+
locale,
|
|
18
|
+
]);
|
|
19
|
+
<% } else { -%>
|
|
20
|
+
useHead({ title: `${params.slug} — Guides` }, [params.slug]);
|
|
21
|
+
<% } -%>
|
|
22
|
+
|
|
23
|
+
return (
|
|
24
|
+
<section aria-labelledby="guide-title">
|
|
25
|
+
<h1 id="guide-title">{params.slug}</h1>
|
|
26
|
+
<% if (intl) { -%>
|
|
27
|
+
<p>{t("guide.body", { slug: params.slug })}</p>
|
|
28
|
+
<% } else { -%>
|
|
29
|
+
<p>Guide content for {params.slug}.</p>
|
|
30
|
+
<% } -%>
|
|
31
|
+
</section>
|
|
32
|
+
);
|
|
33
|
+
}
|
package/dist/esm/application/react/templates/src/domains/marketing/{HomePage.tsx → HomePage.tsx.ejs}
RENAMED
|
@@ -1,16 +1,38 @@
|
|
|
1
|
+
<% if (intl) { -%>
|
|
2
|
+
import { useTranslation } from "@canonical/i18n-react";
|
|
3
|
+
<% } -%>
|
|
1
4
|
import { useHead } from "@canonical/react-head";
|
|
2
5
|
import { type ReactElement, Suspense } from "react";
|
|
3
6
|
import { ExampleComponent, LazyComponent } from "#lib/index.js";
|
|
4
7
|
|
|
5
8
|
export default function HomePage(): ReactElement {
|
|
9
|
+
<% if (intl) { -%>
|
|
10
|
+
const { t } = useTranslation();
|
|
11
|
+
useHead({ title: t("home.title") });
|
|
12
|
+
<% } else { -%>
|
|
6
13
|
useHead({ title: "Home — Boilerplate" });
|
|
14
|
+
<% } -%>
|
|
7
15
|
|
|
8
16
|
return (
|
|
9
17
|
<section aria-labelledby="home-title">
|
|
18
|
+
<% if (intl) { -%>
|
|
19
|
+
<h1 id="home-title">{t("home.heading")}</h1>
|
|
20
|
+
<p>{t("home.tagline")}</p>
|
|
21
|
+
<% } else { -%>
|
|
10
22
|
<h1 id="home-title">Home</h1>
|
|
11
23
|
<p>Welcome to the pragma router boilerplate.</p>
|
|
24
|
+
<% } -%>
|
|
12
25
|
|
|
26
|
+
<% if (intl) { -%>
|
|
27
|
+
<h2>{t("home.exampleHeading")}</h2>
|
|
28
|
+
{/*
|
|
29
|
+
The explanatory paragraphs below are developer documentation rather
|
|
30
|
+
than user-facing copy — they reference file names and APIs — so they
|
|
31
|
+
stay out of the message catalogs deliberately.
|
|
32
|
+
*/}
|
|
33
|
+
<% } else { -%>
|
|
13
34
|
<h2>Example component</h2>
|
|
35
|
+
<% } -%>
|
|
14
36
|
<p>
|
|
15
37
|
A plain component scaffolded alongside the app. It ships with a
|
|
16
38
|
Storybook story (<code>ExampleComponent.stories.tsx</code>) and a test —
|
|
@@ -18,14 +40,22 @@ export default function HomePage(): ReactElement {
|
|
|
18
40
|
</p>
|
|
19
41
|
<ExampleComponent>Hello from ExampleComponent</ExampleComponent>
|
|
20
42
|
|
|
43
|
+
<% if (intl) { -%>
|
|
44
|
+
<h2>{t("home.streamingHeading")}</h2>
|
|
45
|
+
<% } else { -%>
|
|
21
46
|
<h2>Streaming with Suspense</h2>
|
|
47
|
+
<% } -%>
|
|
22
48
|
<p>
|
|
23
49
|
<code>LazyComponent</code> reads a synthetic delayed fetch with React 19{" "}
|
|
24
50
|
<code>use()</code>, so it suspends. Inside this <code>Suspense</code>{" "}
|
|
25
51
|
boundary the server streams the page shell first, shows the fallback,
|
|
26
52
|
then flushes the resolved content in — no full client round-trip.
|
|
27
53
|
</p>
|
|
54
|
+
<% if (intl) { -%>
|
|
55
|
+
<Suspense fallback={<p>{t("home.streamFallback")}</p>}>
|
|
56
|
+
<% } else { -%>
|
|
28
57
|
<Suspense fallback={<p>Loading streamed content…</p>}>
|
|
58
|
+
<% } -%>
|
|
29
59
|
<LazyComponent />
|
|
30
60
|
</Suspense>
|
|
31
61
|
</section>
|