From 23779087a6bcc032c9bcbfc0a76133d547aacb33 Mon Sep 17 00:00:00 2001 From: Divlo Date: Tue, 5 May 2020 13:11:18 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20=C3=89dition=20d'un=20com?= =?UTF-8?q?mentaire=20(frontend)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FunctionPage/CommentCard/CommentCard.js | 66 ++++++++++++++++--- 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/website/components/FunctionPage/CommentCard/CommentCard.js b/website/components/FunctionPage/CommentCard/CommentCard.js index 113f0ed..5f76d84 100644 --- a/website/components/FunctionPage/CommentCard/CommentCard.js +++ b/website/components/FunctionPage/CommentCard/CommentCard.js @@ -1,6 +1,7 @@ import Link from 'next/link'; -import { forwardRef, useContext } from 'react'; +import { useEffect, useState, forwardRef, useContext, Fragment } from 'react'; import date from 'date-and-time'; +import htmlParser from 'html-react-parser'; import { UserContext } from '../../../contexts/UserContext'; import { API_URL } from '../../../utils/config/config'; import api from '../../../utils/api'; @@ -8,7 +9,14 @@ import './CommentCard.css'; const CommentCard = forwardRef((props, ref) => { - const { isAuth, user } = useContext(UserContext); + const { isAuth, user } = useContext(UserContext); + const [isEditing, setEditing] = useState(false); + const [editInput, setEditInput] = useState(""); + const [message, setMessage] = useState(""); + + useEffect(() => { + setEditInput(props.message); + }, []); const deleteCommentById = async () => { props.manageComment.setLoadingComments(true); @@ -24,6 +32,26 @@ const CommentCard = forwardRef((props, ref) => { props.manageComment.setLoadingComments(false); } + const handleChange = (event) => { + setEditInput(event.target.value); + } + + const handleSubmit = (event) => { + event.preventDefault(); + api.put(`/comments/${props.id}`, { message: editInput }, { headers: { 'Authorization': user.token } }) + .then((_response) => { + setEditing(false); + }) + .catch((error) => { + setMessage(`

Erreur: ${error.response.data.message}

`); + }); + } + + const editComment = () => { + setEditing(true); + setMessage(""); + } + return (
@@ -40,13 +68,33 @@ const CommentCard = forwardRef((props, ref) => {
-

- {props.message} -

- {(isAuth && user.name === props.user.name) && -

- supprimer le commentaire -

+ { + (!isEditing) ? + +

+ {editInput} +

+ {(isAuth && user.name === props.user.name) && +

+ supprimer +  -  + modifier +

+ } +
+ : +
+
+ + +
+
+ +
+
+ {htmlParser(message)} +
+
}