Rev 6357 | AutorÃa | Comparar con el anterior | Ultima modificación | Ver Log |
/* eslint-disable camelcase *//* eslint-disable react/prop-types */import React, { useEffect, useRef, 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'import useOutsideClick from '../../../../hooks/useOutsideClick'const FeedComments = ({isShow = true,image = '',addUrl = '',updateTotalComments = function () {},currentComments = [],addNotification,}) => {const { register, handleSubmit, errors, reset } = useForm()const [comments, setComments] = useState(currentComments)const submitCommet = (data) => {const currentFormData = new FormData()Object.entries(data).forEach(([key, value]) =>currentFormData.append(key, value))axios.post(addUrl, currentFormData).then(({ data: response }) => {const { success, data: message, total_comments } = responseif (!success) {addNotification({ style: 'danger', msg: message })return}updateTotalComments(total_comments)setComments((prevMessages) => [...prevMessages, message])reset()})}const deleteComment = (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 }))}useEffect(() => {setComments(currentComments)}, [currentComments])return (<div className={`comments-container ${isShow ? 'show' : 'hidden'}`}><formclassName="feedCommentContainer"onSubmit={handleSubmit(submitCommet)}>{image && <img src={image} alt="User profile image" />}<inputclassName="commentInput"type="text"name="comment"maxLength="256"placeholder={LABELS.WRITE_A_COMMENT}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>)}<FeedComments.List comments={comments} onDelete={deleteComment} /></div>)}const CommentsList = ({ comments, onDelete }) => {return (<ul className="comment-list">{comments.map((comment) => {return (<CommentsList.Itemcomment={comment}onDelete={onDelete}key={comment.unique}/>)})}</ul>)}const CommentTemplate = ({ onDelete, comment }) => {const {unique,user_url,user_name,user_image,time_elapsed,comment: content,link_delete,} = commentconst [showConfirmModal, setShowConfirmModal] = useState(false)const [displayOption, setDisplayOption] = useState(false)const deleteButton = useRef(null)const outsideClick = useOutsideClick(deleteButton)const toggleModal = () => setShowConfirmModal(!showConfirmModal)useEffect(() => {if (!outsideClick) returnsetDisplayOption(false)}, [outsideClick])return (<li><div className="comment-container">{user_image && (<img src={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>{time_elapsed}{comment.link_delete && (<><imgsrc="/images/icons/options.png"className="cursor-pointer img-icon options-sm"onClick={() => setDisplayOption(!displayOption)}/><divclassName={`comments-options ${displayOption ? 'active' : ''}`}><ul><li><buttonclassName="option-btn"onClick={toggleModal}ref={deleteButton}><i className="fa fa-trash-o mr-1" />{LABELS.DELETE}</button></li></ul></div></>)}</span></div><p>{content}</p></div></div><ConfirmModalshow={showConfirmModal}onClose={toggleModal}onAccept={() => onDelete(unique, link_delete)}acceptLabel={LABELS.ACCEPT}/></li>)}FeedComments.List = CommentsListCommentsList.Item = CommentTemplateconst mapDispatchToProps = {addNotification: (notification) => addNotification(notification),}export default connect(null, mapDispatchToProps)(FeedComments)