From cfa4ff6b0d15548f1e6a875e8c9f6c5e3220f722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20FAMIBELLE-PRONZOLA?= Date: Fri, 17 May 2024 08:29:06 +0400 Subject: [PATCH] Fetch Directus data --- .env.sample | 1 + app/page.js | 42 +++++++++++++++++++++++++++++++++++++++++- lib/format.js | 17 +++++++++++++++++ package.json | 4 +++- 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 .env.sample create mode 100644 lib/format.js diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..c54e70a --- /dev/null +++ b/.env.sample @@ -0,0 +1 @@ +DIRECTUS_API_URL= diff --git a/app/page.js b/app/page.js index 65ba824..8e2feaa 100644 --- a/app/page.js +++ b/app/page.js @@ -1,5 +1,45 @@ +import {createDirectus, rest, readItems} from '@directus/sdk' import {Typography} from '@mui/material' +import {formatKonstitisyon} from '../lib/format.js' + +async function getData() { + const apiUrl = process.env.DIRECTUS_API_URL + + if (!apiUrl) { + throw new Error('DIRECTUS_API_URL is required') + } + + const client = createDirectus(apiUrl).with(rest()) + + try { + const titres = await client.request( + readItems('titres', { + sort: 'numero' + }) + ) + + const articles = await client.request( + readItems('articles', { + sort: 'numero' + }) + ) + + if (titres.length === 0 || articles.length === 0) { + throw new Error('No data') + } + + const konstitisyon = formatKonstitisyon(titres, articles) + + return konstitisyon + } catch { + throw new Error('Failed to fetch data') + } +} + +export default async function Page() { + const data = await getData() + + console.log('data', data) -export default function Page() { return konstitisyon.la } diff --git a/lib/format.js b/lib/format.js new file mode 100644 index 0000000..bf5f3af --- /dev/null +++ b/lib/format.js @@ -0,0 +1,17 @@ +export function formatKonstitisyon(titres, articles) { + const konstitisyon = [] + + for (const titre of titres) { + const articlesFromTitres = articles.filter(article => article.titre === titre.id) + + konstitisyon.push( + { + titre: titre.contenu, + titreId: titre.id, + articles: articlesFromTitres + } + ) + } + + return konstitisyon +} diff --git a/package.json b/package.json index b6f8bcf..ff93d31 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,9 @@ { "namedComponents": "function-declaration" } - ] + ], + "n/prefer-global/process": "off", + "comma-dangle": "off" } } }