Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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