Files
pawol.nu/components/komante/vwe-komante.js
T

141 lines
3.9 KiB
JavaScript
Raw Normal View History

2021-06-26 12:27:48 +02:00
import {useState, useEffect, useRef} from 'react'
2022-01-19 06:35:04 +04:00
import {styled} from '@mui/material/styles'
2021-06-26 12:27:48 +02:00
import PropTypes from 'prop-types'
2022-02-03 01:59:49 +04:00
import {useSession} from 'next-auth/react'
2021-06-26 12:27:48 +02:00
import {
Dialog,
DialogTitle,
DialogContent,
DialogActions,
Button,
Typography,
2022-03-26 15:41:05 +04:00
Badge
2022-01-19 07:06:26 +04:00
} from '@mui/material'
2021-06-26 12:27:48 +02:00
import {useRouter} from 'next/router'
2022-01-18 09:08:26 +04:00
import Koneksyon from '../sesyon/koneksyon'
import KomanteList from './komante-list'
2021-06-26 12:27:48 +02:00
import EkriKomante from './ekri-komante'
2022-01-19 06:35:04 +04:00
const PREFIX = 'vwe-komante'
const classes = {
tooltip: `${PREFIX}-tooltip`,
margin: `${PREFIX}-margin`,
extendedIcon: `${PREFIX}-extendedIcon`
}
const Root = styled('div')((
{
theme
}
) => ({
[`& .${classes.margin}`]: {
2021-06-26 12:27:48 +02:00
margin: theme.spacing(1)
},
2022-01-19 06:35:04 +04:00
[`& .${classes.extendedIcon}`]: {
2021-06-26 12:27:48 +02:00
marginRight: theme.spacing(1)
}
}))
2022-05-20 02:19:10 +04:00
export default function VweKomante({commentaires, parole, paroleId}) {
2021-06-26 12:27:48 +02:00
const [esOuve, meteEsOuve] = useState(false)
const [esKoneksyonOuve, meteEsKoneksyonOuve] = useState(false)
const [esKomenteOuve, meteEsKomanteOuve] = useState(false)
2022-02-03 01:59:49 +04:00
const {data: session} = useSession()
2021-06-26 12:27:48 +02:00
const router = useRouter()
const handleClick = () => {
meteEsOuve(true)
}
const handleClose = () => {
meteEsOuve(false)
}
const descriptionElementRef = useRef(null)
useEffect(() => {
if (esOuve) {
const {current: descriptionElement} = descriptionElementRef
if (descriptionElement !== null) {
descriptionElement.focus()
}
}
}, [esOuve])
return (
2022-01-19 07:06:26 +04:00
<Root>
2022-12-12 23:17:30 +04:00
<Badge badgeContent={commentaires.length} color='primary'>
<Button sx={{marginBlock: 3}} variant='outlined' onClick={handleClick}>
Voir les commentaires
</Button>
</Badge>
2022-01-19 07:06:26 +04:00
<Dialog
open={esOuve}
scroll='paper'
aria-labelledby='scroll-dialog-title'
aria-describedby='scroll-dialog-description'
onClose={handleClose}
>
2022-02-05 14:36:49 +04:00
<DialogTitle align='center' id='scroll-dialog-title'>Commentaires</DialogTitle>
2022-01-19 07:06:26 +04:00
<DialogContent dividers>
<Typography
ref={descriptionElementRef}
variant='inherit'
id='scroll-dialog-description'
tabIndex={-1}
2022-03-18 08:10:37 +04:00
component='div'
2022-01-19 07:06:26 +04:00
>
2022-05-20 02:19:10 +04:00
{commentaires.length > 0 ? (
<KomanteList commentaires={commentaires} />
2022-01-19 07:06:26 +04:00
) : (
2022-03-18 08:10:37 +04:00
<Typography component='div' align='center'>
2022-02-07 16:26:10 +04:00
Aucun commentaire
2022-01-19 07:06:26 +04:00
</Typography>
2022-01-19 06:35:04 +04:00
)}
2022-01-19 07:06:26 +04:00
</Typography>
</DialogContent>
<DialogActions align='center' >
{session && session.user && !esKomenteOuve && (
<Button fullWidth color='primary' onClick={() => meteEsKomanteOuve(true)}>
Ajouter un commentaire
2021-06-26 12:27:48 +02:00
</Button>
)}
2022-01-19 07:06:26 +04:00
{!session && !esKoneksyonOuve && (
<Button fullWidth color='primary' onClick={() => meteEsKoneksyonOuve(true)}>
Se connecter pour commenter
</Button>
)}
{!session && esKoneksyonOuve && (
<Koneksyon chimen={router.asPath} />
2021-06-26 12:27:48 +02:00
)}
2022-01-19 07:06:26 +04:00
</DialogActions>
{!session && esKoneksyonOuve && (
2022-02-07 16:26:10 +04:00
<Button style={{marginBlock: 10}} color='secondary' onClick={() => meteEsKoneksyonOuve(false)}>
<strong>Annuler</strong>
2022-01-19 07:06:26 +04:00
</Button>
)}
{session && session.user && esKomenteOuve && (
<>
2022-05-20 02:19:10 +04:00
<EkriKomante session={session} parole={parole} paroleId={paroleId} meteEsKomanteOuve={meteEsKomanteOuve} />
2022-01-19 07:06:26 +04:00
<Button fullWidth style={{marginBottom: 10}} color='primary' onClick={() => meteEsKomanteOuve(false)}>
Fermer
</Button>
</>
)}
</Dialog>
</Root>
2021-06-26 12:27:48 +02:00
)
}
VweKomante.defaultProps = {
2022-05-20 02:19:10 +04:00
commentaires: null
2021-06-26 12:27:48 +02:00
}
VweKomante.propTypes = {
2022-05-20 02:19:10 +04:00
commentaires: PropTypes.array,
parole: PropTypes.object.isRequired,
paroleId: PropTypes.number.isRequired
2021-06-26 12:27:48 +02:00
}