2024-05-20 14:45:43 +04:00
|
|
|
'use client'
|
|
|
|
|
|
2024-07-28 17:35:44 +02:00
|
|
|
import {useState} from 'react'
|
2024-05-20 14:45:43 +04:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
|
import {useRouter} from 'next/navigation'
|
|
|
|
|
import IconButton from '@mui/material/IconButton'
|
|
|
|
|
import {styled} from '@mui/material/styles'
|
|
|
|
|
import Tooltip, {tooltipClasses} from '@mui/material/Tooltip'
|
|
|
|
|
import ArticleIcon from '@mui/icons-material/Article'
|
|
|
|
|
import TitleIcon from '@mui/icons-material/Title'
|
2024-07-28 17:35:44 +02:00
|
|
|
import HandleEdit from './handle-edit.js'
|
|
|
|
|
import AuthAlert from '@/components/auth-form/auth-alert.js'
|
2024-05-20 14:45:43 +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-07-06 16:41:34 +02:00
|
|
|
export default function Edit({id, session, contenu, collection}) {
|
2024-05-20 14:45:43 +04:00
|
|
|
const router = useRouter()
|
2024-07-28 17:35:44 +02:00
|
|
|
const [isDialogOpen, setIsDialogOpen] = useState(false)
|
|
|
|
|
const [isErrorAlertOpen, setIsErrorAlertOpen] = useState(false)
|
|
|
|
|
const [isSuccessAlertOpen, setIsSuccessAlertOpen] = useState(false)
|
|
|
|
|
const [error, setError] = useState('')
|
|
|
|
|
const [success, setSuccess] = useState('')
|
2024-05-20 14:45:43 +04:00
|
|
|
|
|
|
|
|
const handleClick = () => {
|
2024-07-28 17:35:44 +02:00
|
|
|
setIsDialogOpen(true)
|
2024-05-20 14:45:43 +04:00
|
|
|
if (!session) {
|
|
|
|
|
router.push('/login')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2024-07-28 17:35:44 +02:00
|
|
|
<>
|
|
|
|
|
{error && <AuthAlert
|
|
|
|
|
isOpen={isErrorAlertOpen}
|
|
|
|
|
setIsOpen={setIsErrorAlertOpen}
|
|
|
|
|
message={error}
|
|
|
|
|
severity='error'
|
|
|
|
|
/>}
|
|
|
|
|
|
|
|
|
|
{success && <AuthAlert
|
|
|
|
|
isOpen={isSuccessAlertOpen}
|
|
|
|
|
setIsOpen={setIsSuccessAlertOpen}
|
|
|
|
|
message={success}
|
|
|
|
|
severity='success'
|
|
|
|
|
/>}
|
|
|
|
|
|
|
|
|
|
<IconButton size='large' aria-label='edit' onClick={handleClick}>
|
|
|
|
|
{collection === 'titres' && (
|
|
|
|
|
<LightTooltip title='Éditer un titre'>
|
|
|
|
|
<TitleIcon color='warning' fontSize='inherit' />
|
|
|
|
|
</LightTooltip>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{collection === 'articles' && (
|
|
|
|
|
<LightTooltip title='Éditer un article'>
|
|
|
|
|
<ArticleIcon color='warning' fontSize='inherit' />
|
|
|
|
|
</LightTooltip>
|
|
|
|
|
)}
|
|
|
|
|
</IconButton>
|
|
|
|
|
<HandleEdit
|
|
|
|
|
id={id}
|
|
|
|
|
session={session}
|
|
|
|
|
contenu={contenu}
|
|
|
|
|
collection={collection}
|
|
|
|
|
isOpen={isDialogOpen}
|
|
|
|
|
setIsOpen={setIsDialogOpen}
|
|
|
|
|
setError={setError}
|
|
|
|
|
setSuccess={setSuccess}
|
|
|
|
|
setIsErrorAlertOpen={setIsErrorAlertOpen}
|
|
|
|
|
setIsSuccessAlertOpen={setIsSuccessAlertOpen}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
2024-05-20 14:45:43 +04:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Edit.propTypes = {
|
2024-07-06 16:41:34 +02:00
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
|
session: PropTypes.object.isRequired,
|
|
|
|
|
contenu: PropTypes.string.isRequired,
|
|
|
|
|
collection: PropTypes.oneOf(['titres', 'articles']).isRequired
|
2024-05-20 14:45:43 +04:00
|
|
|
}
|