Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use grist to display homepage articles #35

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 19 additions & 61 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { fr } from "@codegouvfr/react-dsfr";
import AutocompleteSearch from "@/components/AutocompleteSearch";
import Link from "next/link";
import Card from "@codegouvfr/react-dsfr/Card";
import { getArticles } from "@/data/articles";

export default async function Page() {
const articles = (await getArticles()).filter(({ homepage }) => homepage);

return (
<>
<div className={fr.cx("fr-container", "fr-pt-4w", "fr-pb-4w")}>
Expand All @@ -30,67 +33,22 @@ export default async function Page() {
</h2>
</div>
<div className={fr.cx("fr-col-md-6")}>
<p>
<Link
href="#"
className={fr.cx(
"fr-link",
"fr-link--icon-left",
"fr-icon-arrow-right-line",
)}
>
Je suis enceinte, quelles précautions prendre&nbsp;?
</Link>
</p>
<p>
<Link
href="#"
className={fr.cx(
"fr-link",
"fr-link--icon-left",
"fr-icon-arrow-right-line",
)}
>
Je ne me sens plus malade, est ce que je peux arrêter mon
traitement&nbsp;?
</Link>
</p>
<p>
<Link
href="#"
className={fr.cx(
"fr-link",
"fr-link--icon-left",
"fr-icon-arrow-right-line",
)}
>
Dois-je me faire vacciner contre la grippe saisonnière&nbsp;?
</Link>
</p>
<p>
<Link
href="#"
className={fr.cx(
"fr-link",
"fr-link--icon-left",
"fr-icon-arrow-right-line",
)}
>
Que faire si j&apos;ai des effets indésirables&nbsp;?
</Link>
</p>
<p>
<Link
href="#"
className={fr.cx(
"fr-link",
"fr-link--icon-left",
"fr-icon-arrow-right-line",
)}
>
Numéros d&apos;urgence
</Link>
</p>
<ul role="nav" className={fr.cx("fr-raw-list")}>
{articles.map(({ title, slug }) => (
<li key={slug} className={fr.cx("fr-mb-3w")}>
<Link
href={`/articles/${slug}`}
className={fr.cx(
"fr-link",
"fr-link--icon-left",
"fr-icon-arrow-right-line",
)}
>
{title}
</Link>
</li>
))}
</ul>
</div>
<div
className={fr.cx(
Expand Down
2 changes: 2 additions & 0 deletions src/data/articles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export async function getArticles() {
"Source",
"Contenu",
"Theme",
"Homepage",
]);

return records.map(({ fields }) => {
Expand All @@ -17,6 +18,7 @@ export async function getArticles() {
source: fields.Source as string,
content: fields.Contenu as string,
category: fields.Theme as string,
homepage: fields.Homepage as boolean,
};
});
}
6 changes: 3 additions & 3 deletions src/data/grist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const gristCache = new Map<string, Promise<any>>();
export const getGristTableData = <F extends string>(
tableId: string,
fields: F[],
): Promise<{ id: number; fields: Record<F, string | number> }[]> => {
): Promise<{ id: number; fields: Record<F, string | number | boolean> }[]> => {
if (!gristCache.has(tableId)) {
gristCache.set(tableId, uncachedGetGristTableData(tableId, fields));
}
Expand All @@ -24,9 +24,9 @@ export const getGristTableData = <F extends string>(
async function uncachedGetGristTableData<F extends string>(
tableId: string,
fields: F[],
): Promise<{ id: number; fields: Record<F, string | number> }[]> {
): Promise<{ id: number; fields: Record<F, string | number | boolean> }[]> {
const response = await fetch(
`https://grist.numerique.gouv.fr/api/docs/${process.env.GRIST_DOC_ID}/tables/${tableId}/records`,
`https://grist.numerique.gouv.fr/api/docs/${process.env.GRIST_DOC_ID}/tables/${tableId}/records?sort=manualSort`,
{
headers: {
Authorization: `Bearer ${process.env.GRIST_API_KEY}`,
Expand Down