Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3944 | Rev 3947 | Ir a la última revisión | | Comparar con el anterior | 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) => {
3944 stevensc 22
 
3945 stevensc 23
        const currentFormData = new FormData();
3944 stevensc 24
        Object.entries(data).forEach(([key, value])=> currentFormData.append(key, value))
3943 stevensc 25
 
26
        axios.post(addUrl, currentFormData)
27
            .then(({ data: response }) => {
28
                const { data: newComment, success, total_comments } = response;
29
 
30
                if (!success) {
31
                    return addNotification({ style: "danger", msg: data })
32
                }
33
 
34
                updateTotalComments(total_comments)
35
                setCommentsState([newComment, ...commentsState]);
36
                reset();
37
            })
38
    };
39
 
40
    return (
41
        <>
42
            <form
43
                className='form-comment-feed'
44
                onSubmit={handleSubmit(submitCommentHandler)}
45
            >
46
                <div className='feedCommentContainer'>
47
                    <img src={image} alt="User profile image" />
48
                    <input
49
                        className='commentInput'
50
                        type="text"
51
                        name="comment"
52
                        maxLength="256"
53
                        placeholder="Escribe un comentario"
54
                        ref={register({ required: "El campo es requerido" })}
55
                    />
56
                    <button className='shareIconContainer iconActive' >
57
                        <TbSend className='shareIcon' />
58
                    </button>
59
                </div>
60
            </form>
61
            {errors.comment && <FormErrorFeedback>{errors.comment.message}</FormErrorFeedback>}
62
            <FeedCommentSection.CommentsList
63
                comments={commentsState}
64
                updateTotalComments={updateTotalComments}
65
                setComments={setCommentsState}
66
            />
67
        </>
68
    )
69
}
70
 
71
const CommentsList = ({ comments, updateTotalComments, setComments }) => {
72
 
73
    const deleteCommentHandler = (commentUnique, deleteCommentUrl) => {
74
        axios.post(deleteCommentUrl)
75
            .then(({ data: response }) => {
76
                const { success, data, total_comments } = response
77
 
78
                if (!success) {
79
                    return addNotification({ style: "danger", msg: data })
80
                }
81
 
82
                updateTotalComments(total_comments)
83
                setComments(prevComments => prevComments.filter((comment) => comment.unique !== commentUnique))
84
                addNotification({ style: "success", msg: data });
85
            })
86
            .catch((error) => addNotification({ style: "danger", msg: error.message }))
87
    };
88
 
89
    return (
90
        <div className='commentSection'>
91
            <div className={`comment-sec comment-sec`}>
92
                <ul>
93
                    {comments.reverse().map((comment) => {
94
                        return (
95
                            <FeedCommentTemplate
96
                                commentData={comment}
97
                                onDeleteHandler={deleteCommentHandler}
98
                                key={comment.unique}
99
                            />
100
                        );
101
                    })}
102
                </ul>
103
            </div>
104
        </div>
105
    )
106
}
107
 
108
FeedCommentSection.CommentsList = CommentsList
109
 
110
const mapDispatchToProps = {
111
    addNotification: (notification) => addNotification(notification),
112
};
113
 
114
export default connect(null, mapDispatchToProps)(FeedCommentSection)