Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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