Rev 4050 | Rev 4100 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
/* eslint-disable react/prop-types */import React, { useState } from 'react'import { axios } from '../../../../utils'import { useForm } from 'react-hook-form'import { connect } from 'react-redux'import { addNotification } from '../../../../redux/notification/notification.actions'import ConfirmModal from '../../../../shared/confirm-modal/ConfirmModal'import FormErrorFeedback from '../../../../shared/form-error-feedback/FormErrorFeedback'const FeedCommentSection = ({isShow = true,image = '',addUrl = '',updateTotalComments = function () { },comments = [],}) => {const { register, handleSubmit, errors, reset } = useForm()const [commentsState, setCommentsState] = useState(comments);const submitCommentHandler = (data) => {const currentFormData = new FormData();Object.entries(data).forEach(([key, value]) => currentFormData.append(key, value))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 (<div className={`comments-container ${isShow ? 'show' : 'hidden'}`}><formclassName='feedCommentContainer'onSubmit={handleSubmit(submitCommentHandler)}><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' ><img src='images/icons/send.png' className='fa img-icon' /></button></form>{errors.comment && <FormErrorFeedback>{errors.comment.message}</FormErrorFeedback>}<FeedCommentSection.CommentsListcomments={commentsState}updateTotalComments={updateTotalComments}setComments={setCommentsState}/></div>)}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 (<ul className='comment-list'>{comments.reverse().map((comment) => {return (<FeedCommentSection.CommentTemplatecommentData={comment}onDeleteHandler={deleteCommentHandler}key={comment.unique}/>);})}</ul>)}const CommentTemplate = ({ onDeleteHandler, commentData }) => {const {user_name,user_url,user_image,link_delete,time_elapsed,comment,unique,} = commentData;const [showConfirmModal, setShowConfirmModal] = useState(false);const handleShowConfirmModal = () => setShowConfirmModal(!showConfirmModal)const handleModalAccept = () => onDeleteHandler(unique, link_delete)return (<li><div className="comment-container"><imgsrc={user_image}alt="user-image"className='user-image'/><div className='comment-content'><div className='info'><a href={user_url}><h3>{user_name}</h3></a><span><imgsrc="/images/clock.png"alt="Clock"className='mr-2'/>{time_elapsed}{link_delete &&<buttonclassName="btn-comment-trash"onClick={handleShowConfirmModal}><i className="fa fa-trash-o"></i></button>}</span></div><p>{comment}</p></div></div><ConfirmModalshow={showConfirmModal}onClose={() => setShowConfirmModal(false)}onAccept={handleModalAccept}acceptLabel="Aceptar"/></li >);};FeedCommentSection.CommentsList = CommentsListFeedCommentSection.CommentTemplate = CommentTemplateconst mapDispatchToProps = {addNotification: (notification) => addNotification(notification),};export default connect(null, mapDispatchToProps)(FeedCommentSection)