| 7129 |
stevensc |
1 |
import React, { useEffect, useState } from 'react'
|
|
|
2 |
import { axios } from '../../utils'
|
| 7226 |
stevensc |
3 |
import { useLocation } from 'react-router-dom'
|
| 7129 |
stevensc |
4 |
import { getBackendVars } from '../../services/backendVars'
|
|
|
5 |
import { addNotification } from '../../redux/notification/notification.actions'
|
| 7226 |
stevensc |
6 |
import { Col, Container, Row } from 'react-bootstrap'
|
|
|
7 |
import { useDispatch, useSelector } from 'react-redux'
|
| 7129 |
stevensc |
8 |
import parse from 'html-react-parser'
|
|
|
9 |
import TungstenIcon from '@mui/icons-material/Tungsten'
|
|
|
10 |
import RecommendIcon from '@mui/icons-material/Recommend'
|
|
|
11 |
import AccessTimeIcon from '@mui/icons-material/AccessTime'
|
|
|
12 |
import FavoriteIcon from '@mui/icons-material/FavoriteTwoTone'
|
|
|
13 |
import SendOutlinedIcon from '@mui/icons-material/SendOutlined'
|
|
|
14 |
import ChatOutlinedIcon from '@mui/icons-material/ChatOutlined'
|
|
|
15 |
import EmojiEmotionsIcon from '@mui/icons-material/EmojiEmotions'
|
|
|
16 |
import VolunteerActivismIcon from '@mui/icons-material/VolunteerActivism'
|
|
|
17 |
|
| 7226 |
stevensc |
18 |
import { CommentForm, CommentsList } from '../../components/feed/CommentSection'
|
| 7129 |
stevensc |
19 |
import HomeNews from '../../components/widgets/default/HomeNews'
|
|
|
20 |
import InputOption from '../../components/feed/linkedin/InputOption'
|
|
|
21 |
import ReactionsButton from '../../components/UI/buttons/ReactionsButton'
|
| 7226 |
stevensc |
22 |
import withExternalShare from '../../components/feed/linkedin/withExternalShare'
|
| 7129 |
stevensc |
23 |
|
|
|
24 |
const PostViewPage = () => {
|
|
|
25 |
const [post, setPost] = useState({})
|
|
|
26 |
const [totalSends, setTotalSends] = useState(0)
|
|
|
27 |
const [reactions, setReactions] = useState([])
|
|
|
28 |
const [myReaction, setMyReaction] = useState('')
|
|
|
29 |
const [totalReactions, setTotalReactions] = useState(0)
|
|
|
30 |
const [comments, setComments] = useState([])
|
|
|
31 |
const [readMore, setReadMore] = useState(false)
|
|
|
32 |
const [showComments, setShowComments] = useState(false)
|
|
|
33 |
const labels = useSelector(({ intl }) => intl.labels)
|
|
|
34 |
const dispatch = useDispatch()
|
|
|
35 |
const { pathname } = useLocation()
|
|
|
36 |
|
|
|
37 |
const reactionsOptions = [
|
|
|
38 |
{
|
|
|
39 |
type: 'r',
|
|
|
40 |
icon: <RecommendIcon style={{ color: '#7405f9' }} />,
|
|
|
41 |
},
|
|
|
42 |
{
|
|
|
43 |
type: 's',
|
|
|
44 |
icon: <VolunteerActivismIcon style={{ color: '#6495ED' }} />,
|
|
|
45 |
},
|
|
|
46 |
{
|
|
|
47 |
type: 'l',
|
|
|
48 |
icon: <FavoriteIcon style={{ color: '#DF704D' }} />,
|
|
|
49 |
},
|
|
|
50 |
{
|
|
|
51 |
type: 'i',
|
|
|
52 |
icon: (
|
|
|
53 |
<TungstenIcon
|
|
|
54 |
style={{ color: '#F5BB5C', transform: 'rotate(180deg)' }}
|
|
|
55 |
/>
|
|
|
56 |
),
|
|
|
57 |
},
|
|
|
58 |
{
|
|
|
59 |
type: 'f',
|
|
|
60 |
icon: <EmojiEmotionsIcon style={{ color: '#FF7F50' }} />,
|
|
|
61 |
},
|
|
|
62 |
]
|
|
|
63 |
|
|
|
64 |
const readMoreHandler = () => setReadMore(!readMore)
|
|
|
65 |
|
|
|
66 |
const displayCommentSection = () => {
|
|
|
67 |
setShowComments(!showComments)
|
|
|
68 |
}
|
|
|
69 |
|
| 7226 |
stevensc |
70 |
const getComments = () => {
|
| 7129 |
stevensc |
71 |
axios.get(post.comments_url).then((response) => {
|
|
|
72 |
const { data, success } = response.data
|
|
|
73 |
|
|
|
74 |
if (!success) {
|
| 7226 |
stevensc |
75 |
const errorMessage =
|
|
|
76 |
typeof data === 'string' ? data : 'Error interno. Intente más tarde.'
|
|
|
77 |
|
|
|
78 |
dispatch(addNotification({ style: 'danger', msg: errorMessage }))
|
| 7129 |
stevensc |
79 |
return
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
setComments(data)
|
|
|
83 |
})
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
const handleExternalShare = (value) => {
|
|
|
87 |
setTotalSends(value)
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
const ExternalShareButton = withExternalShare(
|
|
|
91 |
InputOption,
|
|
|
92 |
post.share_external_url,
|
|
|
93 |
{
|
|
|
94 |
Icon: SendOutlinedIcon,
|
|
|
95 |
color: 'gray',
|
|
|
96 |
title: 'Enviar',
|
|
|
97 |
shareUrl: post.share_increment_external_counter_url,
|
|
|
98 |
setValue: handleExternalShare,
|
|
|
99 |
withTitle: true,
|
|
|
100 |
}
|
|
|
101 |
)
|
|
|
102 |
|
|
|
103 |
const htmlParsedText = (fullStringText) => {
|
|
|
104 |
const fullText = parse(fullStringText)
|
|
|
105 |
if (fullStringText.length > 500) {
|
|
|
106 |
const shortenedString = fullStringText.substr(0, 500)
|
|
|
107 |
const shortenedText = parse(`${shortenedString}... `)
|
|
|
108 |
return (
|
|
|
109 |
<>
|
|
|
110 |
{readMore ? fullText : shortenedText}
|
|
|
111 |
<span className="cursor-pointer" onClick={readMoreHandler}>
|
|
|
112 |
{readMore ? ' Leer menos' : ' Leer más'}
|
|
|
113 |
</span>
|
|
|
114 |
</>
|
|
|
115 |
)
|
|
|
116 |
}
|
|
|
117 |
return <p>{fullText}</p>
|
|
|
118 |
}
|
|
|
119 |
|
| 7226 |
stevensc |
120 |
const addComment = ({ comment }) => {
|
|
|
121 |
const formData = new FormData()
|
|
|
122 |
formData.append('comment', comment)
|
|
|
123 |
|
|
|
124 |
axios.post(post.comments_add_url, formData).then((response) => {
|
|
|
125 |
const { success, data } = response.data
|
|
|
126 |
|
|
|
127 |
if (!success) {
|
|
|
128 |
const errorMessage =
|
|
|
129 |
typeof data === 'string' ? data : 'Error interno. Intente más tarde.'
|
|
|
130 |
|
|
|
131 |
dispatch(addNotification({ style: 'danger', msg: errorMessage }))
|
|
|
132 |
return
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
setComments((prevMessages) => [...prevMessages, data])
|
|
|
136 |
})
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
const deleteComment = (commentUnique, deleteCommentUrl) => {
|
|
|
140 |
axios
|
|
|
141 |
.post(deleteCommentUrl)
|
|
|
142 |
.then((response) => {
|
|
|
143 |
const { success, data } = response.data
|
|
|
144 |
|
|
|
145 |
if (!success) {
|
|
|
146 |
const errorMessage =
|
|
|
147 |
typeof data === 'string'
|
|
|
148 |
? data
|
|
|
149 |
: 'Error interno. Intente más tarde.'
|
|
|
150 |
|
|
|
151 |
dispatch(addNotification({ style: 'danger', msg: errorMessage }))
|
|
|
152 |
return
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
setComments((prevComments) =>
|
|
|
156 |
prevComments.filter((comment) => comment.unique !== commentUnique)
|
|
|
157 |
)
|
|
|
158 |
dispatch(addNotification({ style: 'success', msg: data }))
|
|
|
159 |
})
|
|
|
160 |
.catch((error) => {
|
|
|
161 |
dispatch(addNotification({ style: 'danger', msg: error }))
|
|
|
162 |
throw new Error(error)
|
|
|
163 |
})
|
|
|
164 |
}
|
|
|
165 |
|
| 7129 |
stevensc |
166 |
useEffect(() => {
|
|
|
167 |
getBackendVars(pathname)
|
|
|
168 |
.then((post) => {
|
|
|
169 |
setMyReaction(post.my_reaction)
|
|
|
170 |
setTotalSends(post.total_share_external)
|
|
|
171 |
setPost(post)
|
|
|
172 |
})
|
|
|
173 |
.catch((error) => {
|
|
|
174 |
dispatch(
|
|
|
175 |
addNotification({
|
|
|
176 |
style: 'danger',
|
| 7201 |
stevensc |
177 |
message: 'Error interno. Por favor, inténtelo de nuevo más tarde.',
|
| 7129 |
stevensc |
178 |
})
|
|
|
179 |
)
|
|
|
180 |
throw new Error(error)
|
|
|
181 |
})
|
|
|
182 |
}, [])
|
|
|
183 |
|
|
|
184 |
useEffect(() => {
|
|
|
185 |
if (showComments && !comments.length) {
|
|
|
186 |
getComments()
|
|
|
187 |
}
|
|
|
188 |
}, [showComments])
|
|
|
189 |
|
|
|
190 |
useEffect(() => {
|
|
|
191 |
const feedReactions = reactions.reduce(
|
|
|
192 |
(acc, reaction) => acc + Number(reaction.total),
|
|
|
193 |
|
|
|
194 |
)
|
|
|
195 |
|
|
|
196 |
setTotalReactions(feedReactions)
|
|
|
197 |
}, [reactions])
|
|
|
198 |
|
|
|
199 |
return (
|
|
|
200 |
<Container>
|
|
|
201 |
<Row>
|
|
|
202 |
<Col md="8">
|
|
|
203 |
<div className="feed">
|
|
|
204 |
<div className="feed__body">
|
|
|
205 |
{post.image && (
|
|
|
206 |
<img
|
|
|
207 |
src={`/storage/type/post/code/${post.uuid}/filename/${post.image}`}
|
|
|
208 |
/>
|
|
|
209 |
)}
|
|
|
210 |
</div>
|
|
|
211 |
<div className="feed__body">
|
|
|
212 |
<div className="feed__header">
|
|
|
213 |
<div className="feed__info">
|
|
|
214 |
<h2>{post.title}</h2>
|
|
|
215 |
<div className="time__elapse">
|
|
|
216 |
<p>{post.addedOn}</p>
|
|
|
217 |
<AccessTimeIcon className="time__elapse-icon" />
|
|
|
218 |
</div>
|
|
|
219 |
</div>
|
|
|
220 |
</div>
|
|
|
221 |
{post.description && htmlParsedText(post.description)}
|
|
|
222 |
{post.file && (
|
|
|
223 |
<a href={post.file} download>
|
|
|
224 |
<img
|
|
|
225 |
className="pdf"
|
|
|
226 |
src="/images/extension/pdf.png"
|
|
|
227 |
alt="pdf"
|
|
|
228 |
/>
|
|
|
229 |
</a>
|
|
|
230 |
)}
|
|
|
231 |
</div>
|
|
|
232 |
<div className="d-flex justify-content-between align-items-center px-3">
|
|
|
233 |
<div className="reactions-counter">
|
|
|
234 |
{reactionsOptions
|
|
|
235 |
.filter((option) =>
|
|
|
236 |
reactions.find(({ reaction }) => reaction === option.type)
|
|
|
237 |
)
|
|
|
238 |
.map((reaction) => reaction.icon)}
|
|
|
239 |
<span>{totalReactions} reacciones</span>
|
|
|
240 |
</div>
|
| 7131 |
stevensc |
241 |
{!!totalSends && (
|
| 7129 |
stevensc |
242 |
<span>{`${totalSends} ${labels.sends?.toLowerCase()}`}</span>
|
|
|
243 |
)}
|
|
|
244 |
</div>
|
|
|
245 |
<div className="feed__buttons">
|
|
|
246 |
<ReactionsButton
|
| 7133 |
stevensc |
247 |
className="btn feed__share-option position-relative"
|
| 7129 |
stevensc |
248 |
currentReaction={myReaction}
|
|
|
249 |
withLabel
|
|
|
250 |
onChange={(newReactions) => setReactions(newReactions)}
|
|
|
251 |
/>
|
|
|
252 |
<InputOption
|
|
|
253 |
Icon={ChatOutlinedIcon}
|
|
|
254 |
title={labels.comments}
|
|
|
255 |
color="gray"
|
|
|
256 |
onClick={displayCommentSection}
|
|
|
257 |
withTitle
|
|
|
258 |
/>
|
|
|
259 |
<ExternalShareButton />
|
|
|
260 |
</div>
|
| 7226 |
stevensc |
261 |
{showComments && (
|
|
|
262 |
<>
|
|
|
263 |
<CommentForm onSubmit={addComment} />
|
|
|
264 |
<CommentsList comments={comments} onDelete={deleteComment} />
|
|
|
265 |
</>
|
|
|
266 |
)}
|
| 7129 |
stevensc |
267 |
</div>
|
|
|
268 |
</Col>
|
|
|
269 |
<Col md="4">
|
|
|
270 |
<HomeNews currentPost={post.uuid} />
|
|
|
271 |
</Col>
|
|
|
272 |
</Row>
|
|
|
273 |
</Container>
|
|
|
274 |
)
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
export default PostViewPage
|