| 6618 |
stevensc |
1 |
import React, { useEffect, useRef, useState } from 'react'
|
|
|
2 |
import { axios } from '../../../utils'
|
|
|
3 |
import { useForm } from 'react-hook-form'
|
|
|
4 |
import { connect, useSelector } from 'react-redux'
|
|
|
5 |
|
|
|
6 |
import { addNotification } from '../../redux/notification/notification.actions'
|
|
|
7 |
|
|
|
8 |
import ConfirmModal from '../modals/ConfirmModal'
|
|
|
9 |
import useOutsideClick from '../../hooks/useOutsideClick'
|
|
|
10 |
import FormErrorFeedback from '../UI/FormErrorFeedback'
|
|
|
11 |
|
|
|
12 |
const FeedComments = ({
|
|
|
13 |
isShow = true,
|
|
|
14 |
image = '/images/avatar.jpg',
|
|
|
15 |
addUrl = '',
|
|
|
16 |
currentComments = [],
|
|
|
17 |
addNotification,
|
|
|
18 |
updateTotalComments = function () {},
|
|
|
19 |
}) => {
|
|
|
20 |
const { register, handleSubmit, errors, reset } = useForm()
|
|
|
21 |
const [comments, setComments] = useState(currentComments)
|
|
|
22 |
const labels = useSelector(({ intl }) => intl.labels)
|
|
|
23 |
|
|
|
24 |
const submitCommet = (data) => {
|
|
|
25 |
const currentFormData = new FormData()
|
|
|
26 |
Object.entries(data).forEach(([key, value]) =>
|
|
|
27 |
currentFormData.append(key, value)
|
|
|
28 |
)
|
|
|
29 |
|
|
|
30 |
axios.post(addUrl, currentFormData).then(({ data: response }) => {
|
|
|
31 |
const { success, data: message, total_comments } = response
|
|
|
32 |
|
|
|
33 |
if (!success) {
|
|
|
34 |
addNotification({ style: 'danger', msg: message })
|
|
|
35 |
return
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
updateTotalComments(total_comments)
|
|
|
39 |
setComments((prevMessages) => [...prevMessages, message])
|
|
|
40 |
reset()
|
|
|
41 |
})
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
const deleteComment = (commentUnique, deleteCommentUrl) => {
|
|
|
45 |
axios
|
|
|
46 |
.post(deleteCommentUrl)
|
|
|
47 |
.then(({ data: response }) => {
|
|
|
48 |
const { success, data, total_comments } = response
|
|
|
49 |
|
|
|
50 |
if (!success) {
|
|
|
51 |
return addNotification({ style: 'danger', msg: data })
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
updateTotalComments(total_comments)
|
|
|
55 |
setComments((prevComments) =>
|
|
|
56 |
prevComments.filter((comment) => comment.unique !== commentUnique)
|
|
|
57 |
)
|
|
|
58 |
addNotification({ style: 'success', msg: data })
|
|
|
59 |
})
|
|
|
60 |
.catch((error) => addNotification({ style: 'danger', msg: error }))
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
useEffect(() => {
|
|
|
64 |
setComments(currentComments)
|
|
|
65 |
}, [currentComments])
|
|
|
66 |
|
|
|
67 |
return (
|
|
|
68 |
<div className={`comments-container ${isShow ? 'show' : 'hidden'}`}>
|
|
|
69 |
<form
|
|
|
70 |
className="feedCommentContainer"
|
|
|
71 |
onSubmit={handleSubmit(submitCommet)}
|
|
|
72 |
>
|
|
|
73 |
<img src={image} alt="User profile image" />
|
|
|
74 |
<input
|
|
|
75 |
className="commentInput"
|
|
|
76 |
type="text"
|
|
|
77 |
name="comment"
|
|
|
78 |
maxLength="256"
|
|
|
79 |
placeholder={labels.write_a_comment}
|
|
|
80 |
ref={register({ required: 'El campo es requerido' })}
|
|
|
81 |
/>
|
|
|
82 |
<button className="shareIconContainer iconActive">
|
|
|
83 |
<img src="/images/icons/send.png" className="fa img-icon" />
|
|
|
84 |
</button>
|
|
|
85 |
</form>
|
|
|
86 |
{errors.comment && (
|
|
|
87 |
<FormErrorFeedback>{errors.comment.message}</FormErrorFeedback>
|
|
|
88 |
)}
|
|
|
89 |
<FeedComments.List comments={comments} onDelete={deleteComment} />
|
|
|
90 |
</div>
|
|
|
91 |
)
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
const CommentsList = ({ comments, onDelete }) => {
|
|
|
95 |
return (
|
|
|
96 |
<ul className="comment-list">
|
|
|
97 |
{comments.map((comment) => {
|
|
|
98 |
return (
|
|
|
99 |
<CommentsList.Item
|
|
|
100 |
comment={comment}
|
|
|
101 |
onDelete={onDelete}
|
|
|
102 |
key={comment.unique}
|
|
|
103 |
/>
|
|
|
104 |
)
|
|
|
105 |
})}
|
|
|
106 |
</ul>
|
|
|
107 |
)
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
const CommentTemplate = ({ onDelete, comment }) => {
|
|
|
111 |
const {
|
|
|
112 |
unique,
|
|
|
113 |
user_url,
|
|
|
114 |
user_name,
|
|
|
115 |
user_image = '/images/avatar.jpg',
|
|
|
116 |
time_elapsed,
|
|
|
117 |
comment: content,
|
|
|
118 |
link_delete,
|
|
|
119 |
} = comment
|
|
|
120 |
const [showConfirmModal, setShowConfirmModal] = useState(false)
|
|
|
121 |
const [displayOption, setDisplayOption] = useState(false)
|
|
|
122 |
const deleteButton = useRef(null)
|
|
|
123 |
const labels = useSelector(({ intl }) => intl.labels)
|
|
|
124 |
useOutsideClick(deleteButton, () => setDisplayOption(false))
|
|
|
125 |
|
|
|
126 |
const toggleModal = () => setShowConfirmModal(!showConfirmModal)
|
|
|
127 |
|
|
|
128 |
return (
|
|
|
129 |
<li>
|
|
|
130 |
<div className="comment-container">
|
|
|
131 |
<img src={user_image} alt="user-image" className="user-image" />
|
|
|
132 |
<div className="comment-content">
|
|
|
133 |
<div className="info">
|
|
|
134 |
<a href={user_url}>
|
|
|
135 |
<h3>{user_name}</h3>
|
|
|
136 |
</a>
|
|
|
137 |
<span>
|
|
|
138 |
{time_elapsed}
|
|
|
139 |
{comment.link_delete && (
|
|
|
140 |
<>
|
|
|
141 |
<img
|
|
|
142 |
src="/images/icons/options.png"
|
|
|
143 |
className="cursor-pointer img-icon options-sm"
|
|
|
144 |
onClick={() => setDisplayOption(!displayOption)}
|
|
|
145 |
/>
|
|
|
146 |
<div
|
|
|
147 |
className={`comments-options ${
|
|
|
148 |
displayOption ? 'active' : ''
|
|
|
149 |
}`}
|
|
|
150 |
>
|
|
|
151 |
<ul>
|
|
|
152 |
<li>
|
|
|
153 |
<button
|
|
|
154 |
className="option-btn"
|
|
|
155 |
onClick={toggleModal}
|
|
|
156 |
ref={deleteButton}
|
|
|
157 |
>
|
|
|
158 |
<i className="fa fa-trash-o mr-1" />
|
|
|
159 |
{labels.delete}
|
|
|
160 |
</button>
|
|
|
161 |
</li>
|
|
|
162 |
</ul>
|
|
|
163 |
</div>
|
|
|
164 |
</>
|
|
|
165 |
)}
|
|
|
166 |
</span>
|
|
|
167 |
</div>
|
|
|
168 |
<p>{content}</p>
|
|
|
169 |
</div>
|
|
|
170 |
</div>
|
|
|
171 |
<ConfirmModal
|
|
|
172 |
show={showConfirmModal}
|
|
|
173 |
onClose={toggleModal}
|
|
|
174 |
onAccept={() => onDelete(unique, link_delete)}
|
|
|
175 |
acceptLabel={labels.accept}
|
|
|
176 |
/>
|
|
|
177 |
</li>
|
|
|
178 |
)
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
FeedComments.List = CommentsList
|
|
|
182 |
CommentsList.Item = CommentTemplate
|
|
|
183 |
|
|
|
184 |
const mapDispatchToProps = {
|
|
|
185 |
addNotification: (notification) => addNotification(notification),
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
export default connect(null, mapDispatchToProps)(FeedComments)
|