Change komante to commentaire
This commit is contained in:
@@ -11,26 +11,25 @@ import {
|
|||||||
} from '@mui/material'
|
} from '@mui/material'
|
||||||
import MuiAlert from '@mui/material/Alert'
|
import MuiAlert from '@mui/material/Alert'
|
||||||
|
|
||||||
const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:1337'
|
const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:1337/api'
|
||||||
|
|
||||||
const Alert = forwardRef(function Alert(props, ref) {
|
const Alert = forwardRef(function Alert(props, ref) {
|
||||||
return <MuiAlert ref={ref} elevation={6} variant='filled' {...props} />
|
return <MuiAlert ref={ref} elevation={6} variant='filled' {...props} />
|
||||||
})
|
})
|
||||||
function EkriKomante({session, teks, meteEsKomanteOuve}) {
|
function EkriKomante({session, paroleId, meteEsKomanteOuve}) {
|
||||||
const {jwt, user} = session
|
const {jwt, user} = session
|
||||||
const {id} = teks
|
|
||||||
const [komante, meteKomante] = useState({kontni: ''})
|
const [komante, meteKomante] = useState({kontni: ''})
|
||||||
const [ere, meteEre] = useState('')
|
const [ere, meteEre] = useState('')
|
||||||
const [sikse, meteSikse] = useState('')
|
const [sikse, meteSikse] = useState('')
|
||||||
const [chaje, meteChaje] = useState(false)
|
const [chaje, meteChaje] = useState(false)
|
||||||
const [esOuve, meteEsOuve] = useState(false)
|
const [esOuve, meteEsOuve] = useState(false)
|
||||||
|
|
||||||
const handleClick = () => {
|
const handleClick = async () => {
|
||||||
meteChaje(true)
|
meteChaje(true)
|
||||||
const {kontni} = komante
|
const {kontni} = komante
|
||||||
|
|
||||||
if (kontni === '') {
|
if (kontni === '') {
|
||||||
meteEre({mesaj: 'Champ obligatoire'})
|
meteEre({error: {message: 'Champ obligatoire'}})
|
||||||
meteChaje(false)
|
meteChaje(false)
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -41,21 +40,28 @@ function EkriKomante({session, teks, meteEsKomanteOuve}) {
|
|||||||
Authorization: `Bearer ${jwt}`
|
Authorization: `Bearer ${jwt}`
|
||||||
}
|
}
|
||||||
|
|
||||||
axios.post(`${API_URL}/komante`, {
|
try {
|
||||||
kontni,
|
await axios.post(`${API_URL}/commentaires`, {
|
||||||
teks: id,
|
data: {
|
||||||
user: user.id
|
contenu: kontni,
|
||||||
|
parole: paroleId,
|
||||||
|
user: {
|
||||||
|
id: user.id,
|
||||||
|
username: user.username,
|
||||||
|
email: user.email
|
||||||
|
},
|
||||||
|
datePublication: new Date()
|
||||||
|
}
|
||||||
}, {
|
}, {
|
||||||
headers
|
headers
|
||||||
})
|
})
|
||||||
.then(() => {
|
|
||||||
meteSikse('Commentaire envoyé avec succès. Il apparaîtra sur le site après validation.')
|
meteSikse('Commentaire envoyé avec succès. Il apparaîtra sur le site après validation.')
|
||||||
meteChaje(false)
|
meteChaje(false)
|
||||||
})
|
} catch (error) {
|
||||||
.catch(error => {
|
meteEre(error?.response?.data)
|
||||||
meteEre(error)
|
|
||||||
meteChaje(false)
|
meteChaje(false)
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleUpdate = useCallback(update => {
|
const handleUpdate = useCallback(update => {
|
||||||
@@ -97,10 +103,10 @@ function EkriKomante({session, teks, meteEsKomanteOuve}) {
|
|||||||
fullWidth
|
fullWidth
|
||||||
multiline
|
multiline
|
||||||
required
|
required
|
||||||
helperText='Votre commentaire (obligatoire)'
|
helperText='*Obligatoire. 500 caractères maximum'
|
||||||
style={{marginTop: '1em'}}
|
style={{marginTop: '1em'}}
|
||||||
id='komante'
|
id='komante'
|
||||||
label='Kòmantè'
|
label='Commentaire'
|
||||||
value={komante.kontni}
|
value={komante.kontni}
|
||||||
rows={8}
|
rows={8}
|
||||||
variant='outlined'
|
variant='outlined'
|
||||||
@@ -123,16 +129,16 @@ function EkriKomante({session, teks, meteEsKomanteOuve}) {
|
|||||||
{chaje && <LinearProgress size={24} style={{width: '100%', marginBlock: '1em'}} />}
|
{chaje && <LinearProgress size={24} style={{width: '100%', marginBlock: '1em'}} />}
|
||||||
</div>
|
</div>
|
||||||
{sikse && (
|
{sikse && (
|
||||||
<Snackbar open={esOuve} autoHideDuration={3000} onClose={handleClose}>
|
<Snackbar open={esOuve} autoHideDuration={10_000} onClose={handleClose}>
|
||||||
<Alert severity='success' onClose={handleClose}>
|
<Alert severity='success' onClose={handleClose}>
|
||||||
<strong>{sikse}</strong>
|
<strong>{sikse}</strong>
|
||||||
</Alert>
|
</Alert>
|
||||||
</Snackbar>
|
</Snackbar>
|
||||||
)}
|
)}
|
||||||
{ere && (
|
{ere && (
|
||||||
<Snackbar open={esOuve} autoHideDuration={3000} onClose={handleClose}>
|
<Snackbar open={esOuve} autoHideDuration={10_000} onClose={handleClose}>
|
||||||
<Alert severity='error' onClose={handleClose}>
|
<Alert severity='error' onClose={handleClose}>
|
||||||
<strong>Une erreur s’est produite</strong> : <i>{ere.message}</i>
|
<strong>Une erreur s’est produite</strong> : <i>{ere?.error?.message}</i>
|
||||||
</Alert>
|
</Alert>
|
||||||
</Snackbar>
|
</Snackbar>
|
||||||
)}
|
)}
|
||||||
@@ -142,7 +148,7 @@ function EkriKomante({session, teks, meteEsKomanteOuve}) {
|
|||||||
|
|
||||||
EkriKomante.propTypes = {
|
EkriKomante.propTypes = {
|
||||||
session: PropTypes.object.isRequired,
|
session: PropTypes.object.isRequired,
|
||||||
teks: PropTypes.object.isRequired,
|
paroleId: PropTypes.number.isRequired,
|
||||||
meteEsKomanteOuve: PropTypes.func.isRequired
|
meteEsKomanteOuve: PropTypes.func.isRequired
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,23 +35,23 @@ const StyledList = styled(List)((
|
|||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
export default function KomanteList({komante}) {
|
export default function KomanteList({commentaires}) {
|
||||||
return (
|
return (
|
||||||
<StyledList className={classes.root}>
|
<StyledList className={classes.root}>
|
||||||
{komante.map(({id, username, kontni, sentAt}) => (
|
{commentaires.map(({id, attributes}) => (
|
||||||
<div key={id}>
|
<div key={id}>
|
||||||
<ListItemText
|
<ListItemText
|
||||||
primary={
|
primary={
|
||||||
<Typography gutterBottom style={{fontWeight: 'bold'}} variant='body1'>
|
<Typography gutterBottom style={{textDecoration: 'underline'}} variant='body1'>
|
||||||
{username}
|
<small>{attributes.user.data.attributes.username}</small>
|
||||||
</Typography>
|
</Typography>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<ListItemText
|
<ListItemText
|
||||||
primary={formatJsonString(kontni)}
|
primary={formatJsonString(attributes.contenu)}
|
||||||
secondary={
|
secondary={
|
||||||
<Typography gutterBottom style={{marginBlock: 5, fontStyle: 'italic'}} variant='caption' display='block'>
|
<Typography gutterBottom style={{marginBlock: 5, fontStyle: 'italic'}} variant='caption' display='block'>
|
||||||
{format(new Date(sentAt), 'Pp', {locale: fr})}
|
{format(new Date(attributes.datePublication), 'Pp', {locale: fr})}
|
||||||
</Typography>
|
</Typography>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
@@ -63,5 +63,5 @@ export default function KomanteList({komante}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
KomanteList.propTypes = {
|
KomanteList.propTypes = {
|
||||||
komante: PropTypes.array.isRequired
|
commentaires: PropTypes.array.isRequired
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ const Root = styled('div')((
|
|||||||
|
|
||||||
const KomanteTooltip = Tooltip
|
const KomanteTooltip = Tooltip
|
||||||
|
|
||||||
export default function VweKomante({komante, teks}) {
|
export default function VweKomante({commentaires, parole, paroleId}) {
|
||||||
const [esOuve, meteEsOuve] = useState(false)
|
const [esOuve, meteEsOuve] = useState(false)
|
||||||
const [esKoneksyonOuve, meteEsKoneksyonOuve] = useState(false)
|
const [esKoneksyonOuve, meteEsKoneksyonOuve] = useState(false)
|
||||||
const [esKomenteOuve, meteEsKomanteOuve] = useState(false)
|
const [esKomenteOuve, meteEsKomanteOuve] = useState(false)
|
||||||
@@ -88,7 +88,7 @@ export default function VweKomante({komante, teks}) {
|
|||||||
size='large'
|
size='large'
|
||||||
onClick={handleClick}
|
onClick={handleClick}
|
||||||
>
|
>
|
||||||
<Badge badgeContent={komante.length} color='primary'>
|
<Badge badgeContent={commentaires.length} color='primary'>
|
||||||
<CommentIcon />
|
<CommentIcon />
|
||||||
</Badge>
|
</Badge>
|
||||||
</IconButton>
|
</IconButton>
|
||||||
@@ -109,8 +109,8 @@ export default function VweKomante({komante, teks}) {
|
|||||||
tabIndex={-1}
|
tabIndex={-1}
|
||||||
component='div'
|
component='div'
|
||||||
>
|
>
|
||||||
{komante.length > 0 ? (
|
{commentaires.length > 0 ? (
|
||||||
<KomanteList komante={komante} />
|
<KomanteList commentaires={commentaires} />
|
||||||
) : (
|
) : (
|
||||||
<Typography component='div' align='center'>
|
<Typography component='div' align='center'>
|
||||||
Aucun commentaire
|
Aucun commentaire
|
||||||
@@ -140,7 +140,7 @@ export default function VweKomante({komante, teks}) {
|
|||||||
)}
|
)}
|
||||||
{session && session.user && esKomenteOuve && (
|
{session && session.user && esKomenteOuve && (
|
||||||
<>
|
<>
|
||||||
<EkriKomante session={session} teks={teks} meteEsKomanteOuve={meteEsKomanteOuve} />
|
<EkriKomante session={session} parole={parole} paroleId={paroleId} meteEsKomanteOuve={meteEsKomanteOuve} />
|
||||||
<Button fullWidth style={{marginBottom: 10}} color='primary' onClick={() => meteEsKomanteOuve(false)}>
|
<Button fullWidth style={{marginBottom: 10}} color='primary' onClick={() => meteEsKomanteOuve(false)}>
|
||||||
Fermer
|
Fermer
|
||||||
</Button>
|
</Button>
|
||||||
@@ -152,10 +152,11 @@ export default function VweKomante({komante, teks}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
VweKomante.defaultProps = {
|
VweKomante.defaultProps = {
|
||||||
komante: null
|
commentaires: null
|
||||||
}
|
}
|
||||||
|
|
||||||
VweKomante.propTypes = {
|
VweKomante.propTypes = {
|
||||||
komante: PropTypes.array,
|
commentaires: PropTypes.array,
|
||||||
teks: PropTypes.object.isRequired
|
parole: PropTypes.object.isRequired,
|
||||||
|
paroleId: PropTypes.number.isRequired
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user