Files
konstitisyon.nu/components/konstitisyon/list-comments.js
T

148 lines
4.9 KiB
JavaScript
Raw Normal View History

2024-06-19 09:07:15 +04:00
/* eslint-disable camelcase */
2024-06-19 09:11:29 +04:00
import React, {useEffect, useState, useRef} from 'react'
2024-06-19 09:07:15 +04:00
import PropTypes from 'prop-types'
import List from '@mui/material/List'
import ListItem from '@mui/material/ListItem'
import ListItemText from '@mui/material/ListItemText'
import DialogTitle from '@mui/material/DialogTitle'
import Dialog from '@mui/material/Dialog'
import Typography from '@mui/material/Typography'
import Pagination from '@mui/material/Pagination'
import Divider from '@mui/material/Divider'
import Box from '@mui/material/Box'
import CircularProgress from '@mui/material/CircularProgress'
2024-06-19 09:07:15 +04:00
import {readItems, withToken} from '@directus/sdk'
import SessionExpired from '../session/session-expired.js'
2024-07-02 15:00:02 +02:00
import {directusClient, handleUserStatus} from '@/lib/directus.js'
2024-06-19 09:07:15 +04:00
import {formatDate} from '@/lib/format.js'
const commentsPerPage = process.env.NEXT_PUBLIC_COMMENTS_PER_PAGE || 2
2024-06-23 06:52:27 +04:00
export default function ListComments({session, selectedTitre, isOpen, setIsOpen, setError, setIsErrorAlertOpen}) {
2024-06-19 09:11:29 +04:00
const countdownRef = useRef()
2024-06-19 09:07:15 +04:00
const [comments, setComments] = useState([])
const [isLoading, setIsLoading] = useState(false)
2024-06-19 09:07:15 +04:00
const [page, setPage] = useState(1)
const pageCount = Math.ceil(comments.length / commentsPerPage)
const startIndex = (page - 1) * commentsPerPage
const selectedComments = comments.slice(startIndex, startIndex + commentsPerPage)
useEffect(() => {
setComments([])
setPage(1)
}, [selectedTitre?.id])
2024-06-19 09:07:15 +04:00
useEffect(() => {
async function fetchComments() {
setIsLoading(true)
2024-06-19 09:07:15 +04:00
try {
2024-07-02 15:00:02 +02:00
await handleUserStatus(session.user.accessToken, session.user.userId)
2024-06-19 09:07:15 +04:00
const result = await directusClient.request(withToken(
session.user.accessToken,
readItems('commentaires', {
filter: {
2024-07-02 11:04:24 +02:00
titre: selectedTitre.id,
status: {
_eq: 'published'
}
2024-06-19 09:07:15 +04:00
},
sort: '-date_created'
}))
)
setComments(result)
} catch (error) {
2024-06-19 09:17:22 +04:00
if (error?.errors[0]?.message === 'Token expired.') {
countdownRef.current.startCountdown()
} else {
console.log(error?.errors[0]?.message)
setError(error?.errors[0]?.message)
setIsErrorAlertOpen(true)
}
} finally {
setIsLoading(false)
2024-06-19 09:07:15 +04:00
}
}
if (isOpen) {
fetchComments()
}
}, [isOpen, selectedTitre, setError, setIsErrorAlertOpen, session])
const handleClose = () => {
setIsOpen(false)
}
const handleChange = (event, value) => {
setPage(value)
}
return (
2024-06-19 09:11:29 +04:00
<>
<Dialog open={isOpen} onClose={handleClose}>
<DialogTitle>Commentaires</DialogTitle>
{isLoading ? (
<Box sx={{display: 'flex', justifyContent: 'center', p: 4}}>
<CircularProgress />
</Box>
) : (
<>
<List sx={{width: '100%', maxWidth: 360, bgcolor: 'background.paper'}}>
{selectedComments && selectedComments.length > 0 ? selectedComments.map(({id, date_created, contenu, user_created}) => (
<React.Fragment key={id}>
<ListItem alignItems='flex-start'>
<ListItemText
primary={
<Typography sx={{textDecoration: 'underline'}} component='span' variant='body2'>
@{user_created.split('-')[0]}
</Typography>
}
secondary={
<>
<Typography
sx={{display: 'inline'}}
component='span'
variant='body2'
color='text.primary'
>
{contenu}
</Typography>
<br />
{formatDate(date_created, 'PPPPpp')}
</>
}
/>
</ListItem>
2024-06-19 09:07:15 +04:00
<Divider component='li' />
</React.Fragment>
)) : (
<Typography textAlign='center' sx={{p: 2}}>Aucun commentaire</Typography>
)}
</List>
{pageCount > 1 && (
<Box sx={{display: 'flex', justifyContent: 'center'}}>
<Pagination size='small' sx={{marginBlock: 3}} color='success' count={pageCount} page={page} onChange={handleChange} />
</Box>
)}
</>
)}
2024-06-19 09:11:29 +04:00
</Dialog>
<SessionExpired ref={countdownRef} setError={setError} setIsErrorAlertOpen={setIsErrorAlertOpen} />
2024-06-19 09:11:29 +04:00
</>
2024-06-19 09:07:15 +04:00
)
}
2024-06-23 06:52:27 +04:00
ListComments.propTypes = {
2024-06-19 09:07:15 +04:00
session: PropTypes.object,
selectedTitre: PropTypes.object.isRequired,
isOpen: PropTypes.bool.isRequired,
setIsOpen: PropTypes.func.isRequired,
setError: PropTypes.func.isRequired,
setIsErrorAlertOpen: PropTypes.func.isRequired
}