Fetch Directus data

This commit is contained in:
2024-05-17 08:29:06 +04:00
parent 28160dbd23
commit cfa4ff6b0d
4 changed files with 62 additions and 2 deletions
+1
View File
@@ -0,0 +1 @@
DIRECTUS_API_URL=
+41 -1
View File
@@ -1,5 +1,45 @@
import {createDirectus, rest, readItems} from '@directus/sdk'
import {Typography} from '@mui/material' 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 <Typography variant='h1'>konstitisyon.la</Typography> return <Typography variant='h1'>konstitisyon.la</Typography>
} }
+17
View File
@@ -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
}
+3 -1
View File
@@ -34,7 +34,9 @@
{ {
"namedComponents": "function-declaration" "namedComponents": "function-declaration"
} }
] ],
"n/prefer-global/process": "off",
"comma-dangle": "off"
} }
} }
} }