Files
konstitisyon.nu/components/versions/get-versions.js
T

134 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-09-15 18:02:57 +04:00
'use client'
import {useState, useRef, useEffect} from 'react'
import PropTypes from 'prop-types'
import Grid from '@mui/material/Grid2'
import Typography from '@mui/material/Typography'
2024-09-20 11:45:43 +04:00
import HomeIcon from '@mui/icons-material/Home'
2024-09-15 18:02:57 +04:00
import Box from '@mui/material/Box'
import AuthAlert from '../auth-form/auth-alert.js'
import LogoutCountdown from '../session/logout-countdown.js'
import {Loading} from '../loading.js'
import ListVersions from './list-versions.js'
import {listVersions} from '@/lib/directus.js'
2024-09-20 11:45:43 +04:00
import Sign from '@/components/session/sign.js'
const navButton = {
title: 'Accueil',
path: '/',
color: 'success',
icon: <HomeIcon fontSize='large' />
}
2024-09-15 18:02:57 +04:00
export default function GetVersions({session}) {
const {accessToken, userId} = session.user
const [versions, setVersions] = useState(null)
const [isErrorAlertOpen, setIsErrorAlertOpen] = useState(false)
const [error, setError] = useState('')
const countdownRef = useRef()
useEffect(() => {
async function fetchVersions() {
const data = await listVersions({
accessToken, userId, countdownRef, setError, setIsErrorAlertOpen
})
setVersions(data)
}
fetchVersions()
}, [accessToken, userId])
if (!versions) {
return (
<>
{error && <AuthAlert
isOpen={isErrorAlertOpen}
setIsOpen={setIsErrorAlertOpen}
message={error}
severity='error'
/>}
<Loading />
<LogoutCountdown ref={countdownRef} setError={setError} setIsErrorAlertOpen={setIsErrorAlertOpen} />
</>
)
}
const titres = versions?.filter(({collection}) => collection === 'titres')
const articles = versions?.filter(({collection}) => collection === 'articles')
return (
<>
{error && <AuthAlert
isOpen={isErrorAlertOpen}
setIsOpen={setIsErrorAlertOpen}
message={error}
severity='error'
/>}
2024-09-20 11:45:43 +04:00
<Sign session={session} navButton={navButton} />
2024-09-15 18:02:57 +04:00
<Box
sx={{
border: '2px solid #ccc',
borderRadius: '8px',
padding: 1,
my: 4
}}
>
<Typography
gutterBottom
variant='h5'
component='h2'
textAlign='center'
sx={{display: 'block', fontWeight: 'bold', textTransform: 'uppercase'}}
>
Liste des versions
</Typography>
<Grid
container
spacing={2}
justifyContent={
(titres.length > 0 && articles.length === 0) || (articles.length > 0 && titres.length === 0)
? 'center'
: 'flex-start'
}
>
{titres.length > 0 && (
<Grid size={{xs: 12, md: articles.length > 0 ? 6 : 12}}>
2024-11-28 07:54:37 +04:00
<ListVersions
collection='Titres'
data={titres}
accessToken={accessToken}
userId={userId}
setError={setError}
setIsErrorAlertOpen={setIsErrorAlertOpen}
/>
2024-09-15 18:02:57 +04:00
</Grid>
)}
{articles.length > 0 && (
<Grid size={{xs: 12, md: titres.length > 0 ? 6 : 12}}>
2024-11-28 07:54:37 +04:00
<ListVersions
collection='Articles'
data={articles}
accessToken={accessToken}
userId={userId}
setError={setError}
setIsErrorAlertOpen={setIsErrorAlertOpen}
/>
2024-09-15 18:02:57 +04:00
</Grid>
)}
</Grid>
</Box>
<LogoutCountdown ref={countdownRef} setError={setError} setIsErrorAlertOpen={setIsErrorAlertOpen} />
</>
)
}
GetVersions.propTypes = {
session: PropTypes.object.isRequired
}