Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4820 | Rev 4892 | 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
 
4822 stevensc 21
    useEffect(() => {
22
      setCommentsState(comments)
23
    }, [comments])
24
 
25
 
3943 stevensc 26
    const submitCommentHandler = (data) => {
3944 stevensc 27
 
3945 stevensc 28
        const currentFormData = new FormData();
3947 stevensc 29
        Object.entries(data).forEach(([key, value]) => currentFormData.append(key, value))
3943 stevensc 30
 
31
        axios.post(addUrl, currentFormData)
32
            .then(({ data: response }) => {
33
                const { data: newComment, success, total_comments } = response;
34
 
35
                if (!success) {
36
                    return addNotification({ style: "danger", msg: data })
37
                }
38
 
39
                updateTotalComments(total_comments)
40
                setCommentsState([newComment, ...commentsState]);
41
                reset();
42
            })
43
    };
44
 
45
    return (
3962 stevensc 46
        <div className={`comments-container ${isShow ? 'show' : 'hidden'}`}>
3943 stevensc 47
            <form
3964 stevensc 48
                className='feedCommentContainer'
3943 stevensc 49
                onSubmit={handleSubmit(submitCommentHandler)}
50
            >
4820 stevensc 51
                {image && <img src={image} alt="User profile image" />}
3964 stevensc 52
                <input
53
                    className='commentInput'
54
                    type="text"
55
                    name="comment"
56
                    maxLength="256"
57
                    placeholder="Escribe un comentario"
58
                    ref={register({ required: "El campo es requerido" })}
59
                />
60
                <button className='shareIconContainer iconActive' >
4061 stevensc 61
                    <img src='/images/icons/send.png' className='fa img-icon' />
3964 stevensc 62
                </button>
3943 stevensc 63
            </form>
64
            {errors.comment && <FormErrorFeedback>{errors.comment.message}</FormErrorFeedback>}
65
            <FeedCommentSection.CommentsList
66
                comments={commentsState}
67
                updateTotalComments={updateTotalComments}
68
                setComments={setCommentsState}
69
            />
3962 stevensc 70
        </div>
3943 stevensc 71
    )
72
}
73
 
74
const CommentsList = ({ comments, updateTotalComments, setComments }) => {
75
 
76
    const deleteCommentHandler = (commentUnique, deleteCommentUrl) => {
77
        axios.post(deleteCommentUrl)
78
            .then(({ data: response }) => {
79
                const { success, data, total_comments } = response
80
 
81
                if (!success) {
82
                    return addNotification({ style: "danger", msg: data })
83
                }
84
 
85
                updateTotalComments(total_comments)
86
                setComments(prevComments => prevComments.filter((comment) => comment.unique !== commentUnique))
87
                addNotification({ style: "success", msg: data });
88
            })
89
            .catch((error) => addNotification({ style: "danger", msg: error.message }))
90
    };
91
 
92
    return (
3953 stevensc 93
        <ul className='comment-list'>
94
            {comments.reverse().map((comment) => {
95
                return (
96
                    <FeedCommentSection.CommentTemplate
97
                        commentData={comment}
98
                        onDeleteHandler={deleteCommentHandler}
99
                        key={comment.unique}
100
                    />
101
                );
102
            })}
103
        </ul>
3943 stevensc 104
    )
105
}
106
 
3947 stevensc 107
const CommentTemplate = ({ onDeleteHandler, commentData }) => {
108
 
109
    const {
110
        user_name,
111
        user_url,
112
        user_image,
113
        link_delete,
114
        time_elapsed,
115
        comment,
116
        unique,
117
    } = commentData;
118
 
119
    const [showConfirmModal, setShowConfirmModal] = useState(false);
4100 stevensc 120
    const [displayOption, setDisplayOption] = useState(false)
121
    const deleteButton = useRef();
3947 stevensc 122
 
123
    const handleShowConfirmModal = () => setShowConfirmModal(!showConfirmModal)
124
 
125
    const handleModalAccept = () => onDeleteHandler(unique, link_delete)
126
 
4100 stevensc 127
    useEffect(() => {
128
        const handleClickOutside = (event) => {
129
            if (deleteButton.current && !deleteButton.current.contains(event.target)) {
130
                setDisplayOption(false)
131
            }
132
        }
133
        document.addEventListener("mousedown", handleClickOutside);
134
 
135
        return () => {
136
            document.removeEventListener("mousedown", handleClickOutside);
137
        };
138
    }, [deleteButton]);
139
 
3947 stevensc 140
    return (
3954 stevensc 141
        <li>
142
            <div className="comment-container">
4818 stevensc 143
                {user_image &&
144
                    <img
145
                        src={user_image}
146
                        alt="user-image"
147
                        className='user-image'
148
                    />}
3954 stevensc 149
                <div className='comment-content'>
150
                    <div className='info'>
151
                        <a href={user_url}>
152
                            <h3>{user_name}</h3>
153
                        </a>
154
                        <span>
155
                            {time_elapsed}
156
                            {link_delete &&
4100 stevensc 157
                                <>
158
                                    <img
159
                                        src='/images/icons/options.png'
4106 stevensc 160
                                        className='cursor-pointer img-icon options-sm'
4100 stevensc 161
                                        onClick={() => setDisplayOption(!displayOption)}
162
                                    />
4107 stevensc 163
                                    <div className={`comments-options ${displayOption ? 'active' : ''}`}>
4100 stevensc 164
                                        <ul>
165
                                            <li>
166
                                                <button
167
                                                    className="option-btn"
168
                                                    onClick={handleShowConfirmModal}
169
                                                    ref={deleteButton}
170
                                                >
171
                                                    <i className="fa fa-trash-o mr-1" />
172
                                                    Borrar
173
                                                </button>
174
                                            </li>
175
                                        </ul>
176
                                    </div>
177
                                </>
3954 stevensc 178
                            }
179
                        </span>
3947 stevensc 180
                    </div>
3954 stevensc 181
                    <p>{comment}</p>
3947 stevensc 182
                </div>
3954 stevensc 183
            </div>
3947 stevensc 184
            <ConfirmModal
185
                show={showConfirmModal}
186
                onClose={() => setShowConfirmModal(false)}
187
                onAccept={handleModalAccept}
188
                acceptLabel="Aceptar"
189
            />
3954 stevensc 190
        </li >
3947 stevensc 191
    );
192
};
193
 
194
 
3943 stevensc 195
FeedCommentSection.CommentsList = CommentsList
3947 stevensc 196
FeedCommentSection.CommentTemplate = CommentTemplate
3943 stevensc 197
 
198
const mapDispatchToProps = {
199
    addNotification: (notification) => addNotification(notification),
200
};
201
 
202
export default connect(null, mapDispatchToProps)(FeedCommentSection)