2024-06-18 11:11:15 +04:00
|
|
|
|
'use client'
|
|
|
|
|
|
|
|
|
|
|
|
import {useRef} from 'react'
|
|
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
|
|
import Button from '@mui/material/Button'
|
|
|
|
|
|
import TextField from '@mui/material/TextField'
|
|
|
|
|
|
import Dialog from '@mui/material/Dialog'
|
|
|
|
|
|
import DialogActions from '@mui/material/DialogActions'
|
|
|
|
|
|
import DialogContent from '@mui/material/DialogContent'
|
|
|
|
|
|
import DialogContentText from '@mui/material/DialogContentText'
|
|
|
|
|
|
import DialogTitle from '@mui/material/DialogTitle'
|
|
|
|
|
|
import {createItem, withToken} from '@directus/sdk'
|
|
|
|
|
|
import LogoutCountdown from '../session/logout-countdown.js'
|
|
|
|
|
|
import {directusClient} from '@/lib/directus.js'
|
|
|
|
|
|
|
|
|
|
|
|
function hasRestrictedChar(text) {
|
2024-06-19 09:07:09 +04:00
|
|
|
|
const regex = /[<>&"]/g
|
2024-06-18 11:11:15 +04:00
|
|
|
|
|
|
|
|
|
|
return Boolean(regex.test(text))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default function HandleComments({session, selectedTitre, isOpen, setIsOpen, setError, setSuccess, setIsErrorAlertOpen, setIsSuccessAlertOpen}) {
|
|
|
|
|
|
const countdownRef = useRef()
|
|
|
|
|
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
|
|
setIsOpen(false)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const handleFormSubmit = async e => {
|
|
|
|
|
|
e.preventDefault()
|
|
|
|
|
|
|
|
|
|
|
|
const formData = new FormData(e.currentTarget)
|
|
|
|
|
|
const formJson = Object.fromEntries(formData.entries())
|
|
|
|
|
|
const {comment} = formJson
|
2024-06-19 09:07:09 +04:00
|
|
|
|
const formattedComment = comment.replaceAll('\'', '’')
|
2024-06-18 11:11:15 +04:00
|
|
|
|
|
|
|
|
|
|
try {
|
2024-06-19 09:07:09 +04:00
|
|
|
|
if (hasRestrictedChar(formattedComment)) {
|
|
|
|
|
|
setError('Le texte ne doit pas contenir certains caractères spéciaux : <, >, ", .')
|
2024-06-18 11:11:15 +04:00
|
|
|
|
setIsErrorAlertOpen(true)
|
|
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await directusClient.request(withToken(
|
|
|
|
|
|
session.user.accessToken,
|
|
|
|
|
|
createItem('commentaires', {
|
2024-06-19 09:07:09 +04:00
|
|
|
|
text: formattedComment,
|
2024-06-18 11:11:15 +04:00
|
|
|
|
titre: selectedTitre,
|
|
|
|
|
|
status: 'draft'
|
|
|
|
|
|
})
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
setSuccess('Envoyé avec succès. Commentaire en attente de validation.')
|
|
|
|
|
|
setIsSuccessAlertOpen(true)
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
if (error?.errors[0]?.message === 'Token expired.') {
|
|
|
|
|
|
countdownRef.current.startCountdown()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.log(error?.errors[0]?.message)
|
|
|
|
|
|
setError(error?.errors[0]?.message)
|
|
|
|
|
|
setIsErrorAlertOpen(true)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
handleClose()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<Dialog
|
|
|
|
|
|
open={isOpen}
|
|
|
|
|
|
PaperProps={{
|
|
|
|
|
|
component: 'form',
|
|
|
|
|
|
onSubmit(event) {
|
|
|
|
|
|
handleFormSubmit(event)
|
|
|
|
|
|
},
|
|
|
|
|
|
}}
|
|
|
|
|
|
onClose={handleClose}
|
|
|
|
|
|
>
|
|
|
|
|
|
<DialogTitle>{selectedTitre.titre}</DialogTitle>
|
|
|
|
|
|
<DialogContent sx={{color: 'white'}}>
|
|
|
|
|
|
<DialogContentText sx={{color: 'white'}}>
|
|
|
|
|
|
Écrivez votre commentaire
|
|
|
|
|
|
</DialogContentText>
|
|
|
|
|
|
<TextField
|
|
|
|
|
|
autoFocus
|
|
|
|
|
|
required
|
|
|
|
|
|
multiline
|
|
|
|
|
|
fullWidth
|
|
|
|
|
|
mt={2}
|
|
|
|
|
|
rows={4}
|
|
|
|
|
|
id='comment'
|
|
|
|
|
|
name='comment'
|
|
|
|
|
|
label='Commentaire'
|
|
|
|
|
|
/>
|
|
|
|
|
|
</DialogContent>
|
|
|
|
|
|
<DialogActions>
|
|
|
|
|
|
<Button variant='contained' color='success' onClick={handleClose}>Annuler</Button>
|
|
|
|
|
|
<Button variant='contained' color='error' type='submit'>Valider</Button>
|
|
|
|
|
|
</DialogActions>
|
|
|
|
|
|
</Dialog>
|
|
|
|
|
|
<LogoutCountdown ref={countdownRef} setError={setError} setIsErrorAlertOpen={setIsErrorAlertOpen} />
|
|
|
|
|
|
</>
|
|
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HandleComments.propTypes = {
|
|
|
|
|
|
session: PropTypes.object,
|
|
|
|
|
|
selectedTitre: PropTypes.object.isRequired,
|
|
|
|
|
|
isOpen: PropTypes.bool.isRequired,
|
|
|
|
|
|
setIsOpen: PropTypes.func.isRequired,
|
|
|
|
|
|
setError: PropTypes.func.isRequired,
|
|
|
|
|
|
setSuccess: PropTypes.func.isRequired,
|
|
|
|
|
|
setIsErrorAlertOpen: PropTypes.func.isRequired,
|
|
|
|
|
|
setIsSuccessAlertOpen: PropTypes.func.isRequired
|
|
|
|
|
|
}
|