Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3962 | Rev 4037 | 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
3964 stevensc 44
                className='feedCommentContainer'
3943 stevensc 45
                onSubmit={handleSubmit(submitCommentHandler)}
46
            >
3964 stevensc 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>
3943 stevensc 59
            </form>
60
            {errors.comment && <FormErrorFeedback>{errors.comment.message}</FormErrorFeedback>}
61
            <FeedCommentSection.CommentsList
62
                comments={commentsState}
63
                updateTotalComments={updateTotalComments}
64
                setComments={setCommentsState}
65
            />
3962 stevensc 66
        </div>
3943 stevensc 67
    )
68
}
69
 
70
const CommentsList = ({ comments, updateTotalComments, setComments }) => {
71
 
72
    const deleteCommentHandler = (commentUnique, deleteCommentUrl) => {
73
        axios.post(deleteCommentUrl)
74
            .then(({ data: response }) => {
75
                const { success, data, total_comments } = response
76
 
77
                if (!success) {
78
                    return addNotification({ style: "danger", msg: data })
79
                }
80
 
81
                updateTotalComments(total_comments)
82
                setComments(prevComments => prevComments.filter((comment) => comment.unique !== commentUnique))
83
                addNotification({ style: "success", msg: data });
84
            })
85
            .catch((error) => addNotification({ style: "danger", msg: error.message }))
86
    };
87
 
88
    return (
3953 stevensc 89
        <ul className='comment-list'>
90
            {comments.reverse().map((comment) => {
91
                return (
92
                    <FeedCommentSection.CommentTemplate
93
                        commentData={comment}
94
                        onDeleteHandler={deleteCommentHandler}
95
                        key={comment.unique}
96
                    />
97
                );
98
            })}
99
        </ul>
3943 stevensc 100
    )
101
}
102
 
3947 stevensc 103
const CommentTemplate = ({ onDeleteHandler, commentData }) => {
104
 
105
    const {
106
        user_name,
107
        user_url,
108
        user_image,
109
        link_delete,
110
        time_elapsed,
111
        comment,
112
        unique,
113
    } = commentData;
114
 
115
    const [showConfirmModal, setShowConfirmModal] = useState(false);
116
 
117
    const handleShowConfirmModal = () => setShowConfirmModal(!showConfirmModal)
118
 
119
    const handleModalAccept = () => onDeleteHandler(unique, link_delete)
120
 
121
    return (
3954 stevensc 122
        <li>
123
            <div className="comment-container">
124
                <img
125
                    src={user_image}
126
                    alt="user-image"
127
                    className='user-image'
128
                />
129
                <div className='comment-content'>
130
                    <div className='info'>
131
                        <a href={user_url}>
132
                            <h3>{user_name}</h3>
133
                        </a>
134
                        <span>
135
                            <img
136
                                src="/images/clock.png"
137
                                alt="Clock"
138
                                className='mr-2'
139
                            />
140
                            {time_elapsed}
141
                            {link_delete &&
142
                                <button
143
                                    className="btn-comment-trash"
144
                                    onClick={handleShowConfirmModal}
145
                                >
146
                                    <i className="fa fa-trash"></i>
147
                                </button>
148
                            }
149
                        </span>
3947 stevensc 150
                    </div>
3954 stevensc 151
                    <p>{comment}</p>
3947 stevensc 152
                </div>
3954 stevensc 153
            </div>
3947 stevensc 154
            <ConfirmModal
155
                show={showConfirmModal}
156
                onClose={() => setShowConfirmModal(false)}
157
                onAccept={handleModalAccept}
158
                acceptLabel="Aceptar"
159
            />
3954 stevensc 160
        </li >
3947 stevensc 161
    );
162
};
163
 
164
 
3943 stevensc 165
FeedCommentSection.CommentsList = CommentsList
3947 stevensc 166
FeedCommentSection.CommentTemplate = CommentTemplate
3943 stevensc 167
 
168
const mapDispatchToProps = {
169
    addNotification: (notification) => addNotification(notification),
170
};
171
 
172
export default connect(null, mapDispatchToProps)(FeedCommentSection)