Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3952 | Rev 3954 | 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'
3947 stevensc 8
import ConfirmModal from '../../../../shared/confirm-modal/ConfirmModal'
3943 stevensc 9
import FormErrorFeedback from '../../../../shared/form-error-feedback/FormErrorFeedback'
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();
3947 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 (
3953 stevensc 90
        <ul className='comment-list'>
91
            {comments.reverse().map((comment) => {
92
                return (
93
                    <FeedCommentSection.CommentTemplate
94
                        commentData={comment}
95
                        onDeleteHandler={deleteCommentHandler}
96
                        key={comment.unique}
97
                    />
98
                );
99
            })}
100
        </ul>
3943 stevensc 101
    )
102
}
103
 
3947 stevensc 104
const CommentTemplate = ({ onDeleteHandler, commentData }) => {
105
 
106
    const {
107
        user_name,
108
        user_url,
109
        user_image,
110
        link_delete,
111
        time_elapsed,
112
        comment,
113
        unique,
114
    } = commentData;
115
 
116
    const [showConfirmModal, setShowConfirmModal] = useState(false);
117
 
118
    const handleShowConfirmModal = () => setShowConfirmModal(!showConfirmModal)
119
 
120
    const handleModalAccept = () => onDeleteHandler(unique, link_delete)
121
 
122
    return (
123
        <>
124
            <li>
3950 stevensc 125
                <div className="comment-container">
126
                    <img
127
                        src={user_image}
128
                        alt="user-image"
129
                        className='user-image'
130
                    />
131
                    <div className='comment-content'>
132
                        <div className='info'>
133
                            <a href={user_url}>
134
                                <h3>{user_name}</h3>
135
                            </a>
136
                            <span>
137
                                <img
138
                                    src="/images/clock.png"
139
                                    alt="Clock"
140
                                    className='mr-2'
141
                                />
142
                                {time_elapsed}
143
                                {link_delete &&
144
                                    <button
145
                                        className="btn-comment-trash"
146
                                        onClick={handleShowConfirmModal}
147
                                    >
148
                                        <i className="fa fa-trash"></i>
149
                                    </button>
150
                                }
151
                            </span>
3947 stevensc 152
                        </div>
3952 stevensc 153
                        <p>{comment}</p>
3947 stevensc 154
                    </div>
155
                </div>
3950 stevensc 156
            </li >
3947 stevensc 157
            <ConfirmModal
158
                show={showConfirmModal}
159
                onClose={() => setShowConfirmModal(false)}
160
                onAccept={handleModalAccept}
161
                acceptLabel="Aceptar"
162
            />
163
        </>
164
    );
165
};
166
 
167
 
3943 stevensc 168
FeedCommentSection.CommentsList = CommentsList
3947 stevensc 169
FeedCommentSection.CommentTemplate = CommentTemplate
3943 stevensc 170
 
171
const mapDispatchToProps = {
172
    addNotification: (notification) => addNotification(notification),
173
};
174
 
175
export default connect(null, mapDispatchToProps)(FeedCommentSection)