Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7140 | Rev 7143 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6830 stevensc 1
import React, { useEffect, useRef, useState } from 'react'
2
import { axios } from '../../../utils'
3
import { feedTypes } from '../../../redux/feed/feed.types'
4
import { deleteFeed } from '../../../redux/feed/feed.actions'
5
import { openShareModal } from '../../../redux/share-modal/shareModal.actions'
6
import { shareModalTypes } from '../../../redux/share-modal/shareModal.types'
7
import { addNotification } from '../../../redux/notification/notification.actions'
8
import { connect, useDispatch, useSelector } from 'react-redux'
9
import parse from 'html-react-parser'
10
import TungstenIcon from '@mui/icons-material/Tungsten'
11
import FavoriteIcon from '@mui/icons-material/FavoriteTwoTone'
12
import RecommendIcon from '@mui/icons-material/Recommend'
13
import AccessTimeIcon from '@mui/icons-material/AccessTime'
14
import SendOutlinedIcon from '@mui/icons-material/SendOutlined'
15
import ChatOutlinedIcon from '@mui/icons-material/ChatOutlined'
16
import ShareOutlinedIcon from '@mui/icons-material/ShareOutlined'
17
import EmojiEmotionsIcon from '@mui/icons-material/EmojiEmotions'
18
import VolunteerActivismIcon from '@mui/icons-material/VolunteerActivism'
19
 
20
import InputOption from './InputOption'
21
import Avatar from '../../UI/AvatarImage'
22
import ConfirmModal from '../../modals/ConfirmModal'
23
import FeedCommentSection from '../CommentSection'
24
import withExternalShare from './withExternalShare'
25
import FeedModal from '../FeedModal'
26
import SurveyForm from '../../survey-form/SurveyForm'
27
 
6835 stevensc 28
import './Feed.scss'
7142 stevensc 29
import ReactionsButton from '../../UI/buttons/ReactionsButton'
6835 stevensc 30
 
6830 stevensc 31
const Feed = (props) => {
32
  const {
33
    isShare = false,
34
    feed_unique,
35
    feed_share_url,
36
    feed_share_external_url,
37
    feed_delete_url,
38
    feed_my_reaction,
39
    feed_reactions,
40
    owner_url,
7136 stevensc 41
    image,
6830 stevensc 42
    owner_image,
43
    owner_name,
44
    owner_description,
45
    owner_shared,
46
    owner_comments,
47
    owner_time_elapse,
48
    owner_file_image_preview,
49
    owner_file_video,
50
    owner_file_image,
51
    owner_file_document,
52
    comment_add_url,
53
    comments,
54
    shared_name,
55
    shared_image,
56
    shared_time_elapse,
57
    shared_description,
58
    shared_file_video,
59
    shared_file_image_preview,
60
    shared_file_image,
61
    owner_external_shared,
62
    shared_file_document,
63
    shared_url,
64
    feed_increment_external_counter_url,
65
    feed_content_type,
66
    feed_vote_url,
7142 stevensc 67
    save_reaction_url,
68
    delete_reaction_url,
6830 stevensc 69
    openShareModal, // REDUX ACTION
70
  } = props
71
  const [ownerReactions, setOwnerReaction] = useState(feed_reactions)
72
  const [totalReactions, setTotalReactions] = useState(0)
73
  const [totalComments, setTotalComments] = useState(owner_comments)
74
  const [externalShare, setExternalShare] = useState(owner_external_shared)
75
  const [sharedState, setSharedState] = useState(owner_shared)
76
  const [showComments, setShowComments] = useState(false)
77
  const [showModal, setShowModal] = useState(false)
6832 stevensc 78
  const labels = useSelector(({ intl }) => intl.labels)
6830 stevensc 79
 
80
  const reactionsOptions = [
81
    {
82
      type: 'r',
83
      icon: <RecommendIcon style={{ color: '#7405f9' }} />,
84
    },
85
    {
86
      type: 's',
87
      icon: <VolunteerActivismIcon style={{ color: '#6495ED' }} />,
88
    },
89
    {
90
      type: 'l',
91
      icon: <FavoriteIcon style={{ color: '#DF704D' }} />,
92
    },
93
    {
94
      type: 'i',
95
      icon: (
96
        <TungstenIcon
97
          style={{ color: '#F5BB5C', transform: 'rotate(180deg)' }}
98
        />
99
      ),
100
    },
101
    {
102
      type: 'f',
103
      icon: <EmojiEmotionsIcon style={{ color: '#FF7F50' }} />,
104
    },
105
  ]
106
 
107
  const handleShare = () =>
108
    openShareModal(
109
      feed_share_url,
110
      shareModalTypes.SHARE,
111
      feedTypes.DASHBOARD,
112
      feed_unique
113
    )
114
  const handleExternalShare = (value) => setExternalShare(value)
115
 
116
  const displayCommentSection = () => setShowComments(!showComments)
117
 
118
  const ExternalShareButton = withExternalShare(
119
    InputOption,
120
    feed_share_external_url,
121
    {
122
      Icon: SendOutlinedIcon,
123
      color: 'gray',
124
      title: 'Send',
125
      shareUrl: feed_increment_external_counter_url,
126
      setValue: handleExternalShare,
127
      withTitle: true,
128
    }
129
  )
130
 
131
  useEffect(() => setSharedState(owner_shared), [owner_shared])
132
 
133
  useEffect(() => {
134
    const feedReactions = ownerReactions?.reduce(
135
      (acc, reaction) => acc + Number(reaction.total),
136
 
137
    )
138
    setTotalReactions(feedReactions)
139
  }, [ownerReactions])
140
 
141
  return (
142
    <>
143
      {showModal && (
144
        <FeedModal
145
          isShow={true}
146
          feed={props}
147
          handleClose={() => setShowModal(false)}
148
        />
149
      )}
150
      <div className="feed">
151
        <Feed.Header
152
          image={owner_image}
153
          name={owner_name}
154
          timeElapsed={owner_time_elapse}
155
          viewUrl={owner_url}
156
          deleteUrl={feed_delete_url}
157
          feedUnique={feed_unique}
158
        />
159
 
160
        <div
161
          className="feed__body"
162
          onClick={() =>
163
            (owner_file_image || owner_file_video || owner_file_document) &&
164
            setShowModal(true)
165
          }
166
        >
167
          <Feed.Content
168
            description={owner_description}
169
            image={owner_file_image}
170
            imagePreview={owner_file_image_preview}
171
            video={owner_file_video}
172
            document={owner_file_document}
173
            sharedItem={{
174
              name: shared_name,
175
              image: shared_image,
176
              time_elapse: shared_time_elapse,
177
              description: shared_description,
178
              file_video: shared_file_video,
179
              file_image_preview: shared_file_image_preview,
180
              file_image: shared_file_image,
181
              file_document: shared_file_document,
182
              shared_url,
183
            }}
184
            type={feed_content_type}
185
            voteUrl={feed_vote_url}
186
          />
187
        </div>
188
 
189
        {!isShare && feed_content_type !== 'fast-survey' && (
190
          <div className="px-3 d-flex align-items-center justify-content-between">
191
            <div className="reactions-counter">
192
              {reactionsOptions
193
                .filter((option) =>
194
                  ownerReactions.find(
195
                    (reaction) => reaction.reaction === option.type
196
                  )
197
                )
198
                .map((reaction) => reaction.icon)}
199
              <span>{totalReactions} reacciones</span>
200
            </div>
201
            <div
202
              className="d-inline-flex align-items-center"
203
              style={{ gap: '5px' }}
204
            >
205
              {!!totalComments && (
6832 stevensc 206
                <span>{`${totalComments} ${labels.comments?.toLowerCase()}`}</span>
6830 stevensc 207
              )}
208
              {!!sharedState && (
6832 stevensc 209
                <span>{`${sharedState} ${labels.shared?.toLowerCase()}`}</span>
6830 stevensc 210
              )}
211
              {!!externalShare && (
6832 stevensc 212
                <span>{`${externalShare} ${labels.sends?.toLowerCase()}`}</span>
6830 stevensc 213
              )}
214
            </div>
215
          </div>
216
        )}
217
 
218
        {!isShare && feed_content_type !== 'fast-survey' && (
219
          <div className="feed__buttons">
7142 stevensc 220
            <ReactionsButton
221
              currentReaction={feed_my_reaction}
222
              saveUrl={save_reaction_url}
223
              deleteUrl={delete_reaction_url}
224
              onChange={(reactions) => setOwnerReaction(reactions)}
225
              withLabel
226
            />
6830 stevensc 227
            <InputOption
228
              Icon={ChatOutlinedIcon}
6832 stevensc 229
              title={labels.comment}
6830 stevensc 230
              color="gray"
231
              onClick={displayCommentSection}
232
              withTitle
233
            />
234
            <InputOption
235
              Icon={ShareOutlinedIcon}
6832 stevensc 236
              title={labels.share}
6830 stevensc 237
              color="gray"
238
              onClick={handleShare}
239
              withTitle
240
            />
241
            <ExternalShareButton />
242
          </div>
243
        )}
244
 
245
        <div className="px-2 pb-2">
246
          <FeedCommentSection
247
            feedId={feed_unique}
7136 stevensc 248
            image={image}
6830 stevensc 249
            addUrl={comment_add_url}
250
            updateTotalComments={(total) => setTotalComments(total)}
251
            currentComments={comments}
252
            isShow={showComments}
253
          />
254
        </div>
255
      </div>
256
    </>
257
  )
258
}
259
 
260
const Content = ({
261
  description,
262
  image,
263
  imagePreview,
264
  video,
265
  document,
266
  sharedItem,
267
  type,
268
  voteUrl,
269
}) => {
270
  const [isReadMoreActive, setIsReadMoreActive] = useState(false)
6832 stevensc 271
  const labels = useSelector(({ intl }) => intl.labels)
6830 stevensc 272
 
273
  const readMoreHandler = () => setIsReadMoreActive(!isReadMoreActive)
274
 
6831 stevensc 275
  const htmlParsedText = (fullStringText = '') => {
6830 stevensc 276
    const fullText = parse(fullStringText)
277
    if (fullStringText.length > 500) {
278
      const shortenedString = fullStringText.substr(0, 500)
279
      const shortenedText = parse(`${shortenedString}... `)
280
      return (
281
        <>
282
          {isReadMoreActive ? fullText : shortenedText}
283
          <span className="cursor-pointer" onClick={readMoreHandler}>
6832 stevensc 284
            {isReadMoreActive ? labels.read_less : labels.read_more}
6830 stevensc 285
          </span>
286
        </>
287
      )
288
    }
289
    return <p>{fullText}</p>
290
  }
291
 
292
  return (
293
    <>
294
      {type !== 'fast-survey' ? (
295
        htmlParsedText(description)
296
      ) : (
297
        <SurveyForm
298
          active={description.active}
299
          question={description.question}
300
          answers={[
301
            description.answer1,
302
            description.answer2,
303
            description.answer3,
304
            description.answer4,
305
            description.answer5,
306
          ]}
307
          votes={
308
            description.votes1 && [
309
              description.votes1,
310
              description.votes2,
311
              description.votes3,
312
              description.votes4,
313
              description.votes5,
314
            ]
315
          }
316
          time={description.time_remaining}
317
          voteUrl={voteUrl}
318
          resultType={description.result_type}
319
        />
320
      )}
321
      {image && <img src={image} className="Entradas" loading="lazy" />}
322
      {video && (
323
        <video src={video} controls poster={imagePreview} preload="none" />
324
      )}
325
      {document && (
7140 stevensc 326
        <a href={document} download>
327
          <img className="pdf" src="/images/extension/pdf.png" alt="pdf" />
6830 stevensc 328
        </a>
329
      )}
330
      {sharedItem.name && (
331
        <div className="py-3 px-md-3">
332
          <Feed
333
            isShare={true}
334
            owner_name={sharedItem.name}
335
            owner_image={sharedItem.image}
336
            owner_time_elapse={sharedItem.time_elapse}
337
            owner_description={sharedItem.description}
338
            owner_file_video={sharedItem.file_video}
339
            owner_file_image_preview={sharedItem.file_image_preview}
340
            owner_file_image={sharedItem.file_image}
341
            owner_file_document={sharedItem.file_document}
342
            owner_url={sharedItem.shared_url}
343
          />
344
        </div>
345
      )}
346
    </>
347
  )
348
}
349
 
350
const Header = ({
351
  image = '',
352
  name = '',
353
  timeElapsed = '',
354
  deleteUrl = '',
355
  viewUrl = '',
356
  feedUnique = '',
357
}) => {
358
  const [showConfirmModal, setShowConfirmModal] = useState(false)
359
  const [displayOption, setDisplayOption] = useState(false)
360
  const deleteButton = useRef()
6832 stevensc 361
  const labels = useSelector(({ intl }) => intl.labels)
6830 stevensc 362
  const dispatch = useDispatch()
363
 
364
  const handleShowConfirmModal = () => setShowConfirmModal(!showConfirmModal)
365
 
366
  const deleteFeedHandler = () => {
367
    axios.post(deleteUrl).then((res) => {
368
      const { data } = res
369
      if (!data.success) {
370
        dispatch(addNotification({ style: 'danger', msg: data.data }))
371
        return
372
      }
373
      dispatch(addNotification({ style: 'success', msg: data.data }))
374
      handleShowConfirmModal()
375
      dispatch(deleteFeed(feedUnique))
376
    })
377
  }
378
 
379
  useEffect(() => {
380
    const handleClickOutside = (event) => {
381
      if (
382
        deleteButton.current &&
383
        !deleteButton.current.contains(event.target)
384
      ) {
385
        setDisplayOption(false)
386
      }
387
    }
388
    document.addEventListener('mousedown', handleClickOutside)
389
 
390
    return () => {
391
      document.removeEventListener('mousedown', handleClickOutside)
392
    }
393
  }, [deleteButton])
394
 
395
  return (
396
    <div className="feed__header">
397
      <div className="d-inline-flex" style={{ gap: '.5rem' }}>
398
        <Avatar imageUrl={image} name={name} size="xl" />
399
 
400
        <div className="feed__info">
401
          <a href={viewUrl}>
402
            <h2>{name}</h2>
403
          </a>
404
          <div className="time__elapse">
405
            <p>{timeElapsed}</p>
406
            <AccessTimeIcon className="time__elapse-icon" />
407
          </div>
408
        </div>
409
      </div>
410
 
411
      {deleteUrl && (
412
        <div className="cursor-pointer d-flex align-items-center position-relative">
413
          <img
414
            src="/images/icons/options.png"
415
            className="cursor-pointer img-icon options"
416
            onClick={() => setDisplayOption(!displayOption)}
417
          />
418
          <div className={`feed-options ${displayOption ? 'active' : ''}`}>
419
            <ul>
420
              <li>
421
                <button
422
                  className="option-btn"
423
                  onClick={handleShowConfirmModal}
424
                  ref={deleteButton}
425
                >
426
                  <i className="fa fa-trash-o mr-1" />
6832 stevensc 427
                  {labels.delete}
6830 stevensc 428
                </button>
429
              </li>
430
            </ul>
431
          </div>
432
          <ConfirmModal
433
            show={showConfirmModal}
434
            onClose={() => handleShowConfirmModal(false)}
435
            onAccept={deleteFeedHandler}
6832 stevensc 436
            acceptLabel={labels.accept}
6830 stevensc 437
          />
438
        </div>
439
      )}
440
    </div>
441
  )
442
}
443
 
444
Feed.Content = Content
445
Feed.Header = Header
446
 
447
const mapDispatchToProps = {
448
  addNotification: (notification) => addNotification(notification),
449
  openShareModal: (postUrl, modalType, feedType) =>
450
    openShareModal(postUrl, modalType, feedType),
451
}
452
 
453
export default connect(null, mapDispatchToProps)(Feed)