Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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