Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4050 | Rev 4061 | 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 React, { 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
            >
3964 stevensc 46
                <img src={image} alt="User profile image" />
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' >
4051 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);
115
 
116
    const handleShowConfirmModal = () => setShowConfirmModal(!showConfirmModal)
117
 
118
    const handleModalAccept = () => onDeleteHandler(unique, link_delete)
119
 
120
    return (
3954 stevensc 121
        <li>
122
            <div className="comment-container">
123
                <img
124
                    src={user_image}
125
                    alt="user-image"
126
                    className='user-image'
127
                />
128
                <div className='comment-content'>
129
                    <div className='info'>
130
                        <a href={user_url}>
131
                            <h3>{user_name}</h3>
132
                        </a>
133
                        <span>
134
                            <img
135
                                src="/images/clock.png"
136
                                alt="Clock"
137
                                className='mr-2'
138
                            />
139
                            {time_elapsed}
140
                            {link_delete &&
141
                                <button
142
                                    className="btn-comment-trash"
143
                                    onClick={handleShowConfirmModal}
144
                                >
4046 stevensc 145
                                    <i className="fa fa-trash-o"></i>
3954 stevensc 146
                                </button>
147
                            }
148
                        </span>
3947 stevensc 149
                    </div>
3954 stevensc 150
                    <p>{comment}</p>
3947 stevensc 151
                </div>
3954 stevensc 152
            </div>
3947 stevensc 153
            <ConfirmModal
154
                show={showConfirmModal}
155
                onClose={() => setShowConfirmModal(false)}
156
                onAccept={handleModalAccept}
157
                acceptLabel="Aceptar"
158
            />
3954 stevensc 159
        </li >
3947 stevensc 160
    );
161
};
162
 
163
 
3943 stevensc 164
FeedCommentSection.CommentsList = CommentsList
3947 stevensc 165
FeedCommentSection.CommentTemplate = CommentTemplate
3943 stevensc 166
 
167
const mapDispatchToProps = {
168
    addNotification: (notification) => addNotification(notification),
169
};
170
 
171
export default connect(null, mapDispatchToProps)(FeedCommentSection)