2024-05-20 14:49:31 +04:00
|
|
|
'use client'
|
2024-05-18 09:36:44 +04:00
|
|
|
import PropTypes from 'prop-types'
|
2024-06-18 11:11:15 +04:00
|
|
|
import {useState} from 'react'
|
2024-05-18 09:36:44 +04:00
|
|
|
import Box from '@mui/material/Box'
|
|
|
|
|
import Paper from '@mui/material/Paper'
|
2024-05-20 14:49:31 +04:00
|
|
|
import {styled} from '@mui/material/styles'
|
|
|
|
|
import IconButton from '@mui/material/IconButton'
|
|
|
|
|
import AddCommentIcon from '@mui/icons-material/AddComment'
|
2024-06-19 09:07:15 +04:00
|
|
|
import CommentIcon from '@mui/icons-material/Comment'
|
2024-05-20 14:49:31 +04:00
|
|
|
import Tooltip, {tooltipClasses} from '@mui/material/Tooltip'
|
2024-06-18 11:11:15 +04:00
|
|
|
import AuthAlert from '../auth-form/auth-alert.js'
|
2024-05-18 09:36:44 +04:00
|
|
|
import Titre from './titre.js'
|
|
|
|
|
import Article from './article.js'
|
2024-06-22 21:11:01 +04:00
|
|
|
import HandleCreate from './create/handle-create.js'
|
2024-06-22 21:18:05 +04:00
|
|
|
import ReadComments from './comments/read-comments.js'
|
2024-05-20 04:17:45 +04:00
|
|
|
import {formatKonstitisyon} from '@/lib/format.js'
|
2024-05-18 09:36:44 +04:00
|
|
|
|
2024-05-20 14:49:31 +04:00
|
|
|
const LightTooltip = styled(({className, ...props}) => (
|
|
|
|
|
<Tooltip {...props} classes={{popper: className}} />
|
|
|
|
|
))(({theme}) => ({
|
|
|
|
|
[`& .${tooltipClasses.tooltip}`]: {
|
|
|
|
|
backgroundColor: theme.palette.common.white,
|
|
|
|
|
color: 'rgba(0, 0, 0, 0.87)',
|
|
|
|
|
boxShadow: theme.shadows[1],
|
|
|
|
|
fontSize: 15,
|
|
|
|
|
},
|
|
|
|
|
}))
|
|
|
|
|
|
2024-05-20 14:48:52 +04:00
|
|
|
export default function Konstitisyon({session, titres, articles}) {
|
2024-05-18 09:36:44 +04:00
|
|
|
const konstitisyon = formatKonstitisyon(titres, articles)
|
2024-06-19 14:44:49 +04:00
|
|
|
const [isDialogOpen, setIsDialogOpen] = useState(false)
|
|
|
|
|
const [operation, setOperation] = useState(null)
|
2024-06-18 11:11:15 +04:00
|
|
|
const [isErrorAlertOpen, setIsErrorAlertOpen] = useState(false)
|
|
|
|
|
const [isSuccessAlertOpen, setIsSuccessAlertOpen] = useState(false)
|
|
|
|
|
const [selectedTitre, setSelectedTitre] = useState(null)
|
|
|
|
|
const [error, setError] = useState('')
|
|
|
|
|
const [success, setSuccess] = useState('')
|
|
|
|
|
|
2024-06-19 09:07:15 +04:00
|
|
|
const handleCommentsDialog = (titreId, titre, action) => {
|
2024-06-18 11:11:15 +04:00
|
|
|
setSelectedTitre({id: titreId, titre})
|
2024-06-19 14:44:49 +04:00
|
|
|
setOperation(action)
|
|
|
|
|
setIsDialogOpen(true)
|
2024-06-18 11:11:15 +04:00
|
|
|
}
|
2024-05-18 09:36:44 +04:00
|
|
|
|
|
|
|
|
return (
|
2024-06-18 11:11:15 +04:00
|
|
|
<>
|
|
|
|
|
{error && <AuthAlert
|
|
|
|
|
isOpen={isErrorAlertOpen}
|
|
|
|
|
setIsOpen={setIsErrorAlertOpen}
|
|
|
|
|
message={error}
|
|
|
|
|
severity='error'
|
|
|
|
|
/>}
|
|
|
|
|
|
|
|
|
|
{success && <AuthAlert
|
|
|
|
|
isOpen={isSuccessAlertOpen}
|
|
|
|
|
setIsOpen={setIsSuccessAlertOpen}
|
|
|
|
|
message={success}
|
|
|
|
|
severity='success'
|
|
|
|
|
/>}
|
|
|
|
|
|
|
|
|
|
<Box>
|
|
|
|
|
{konstitisyon.map(({titreId, titre, articles}) => (
|
|
|
|
|
<Paper key={titreId} variant='outlined' sx={{p: 1, marginBlock: 2}} p={2} >
|
|
|
|
|
<Titre session={session} titreId={titreId} titre={titre} />
|
|
|
|
|
{articles.map(({id, numero, contenu}) => (
|
|
|
|
|
<Article key={id} session={session} articleId={id} numero={numero} contenu={contenu} />
|
|
|
|
|
))}
|
|
|
|
|
{session && (
|
2024-06-19 09:07:15 +04:00
|
|
|
<Box sx={{display: 'flex', justifyContent: 'space-between'}}>
|
|
|
|
|
<Box>
|
|
|
|
|
<IconButton size='large' aria-label='Voir les commentaires' onClick={() => handleCommentsDialog(titreId, titre, 'read')}>
|
|
|
|
|
<LightTooltip title='Voir les commentaires'>
|
|
|
|
|
<CommentIcon color='warning' fontSize='inherit' />
|
|
|
|
|
</LightTooltip>
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Box>
|
|
|
|
|
<Box>
|
2024-06-22 07:58:04 +04:00
|
|
|
<IconButton size='large' aria-label='commenter' onClick={() => handleCommentsDialog(titreId, titre, 'create')}>
|
2024-06-19 09:07:15 +04:00
|
|
|
<LightTooltip title='Commenter'>
|
|
|
|
|
<AddCommentIcon color='warning' fontSize='inherit' />
|
|
|
|
|
</LightTooltip>
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Box>
|
2024-06-18 11:11:15 +04:00
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
</Paper>
|
|
|
|
|
))}
|
2024-06-22 21:11:01 +04:00
|
|
|
{selectedTitre && operation === 'create' && (
|
|
|
|
|
<HandleCreate
|
2024-06-19 14:44:49 +04:00
|
|
|
session={session}
|
|
|
|
|
selectedTitre={selectedTitre}
|
|
|
|
|
isOpen={isDialogOpen}
|
|
|
|
|
setIsOpen={setIsDialogOpen}
|
|
|
|
|
setError={setError}
|
|
|
|
|
setSuccess={setSuccess}
|
|
|
|
|
setIsErrorAlertOpen={setIsErrorAlertOpen}
|
|
|
|
|
setIsSuccessAlertOpen={setIsSuccessAlertOpen}
|
2024-06-22 21:11:01 +04:00
|
|
|
collection='commentaires'
|
2024-06-19 14:44:49 +04:00
|
|
|
/>
|
2024-06-18 11:11:15 +04:00
|
|
|
)}
|
2024-06-22 21:18:05 +04:00
|
|
|
|
|
|
|
|
{selectedTitre && operation === 'read' && (
|
|
|
|
|
<ReadComments
|
|
|
|
|
session={session}
|
|
|
|
|
selectedTitre={selectedTitre}
|
|
|
|
|
isOpen={isDialogOpen}
|
|
|
|
|
setIsOpen={setIsDialogOpen}
|
|
|
|
|
setError={setError}
|
|
|
|
|
setIsErrorAlertOpen={setIsErrorAlertOpen}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2024-06-18 11:11:15 +04:00
|
|
|
</Box>
|
|
|
|
|
</>
|
2024-05-18 09:36:44 +04:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Konstitisyon.propTypes = {
|
2024-05-20 14:48:52 +04:00
|
|
|
session: PropTypes.object,
|
2024-05-18 09:36:44 +04:00
|
|
|
titres: PropTypes.object.isRequired,
|
|
|
|
|
articles: PropTypes.object.isRequired
|
|
|
|
|
}
|