Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3944 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3943 stevensc 1
/* eslint-disable react/prop-types */
2
import axios from 'axios'
3
import React, { useState } from 'react'
4
import { useForm } from 'react-hook-form'
5
import { TbSend } from 'react-icons/tb'
6
import { connect } from 'react-redux'
7
import { addNotification } from '../../../../redux/notification/notification.actions'
8
import FormErrorFeedback from '../../../../shared/form-error-feedback/FormErrorFeedback'
9
import FeedCommentTemplate from './FeedCommentTemplate'
10
 
11
const FeedCommentSection = ({
12
    image = '',
13
    addUrl = '',
14
    updateTotalComments = function () { },
15
    comments = []
16
}) => {
17
 
18
    const { register, handleSubmit, errors, reset } = useForm()
19
    const [commentsState, setCommentsState] = useState(comments);
20
 
21
    const submitCommentHandler = (data) => {
22
        const currentFormData = new FormData(data);
23
 
24
        axios.post(addUrl, currentFormData)
25
            .then(({ data: response }) => {
26
                const { data: newComment, success, total_comments } = response;
27
 
28
                if (!success) {
29
                    return addNotification({ style: "danger", msg: data })
30
                }
31
 
32
                updateTotalComments(total_comments)
33
                setCommentsState([newComment, ...commentsState]);
34
                reset();
35
            })
36
    };
37
 
38
    return (
39
        <>
40
            <form
41
                className='form-comment-feed'
42
                onSubmit={handleSubmit(submitCommentHandler)}
43
            >
44
                <div className='feedCommentContainer'>
45
                    <img src={image} alt="User profile image" />
46
                    <input
47
                        className='commentInput'
48
                        type="text"
49
                        name="comment"
50
                        maxLength="256"
51
                        placeholder="Escribe un comentario"
52
                        ref={register({ required: "El campo es requerido" })}
53
                    />
54
                    <button className='shareIconContainer iconActive' >
55
                        <TbSend className='shareIcon' />
56
                    </button>
57
                </div>
58
            </form>
59
            {errors.comment && <FormErrorFeedback>{errors.comment.message}</FormErrorFeedback>}
60
            <FeedCommentSection.CommentsList
61
                comments={commentsState}
62
                updateTotalComments={updateTotalComments}
63
                setComments={setCommentsState}
64
            />
65
        </>
66
    )
67
}
68
 
69
const CommentsList = ({ comments, updateTotalComments, setComments }) => {
70
 
71
    const deleteCommentHandler = (commentUnique, deleteCommentUrl) => {
72
        axios.post(deleteCommentUrl)
73
            .then(({ data: response }) => {
74
                const { success, data, total_comments } = response
75
 
76
                if (!success) {
77
                    return addNotification({ style: "danger", msg: data })
78
                }
79
 
80
                updateTotalComments(total_comments)
81
                setComments(prevComments => prevComments.filter((comment) => comment.unique !== commentUnique))
82
                addNotification({ style: "success", msg: data });
83
            })
84
            .catch((error) => addNotification({ style: "danger", msg: error.message }))
85
    };
86
 
87
    return (
88
        <div className='commentSection'>
89
            <div className={`comment-sec comment-sec`}>
90
                <ul>
91
                    {comments.reverse().map((comment) => {
92
                        return (
93
                            <FeedCommentTemplate
94
                                commentData={comment}
95
                                onDeleteHandler={deleteCommentHandler}
96
                                key={comment.unique}
97
                            />
98
                        );
99
                    })}
100
                </ul>
101
            </div>
102
        </div>
103
    )
104
}
105
 
106
FeedCommentSection.CommentsList = CommentsList
107
 
108
const mapDispatchToProps = {
109
    addNotification: (notification) => addNotification(notification),
110
};
111
 
112
export default connect(null, mapDispatchToProps)(FeedCommentSection)