Rev 3944 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
/* eslint-disable react/prop-types */import axios from 'axios'import React, { useState } from 'react'import { useForm } from 'react-hook-form'import { TbSend } from 'react-icons/tb'import { connect } from 'react-redux'import { addNotification } from '../../../../redux/notification/notification.actions'import FormErrorFeedback from '../../../../shared/form-error-feedback/FormErrorFeedback'import FeedCommentTemplate from './FeedCommentTemplate'const FeedCommentSection = ({image = '',addUrl = '',updateTotalComments = function () { },comments = []}) => {const { register, handleSubmit, errors, reset } = useForm()const [commentsState, setCommentsState] = useState(comments);const submitCommentHandler = (data) => {const currentFormData = new FormData(data);axios.post(addUrl, currentFormData).then(({ data: response }) => {const { data: newComment, success, total_comments } = response;if (!success) {return addNotification({ style: "danger", msg: data })}updateTotalComments(total_comments)setCommentsState([newComment, ...commentsState]);reset();})};return (<><formclassName='form-comment-feed'onSubmit={handleSubmit(submitCommentHandler)}><div className='feedCommentContainer'><img src={image} alt="User profile image" /><inputclassName='commentInput'type="text"name="comment"maxLength="256"placeholder="Escribe un comentario"ref={register({ required: "El campo es requerido" })}/><button className='shareIconContainer iconActive' ><TbSend className='shareIcon' /></button></div></form>{errors.comment && <FormErrorFeedback>{errors.comment.message}</FormErrorFeedback>}<FeedCommentSection.CommentsListcomments={commentsState}updateTotalComments={updateTotalComments}setComments={setCommentsState}/></>)}const CommentsList = ({ comments, updateTotalComments, setComments }) => {const deleteCommentHandler = (commentUnique, deleteCommentUrl) => {axios.post(deleteCommentUrl).then(({ data: response }) => {const { success, data, total_comments } = responseif (!success) {return addNotification({ style: "danger", msg: data })}updateTotalComments(total_comments)setComments(prevComments => prevComments.filter((comment) => comment.unique !== commentUnique))addNotification({ style: "success", msg: data });}).catch((error) => addNotification({ style: "danger", msg: error.message }))};return (<div className='commentSection'><div className={`comment-sec comment-sec`}><ul>{comments.reverse().map((comment) => {return (<FeedCommentTemplatecommentData={comment}onDeleteHandler={deleteCommentHandler}key={comment.unique}/>);})}</ul></div></div>)}FeedCommentSection.CommentsList = CommentsListconst mapDispatchToProps = {addNotification: (notification) => addNotification(notification),};export default connect(null, mapDispatchToProps)(FeedCommentSection)