From e76a7cf8cc23cfa8e022e1906d12045e01f189a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20FAMIBELLE-PRONZOLA?= Date: Fri, 20 May 2022 02:19:10 +0400 Subject: [PATCH] Change komante to commentaire --- components/komante/ekri-komante.js | 56 +++++++++++++++++------------- components/komante/komante-list.js | 14 ++++---- components/komante/vwe-komante.js | 17 ++++----- 3 files changed, 47 insertions(+), 40 deletions(-) diff --git a/components/komante/ekri-komante.js b/components/komante/ekri-komante.js index 6fd045e..34d8338 100644 --- a/components/komante/ekri-komante.js +++ b/components/komante/ekri-komante.js @@ -11,26 +11,25 @@ import { } from '@mui/material' 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) { return }) -function EkriKomante({session, teks, meteEsKomanteOuve}) { +function EkriKomante({session, paroleId, meteEsKomanteOuve}) { const {jwt, user} = session - const {id} = teks const [komante, meteKomante] = useState({kontni: ''}) const [ere, meteEre] = useState('') const [sikse, meteSikse] = useState('') const [chaje, meteChaje] = useState(false) const [esOuve, meteEsOuve] = useState(false) - const handleClick = () => { + const handleClick = async () => { meteChaje(true) const {kontni} = komante if (kontni === '') { - meteEre({mesaj: 'Champ obligatoire'}) + meteEre({error: {message: 'Champ obligatoire'}}) meteChaje(false) return @@ -41,21 +40,28 @@ function EkriKomante({session, teks, meteEsKomanteOuve}) { Authorization: `Bearer ${jwt}` } - axios.post(`${API_URL}/komante`, { - kontni, - teks: id, - user: user.id - }, { - headers - }) - .then(() => { - meteSikse('Commentaire envoyé avec succès. Il apparaîtra sur le site après validation.') - meteChaje(false) - }) - .catch(error => { - meteEre(error) - meteChaje(false) + try { + await axios.post(`${API_URL}/commentaires`, { + data: { + contenu: kontni, + parole: paroleId, + user: { + id: user.id, + username: user.username, + email: user.email + }, + datePublication: new Date() + } + }, { + headers }) + + meteSikse('Commentaire envoyé avec succès. Il apparaîtra sur le site après validation.') + meteChaje(false) + } catch (error) { + meteEre(error?.response?.data) + meteChaje(false) + } } const handleUpdate = useCallback(update => { @@ -97,10 +103,10 @@ function EkriKomante({session, teks, meteEsKomanteOuve}) { fullWidth multiline required - helperText='Votre commentaire (obligatoire)' + helperText='*Obligatoire. 500 caractères maximum' style={{marginTop: '1em'}} id='komante' - label='Kòmantè' + label='Commentaire' value={komante.kontni} rows={8} variant='outlined' @@ -123,16 +129,16 @@ function EkriKomante({session, teks, meteEsKomanteOuve}) { {chaje && } {sikse && ( - + {sikse} )} {ere && ( - + - Une erreur s’est produite : {ere.message} + Une erreur s’est produite : {ere?.error?.message} )} @@ -142,7 +148,7 @@ function EkriKomante({session, teks, meteEsKomanteOuve}) { EkriKomante.propTypes = { session: PropTypes.object.isRequired, - teks: PropTypes.object.isRequired, + paroleId: PropTypes.number.isRequired, meteEsKomanteOuve: PropTypes.func.isRequired } diff --git a/components/komante/komante-list.js b/components/komante/komante-list.js index 4935647..c70b7dc 100644 --- a/components/komante/komante-list.js +++ b/components/komante/komante-list.js @@ -35,23 +35,23 @@ const StyledList = styled(List)(( } })) -export default function KomanteList({komante}) { +export default function KomanteList({commentaires}) { return ( - {komante.map(({id, username, kontni, sentAt}) => ( + {commentaires.map(({id, attributes}) => (
- {username} + + {attributes.user.data.attributes.username} } /> - {format(new Date(sentAt), 'Pp', {locale: fr})} + {format(new Date(attributes.datePublication), 'Pp', {locale: fr})} } /> @@ -63,5 +63,5 @@ export default function KomanteList({komante}) { } KomanteList.propTypes = { - komante: PropTypes.array.isRequired + commentaires: PropTypes.array.isRequired } diff --git a/components/komante/vwe-komante.js b/components/komante/vwe-komante.js index a4e3989..bc4f3d7 100644 --- a/components/komante/vwe-komante.js +++ b/components/komante/vwe-komante.js @@ -45,7 +45,7 @@ const Root = styled('div')(( const KomanteTooltip = Tooltip -export default function VweKomante({komante, teks}) { +export default function VweKomante({commentaires, parole, paroleId}) { const [esOuve, meteEsOuve] = useState(false) const [esKoneksyonOuve, meteEsKoneksyonOuve] = useState(false) const [esKomenteOuve, meteEsKomanteOuve] = useState(false) @@ -88,7 +88,7 @@ export default function VweKomante({komante, teks}) { size='large' onClick={handleClick} > - + @@ -109,8 +109,8 @@ export default function VweKomante({komante, teks}) { tabIndex={-1} component='div' > - {komante.length > 0 ? ( - + {commentaires.length > 0 ? ( + ) : ( Aucun commentaire @@ -140,7 +140,7 @@ export default function VweKomante({komante, teks}) { )} {session && session.user && esKomenteOuve && ( <> - + @@ -152,10 +152,11 @@ export default function VweKomante({komante, teks}) { } VweKomante.defaultProps = { - komante: null + commentaires: null } VweKomante.propTypes = { - komante: PropTypes.array, - teks: PropTypes.object.isRequired + commentaires: PropTypes.array, + parole: PropTypes.object.isRequired, + paroleId: PropTypes.number.isRequired }