Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3954 | Rev 3964 | 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 = ({
3962 stevensc 12
    isShow = true,
3943 stevensc 13
    image = '',
14
    addUrl = '',
15
    updateTotalComments = function () { },
3962 stevensc 16
    comments = [],
3943 stevensc 17
}) => {
18
 
19
    const { register, handleSubmit, errors, reset } = useForm()
20
    const [commentsState, setCommentsState] = useState(comments);
21
 
22
    const submitCommentHandler = (data) => {
3944 stevensc 23
 
3945 stevensc 24
        const currentFormData = new FormData();
3947 stevensc 25
        Object.entries(data).forEach(([key, value]) => currentFormData.append(key, value))
3943 stevensc 26
 
27
        axios.post(addUrl, currentFormData)
28
            .then(({ data: response }) => {
29
                const { data: newComment, success, total_comments } = response;
30
 
31
                if (!success) {
32
                    return addNotification({ style: "danger", msg: data })
33
                }
34
 
35
                updateTotalComments(total_comments)
36
                setCommentsState([newComment, ...commentsState]);
37
                reset();
38
            })
39
    };
40
 
41
    return (
3962 stevensc 42
        <div className={`comments-container ${isShow ? 'show' : 'hidden'}`}>
3943 stevensc 43
            <form
44
                className='form-comment-feed'
45
                onSubmit={handleSubmit(submitCommentHandler)}
3962 stevensc 46
 
3943 stevensc 47
            >
48
                <div className='feedCommentContainer'>
49
                    <img src={image} alt="User profile image" />
50
                    <input
51
                        className='commentInput'
52
                        type="text"
53
                        name="comment"
54
                        maxLength="256"
55
                        placeholder="Escribe un comentario"
56
                        ref={register({ required: "El campo es requerido" })}
57
                    />
58
                    <button className='shareIconContainer iconActive' >
59
                        <TbSend className='shareIcon' />
60
                    </button>
61
                </div>
62
            </form>
63
            {errors.comment && <FormErrorFeedback>{errors.comment.message}</FormErrorFeedback>}
64
            <FeedCommentSection.CommentsList
65
                comments={commentsState}
66
                updateTotalComments={updateTotalComments}
67
                setComments={setCommentsState}
68
            />
3962 stevensc 69
        </div>
3943 stevensc 70
    )
71
}
72
 
73
const CommentsList = ({ comments, updateTotalComments, setComments }) => {
74
 
75
    const deleteCommentHandler = (commentUnique, deleteCommentUrl) => {
76
        axios.post(deleteCommentUrl)
77
            .then(({ data: response }) => {
78
                const { success, data, total_comments } = response
79
 
80
                if (!success) {
81
                    return addNotification({ style: "danger", msg: data })
82
                }
83
 
84
                updateTotalComments(total_comments)
85
                setComments(prevComments => prevComments.filter((comment) => comment.unique !== commentUnique))
86
                addNotification({ style: "success", msg: data });
87
            })
88
            .catch((error) => addNotification({ style: "danger", msg: error.message }))
89
    };
90
 
91
    return (
3953 stevensc 92
        <ul className='comment-list'>
93
            {comments.reverse().map((comment) => {
94
                return (
95
                    <FeedCommentSection.CommentTemplate
96
                        commentData={comment}
97
                        onDeleteHandler={deleteCommentHandler}
98
                        key={comment.unique}
99
                    />
100
                );
101
            })}
102
        </ul>
3943 stevensc 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 (
3954 stevensc 125
        <li>
126
            <div className="comment-container">
127
                <img
128
                    src={user_image}
129
                    alt="user-image"
130
                    className='user-image'
131
                />
132
                <div className='comment-content'>
133
                    <div className='info'>
134
                        <a href={user_url}>
135
                            <h3>{user_name}</h3>
136
                        </a>
137
                        <span>
138
                            <img
139
                                src="/images/clock.png"
140
                                alt="Clock"
141
                                className='mr-2'
142
                            />
143
                            {time_elapsed}
144
                            {link_delete &&
145
                                <button
146
                                    className="btn-comment-trash"
147
                                    onClick={handleShowConfirmModal}
148
                                >
149
                                    <i className="fa fa-trash"></i>
150
                                </button>
151
                            }
152
                        </span>
3947 stevensc 153
                    </div>
3954 stevensc 154
                    <p>{comment}</p>
3947 stevensc 155
                </div>
3954 stevensc 156
            </div>
3947 stevensc 157
            <ConfirmModal
158
                show={showConfirmModal}
159
                onClose={() => setShowConfirmModal(false)}
160
                onAccept={handleModalAccept}
161
                acceptLabel="Aceptar"
162
            />
3954 stevensc 163
        </li >
3947 stevensc 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)