Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

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