Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3947 | Rev 3952 | 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 (
90
        <div className='commentSection'>
91
            <div className={`comment-sec comment-sec`}>
92
                <ul>
93
                    {comments.reverse().map((comment) => {
94
                        return (
3947 stevensc 95
                            <FeedCommentSection.CommentTemplate
3943 stevensc 96
                                commentData={comment}
97
                                onDeleteHandler={deleteCommentHandler}
98
                                key={comment.unique}
99
                            />
100
                        );
101
                    })}
102
                </ul>
103
            </div>
104
        </div>
105
    )
106
}
107
 
3947 stevensc 108
const CommentTemplate = ({ onDeleteHandler, commentData }) => {
109
 
110
    const {
111
        user_name,
112
        user_url,
113
        user_image,
114
        link_delete,
115
        time_elapsed,
116
        comment,
117
        unique,
118
    } = commentData;
119
 
120
    const [showConfirmModal, setShowConfirmModal] = useState(false);
121
 
122
    const handleShowConfirmModal = () => setShowConfirmModal(!showConfirmModal)
123
 
124
    const handleModalAccept = () => onDeleteHandler(unique, link_delete)
125
 
126
    return (
127
        <>
128
            <li>
3950 stevensc 129
                <div className="comment-container">
130
                    <img
131
                        src={user_image}
132
                        alt="user-image"
133
                        className='user-image'
134
                    />
135
                    <div className='comment-content'>
136
                        <div className='info'>
137
                            <a href={user_url}>
138
                                <h3>{user_name}</h3>
139
                            </a>
140
                            <span>
141
                                <img
142
                                    src="/images/clock.png"
143
                                    alt="Clock"
144
                                    className='mr-2'
145
                                />
146
                                {time_elapsed}
147
                                {link_delete &&
148
                                    <button
149
                                        className="btn-comment-trash"
150
                                        onClick={handleShowConfirmModal}
151
                                    >
152
                                        <i className="fa fa-trash"></i>
153
                                    </button>
154
                                }
155
                            </span>
3947 stevensc 156
                        </div>
157
                    </div>
3950 stevensc 158
                    <p>{comment}</p>
3947 stevensc 159
                </div>
3950 stevensc 160
            </li >
3947 stevensc 161
            <ConfirmModal
162
                show={showConfirmModal}
163
                onClose={() => setShowConfirmModal(false)}
164
                onAccept={handleModalAccept}
165
                acceptLabel="Aceptar"
166
            />
167
        </>
168
    );
169
};
170
 
171
 
3943 stevensc 172
FeedCommentSection.CommentsList = CommentsList
3947 stevensc 173
FeedCommentSection.CommentTemplate = CommentTemplate
3943 stevensc 174
 
175
const mapDispatchToProps = {
176
    addNotification: (notification) => addNotification(notification),
177
};
178
 
179
export default connect(null, mapDispatchToProps)(FeedCommentSection)