From 54a251e729918eb5b37a2cdcd2cff9489eda0977 Mon Sep 17 00:00:00 2001 From: Maud Royer Date: Tue, 22 Oct 2024 16:27:59 +0200 Subject: [PATCH] feat: add pathologies definitions + a few tweaks to cache grist requests Signed-off-by: Maud Royer --- src/app/(container)/pathologie/[code]/page.tsx | 10 ++++++---- src/data/grist.ts | 7 +++++-- src/data/pathologies.ts | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 src/data/pathologies.ts diff --git a/src/app/(container)/pathologie/[code]/page.tsx b/src/app/(container)/pathologie/[code]/page.tsx index 6098715..da89872 100644 --- a/src/app/(container)/pathologie/[code]/page.tsx +++ b/src/app/(container)/pathologie/[code]/page.tsx @@ -7,6 +7,7 @@ import liste_CIS_MVP from "@/liste_CIS_MVP.json"; import { fr } from "@codegouvfr/react-dsfr"; import { MedGroupSpecListList } from "@/components/MedGroupSpecList"; import Breadcrumb from "@codegouvfr/react-dsfr/Breadcrumb"; +import { getPathologyDefinition } from "@/data/pathologies"; export async function generateStaticParams(): Promise<{ code: string }[]> { return pdbmMySQL.selectFrom("Patho").select("codePatho as code").execute(); @@ -24,7 +25,7 @@ async function getPatho(code: string): Promise { return patho; } -async function getSpecialite(code: string): Promise { +async function getPathoSpecialites(code: `${number}`): Promise { return pdbmMySQL .selectFrom("Specialite") .selectAll("Specialite") @@ -37,10 +38,11 @@ async function getSpecialite(code: string): Promise { export default async function Page({ params: { code }, }: { - params: { code: string }; + params: { code: `${number}` }; }) { const patho = await getPatho(code); - const specialites = await getSpecialite(code); + const definition = await getPathologyDefinition(code); + const specialites = await getPathoSpecialites(code); const medicaments = groupSpecialites(specialites); return ( <> @@ -59,7 +61,7 @@ export default async function Page({

diff --git a/src/data/grist.ts b/src/data/grist.ts index f2243f1..0b880b5 100644 --- a/src/data/grist.ts +++ b/src/data/grist.ts @@ -1,4 +1,7 @@ -export async function getGristTableData( +import "server-only"; +import { unstable_cache } from "next/cache"; + +export const getGristTableData = unstable_cache(async function ( tableId: string, ): Promise<{ id: number; fields: Record }[]> { const response = await fetch( @@ -10,4 +13,4 @@ export async function getGristTableData( }, ); return (await response.json()).records; -} +}); diff --git a/src/data/pathologies.ts b/src/data/pathologies.ts new file mode 100644 index 0000000..3361623 --- /dev/null +++ b/src/data/pathologies.ts @@ -0,0 +1,15 @@ +import { getGristTableData } from "@/data/grist"; + +export async function getPathologyDefinition( + code: `${number}`, +): Promise { + const data = await getGristTableData("Pathologies"); + const record = data.find( + (record: any) => record.fields.codePatho === Number(code), + ); + if (!record) { + throw new Error(`Pathology code not found: ${code}`); + } + + return record.fields.Definition_pathologie as string; +}