200 lines
6.4 KiB
JavaScript
200 lines
6.4 KiB
JavaScript
import Box from '@mui/material/Box'
|
|
import Typography from '@mui/material/Typography'
|
|
import Paper from '@mui/material/Paper'
|
|
import Grid from '@mui/material/Grid2'
|
|
import PropTypes from 'prop-types'
|
|
import IconButton from '@mui/material/IconButton'
|
|
import ThumbUpIcon from '@mui/icons-material/ThumbUp'
|
|
import ThumbDownIcon from '@mui/icons-material/ThumbDown'
|
|
import {useState, useEffect} from 'react'
|
|
import {useSession} from 'next-auth/react'
|
|
import Snackbar from '@mui/material/Snackbar'
|
|
import Alert from '@mui/material/Alert'
|
|
import {handleVote, getUserVote} from '../../lib/directus.js'
|
|
import {formatDate} from '@/lib/format.js'
|
|
|
|
export default function VersionComparison({versionData, versionCompare}) {
|
|
const {current, main, outdated} = versionCompare
|
|
const {data: session} = useSession()
|
|
const [snackbar, setSnackbar] = useState({open: false, message: '', severity: 'success'})
|
|
const [currentVote, setCurrentVote] = useState(null)
|
|
|
|
useEffect(() => {
|
|
const fetchVote = async () => {
|
|
if (session?.user && versionCompare.versionId) {
|
|
try {
|
|
const vote = await getUserVote({
|
|
accessToken: session.user.accessToken,
|
|
userId: session.user.userId,
|
|
versionId: versionCompare.versionId
|
|
})
|
|
setCurrentVote(vote)
|
|
} catch (error) {
|
|
console.error('Error fetching vote:', error)
|
|
}
|
|
}
|
|
}
|
|
|
|
fetchVote()
|
|
}, [session, versionCompare.versionId])
|
|
|
|
const handleVoteClick = async voteValue => {
|
|
try {
|
|
const newVoteValue = await handleVote({
|
|
accessToken: session.user.accessToken,
|
|
userId: session.user.userId,
|
|
versionId: versionCompare.versionId,
|
|
voteValue
|
|
})
|
|
setCurrentVote(newVoteValue)
|
|
setSnackbar({
|
|
open: true,
|
|
message: newVoteValue === null ? 'Vote annulé' : 'Vote enregistré avec succès',
|
|
severity: 'success'
|
|
})
|
|
} catch (error) {
|
|
setSnackbar({
|
|
open: true,
|
|
message: `Erreur lors du vote: ${error}`,
|
|
severity: 'error'
|
|
})
|
|
}
|
|
}
|
|
|
|
const handleCloseSnackbar = () => {
|
|
setSnackbar(prev => ({...prev, open: false}))
|
|
}
|
|
|
|
return (
|
|
<Box sx={{padding: 3}}>
|
|
<Grid container spacing={2}>
|
|
<Grid xs={6}>
|
|
<Paper sx={{padding: 1, backgroundColor: '#E8F5E9'}}>
|
|
<Typography variant='body1' sx={{color: '#388E3C', fontWeight: 'bold'}}>
|
|
{main.contenu}
|
|
</Typography>
|
|
</Paper>
|
|
</Grid>
|
|
<Grid xs={6}>
|
|
<Paper sx={{padding: 1, backgroundColor: outdated ? '#F9E8E8' : '#E3F2FD'}}>
|
|
<Typography variant='body1' sx={{color: outdated ? '#D32F2F' : '#1976D2', fontWeight: 'bold'}}>
|
|
{current.contenu}
|
|
</Typography>
|
|
{!outdated && session?.user && (
|
|
<>
|
|
<Box>
|
|
<Typography sx={{textDecoration: 'underline'}} variant='caption' color='primary'>@{versionData.user_created.split('-')[0]}</Typography>
|
|
</Box>
|
|
<Box sx={{
|
|
display: 'flex', alignItems: 'center', justifyContent: 'space-between', mt: 1
|
|
}}
|
|
>
|
|
<Box>
|
|
{versionData && (
|
|
<Typography sx={{fontWeight: 'bold'}} color='primary'>
|
|
{formatDate(versionData.date_created)}
|
|
</Typography>
|
|
)}
|
|
</Box>
|
|
<Box sx={{display: 'flex', justifyContent: 'flex-end'}}>
|
|
<IconButton
|
|
size='small'
|
|
color={currentVote === 1 ? 'success' : 'primary'}
|
|
aria-label='vote positif'
|
|
onClick={() => handleVoteClick(1)}
|
|
>
|
|
<ThumbUpIcon />
|
|
</IconButton>
|
|
<IconButton
|
|
size='small'
|
|
color={currentVote === -1 ? 'error' : 'primary'}
|
|
aria-label='vote négatif'
|
|
onClick={() => handleVoteClick(-1)}
|
|
>
|
|
<ThumbDownIcon />
|
|
</IconButton>
|
|
</Box>
|
|
</Box>
|
|
</>
|
|
)}
|
|
</Paper>
|
|
</Grid>
|
|
</Grid>
|
|
|
|
<Box sx={{marginTop: 4}}>
|
|
<Typography variant='button' sx={{
|
|
fontWeight: 'bold', marginBottom: 1, textAlign: 'center', display: 'block'
|
|
}}
|
|
>
|
|
LÉGENDE
|
|
</Typography>
|
|
<Grid container textAlign='center' spacing={2}>
|
|
<Grid size={{xs: 12, sm: 4}}>
|
|
<Typography
|
|
variant='body2'
|
|
sx={{
|
|
backgroundColor: '#E8F5E9',
|
|
padding: 1,
|
|
borderRadius: 1,
|
|
color: '#388E3C',
|
|
fontWeight: 'bold'
|
|
}}
|
|
>
|
|
En ligne
|
|
</Typography>
|
|
</Grid>
|
|
<Grid size={{xs: 12, sm: 4}}>
|
|
<Typography
|
|
variant='body2'
|
|
sx={{
|
|
backgroundColor: '#E3F2FD',
|
|
padding: 1,
|
|
borderRadius: 1,
|
|
color: '#1976D2',
|
|
fontWeight: 'bold'
|
|
}}
|
|
>
|
|
En cours de révision
|
|
</Typography>
|
|
</Grid>
|
|
<Grid size={{xs: 12, sm: 4}}>
|
|
<Typography
|
|
variant='body2'
|
|
sx={{
|
|
backgroundColor: '#F9E8E8',
|
|
padding: 1,
|
|
borderRadius: 1,
|
|
color: '#D32F2F',
|
|
fontWeight: 'bold'
|
|
}}
|
|
>
|
|
Remplacée
|
|
</Typography>
|
|
</Grid>
|
|
</Grid>
|
|
</Box>
|
|
<Snackbar
|
|
open={snackbar.open}
|
|
autoHideDuration={6000}
|
|
anchorOrigin={{vertical: 'bottom', horizontal: 'center'}}
|
|
onClose={handleCloseSnackbar}
|
|
>
|
|
<Alert variant='filled' severity={snackbar.severity} sx={{width: '100%'}} onClose={handleCloseSnackbar}>
|
|
{snackbar.message}
|
|
</Alert>
|
|
</Snackbar>
|
|
</Box>
|
|
)
|
|
}
|
|
|
|
VersionComparison.propTypes = {
|
|
versionData: PropTypes.object,
|
|
versionCompare: PropTypes.shape({
|
|
outdated: PropTypes.bool.isRequired,
|
|
mainHash: PropTypes.string.isRequired,
|
|
current: PropTypes.object.isRequired,
|
|
main: PropTypes.object.isRequired,
|
|
versionId: PropTypes.string
|
|
}).isRequired,
|
|
}
|