Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4818 | Rev 4822 | 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 */
4100 stevensc 2
import React, { useEffect, useRef, useState } from 'react'
4050 stevensc 3
import { axios } from '../../../../utils'
3943 stevensc 4
import { useForm } from 'react-hook-form'
5
import { connect } from 'react-redux'
6
import { addNotification } from '../../../../redux/notification/notification.actions'
3947 stevensc 7
import ConfirmModal from '../../../../shared/confirm-modal/ConfirmModal'
3943 stevensc 8
import FormErrorFeedback from '../../../../shared/form-error-feedback/FormErrorFeedback'
9
 
10
const FeedCommentSection = ({
3962 stevensc 11
    isShow = true,
3943 stevensc 12
    image = '',
13
    addUrl = '',
14
    updateTotalComments = function () { },
3962 stevensc 15
    comments = [],
3943 stevensc 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 (
3962 stevensc 41
        <div className={`comments-container ${isShow ? 'show' : 'hidden'}`}>
3943 stevensc 42
            <form
3964 stevensc 43
                className='feedCommentContainer'
3943 stevensc 44
                onSubmit={handleSubmit(submitCommentHandler)}
45
            >
4820 stevensc 46
                {image && <img src={image} alt="User profile image" />}
3964 stevensc 47
                <input
48
                    className='commentInput'
49
                    type="text"
50
                    name="comment"
51
                    maxLength="256"
52
                    placeholder="Escribe un comentario"
53
                    ref={register({ required: "El campo es requerido" })}
54
                />
55
                <button className='shareIconContainer iconActive' >
4061 stevensc 56
                    <img src='/images/icons/send.png' className='fa img-icon' />
3964 stevensc 57
                </button>
3943 stevensc 58
            </form>
59
            {errors.comment && <FormErrorFeedback>{errors.comment.message}</FormErrorFeedback>}
60
            <FeedCommentSection.CommentsList
61
                comments={commentsState}
62
                updateTotalComments={updateTotalComments}
63
                setComments={setCommentsState}
64
            />
3962 stevensc 65
        </div>
3943 stevensc 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 (
3953 stevensc 88
        <ul className='comment-list'>
89
            {comments.reverse().map((comment) => {
90
                return (
91
                    <FeedCommentSection.CommentTemplate
92
                        commentData={comment}
93
                        onDeleteHandler={deleteCommentHandler}
94
                        key={comment.unique}
95
                    />
96
                );
97
            })}
98
        </ul>
3943 stevensc 99
    )
100
}
101
 
3947 stevensc 102
const CommentTemplate = ({ onDeleteHandler, commentData }) => {
103
 
104
    const {
105
        user_name,
106
        user_url,
107
        user_image,
108
        link_delete,
109
        time_elapsed,
110
        comment,
111
        unique,
112
    } = commentData;
113
 
114
    const [showConfirmModal, setShowConfirmModal] = useState(false);
4100 stevensc 115
    const [displayOption, setDisplayOption] = useState(false)
116
    const deleteButton = useRef();
3947 stevensc 117
 
118
    const handleShowConfirmModal = () => setShowConfirmModal(!showConfirmModal)
119
 
120
    const handleModalAccept = () => onDeleteHandler(unique, link_delete)
121
 
4100 stevensc 122
    useEffect(() => {
123
        const handleClickOutside = (event) => {
124
            if (deleteButton.current && !deleteButton.current.contains(event.target)) {
125
                setDisplayOption(false)
126
            }
127
        }
128
        document.addEventListener("mousedown", handleClickOutside);
129
 
130
        return () => {
131
            document.removeEventListener("mousedown", handleClickOutside);
132
        };
133
    }, [deleteButton]);
134
 
3947 stevensc 135
    return (
3954 stevensc 136
        <li>
137
            <div className="comment-container">
4818 stevensc 138
                {user_image &&
139
                    <img
140
                        src={user_image}
141
                        alt="user-image"
142
                        className='user-image'
143
                    />}
3954 stevensc 144
                <div className='comment-content'>
145
                    <div className='info'>
146
                        <a href={user_url}>
147
                            <h3>{user_name}</h3>
148
                        </a>
149
                        <span>
150
                            {time_elapsed}
151
                            {link_delete &&
4100 stevensc 152
                                <>
153
                                    <img
154
                                        src='/images/icons/options.png'
4106 stevensc 155
                                        className='cursor-pointer img-icon options-sm'
4100 stevensc 156
                                        onClick={() => setDisplayOption(!displayOption)}
157
                                    />
4107 stevensc 158
                                    <div className={`comments-options ${displayOption ? 'active' : ''}`}>
4100 stevensc 159
                                        <ul>
160
                                            <li>
161
                                                <button
162
                                                    className="option-btn"
163
                                                    onClick={handleShowConfirmModal}
164
                                                    ref={deleteButton}
165
                                                >
166
                                                    <i className="fa fa-trash-o mr-1" />
167
                                                    Borrar
168
                                                </button>
169
                                            </li>
170
                                        </ul>
171
                                    </div>
172
                                </>
3954 stevensc 173
                            }
174
                        </span>
3947 stevensc 175
                    </div>
3954 stevensc 176
                    <p>{comment}</p>
3947 stevensc 177
                </div>
3954 stevensc 178
            </div>
3947 stevensc 179
            <ConfirmModal
180
                show={showConfirmModal}
181
                onClose={() => setShowConfirmModal(false)}
182
                onAccept={handleModalAccept}
183
                acceptLabel="Aceptar"
184
            />
3954 stevensc 185
        </li >
3947 stevensc 186
    );
187
};
188
 
189
 
3943 stevensc 190
FeedCommentSection.CommentsList = CommentsList
3947 stevensc 191
FeedCommentSection.CommentTemplate = CommentTemplate
3943 stevensc 192
 
193
const mapDispatchToProps = {
194
    addNotification: (notification) => addNotification(notification),
195
};
196
 
197
export default connect(null, mapDispatchToProps)(FeedCommentSection)