Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
2838 stevensc 1
import React, { useMemo, useState } from 'react'
2
import { useSelector } from 'react-redux'
3
 
4
import Widget from '@components/UI/Widget'
5
import SurveyForm from '@components/survey-form/SurveyForm'
6
import FeedDescription from './feed-description'
7
 
8
export default function FeedContent({ id }) {
9
  const [showModal, setShowModal] = useState(false)
10
 
11
  const {
12
    owner_description: description,
13
    owner_file_image: image,
14
    owner_file_document: document,
15
    owner_file_video: video,
16
    owner_file_image_preview: imagePreview,
17
    feed_vote_url: voteUrl,
18
    feed_content_type: contentType,
19
    shared_name: sharedName,
20
    shared_image: sharedImage,
21
    shared_time_elapse: sharedTimeElapse,
22
    shared_description: sharedFeedDescription,
23
    shared_file_video: sharedFileVideo,
24
    shared_file_image_preview: sharedFileImagePreview,
25
    shared_file_image: sharedFileImage,
26
    shared_file_document: sharedFileDocument,
27
    shared_content_type: sharedContentType
28
  } = useSelector(({ feed }) => feed.feeds.byId[id])
29
 
30
  const answers = useMemo(() => {
31
    if (contentType !== 'fast-survey') return []
32
    return Array.from({ length: 5 }, (_, i) => description[`answer${i + 1}`])
33
  }, [description, contentType])
34
 
35
  const votes = useMemo(() => {
36
    if (contentType !== 'fast-survey') return []
37
    return Array.from({ length: 5 }, (_, i) => description[`votes${i + 1}`])
38
  }, [description, contentType])
39
 
40
  const toggleModal = () => setShowModal(!showModal)
41
 
42
  const renderContent = ({
43
    description,
44
    image,
45
    document,
46
    video,
47
    imagePreview,
48
    voteUrl,
49
    sharedName,
50
    sharedImage,
51
    sharedTimeElapse,
52
    type
53
  }) => {
54
    switch (type) {
55
      case 'video': {
56
        return (
57
          <>
58
            <FeedDescription description={description} />
59
            <Widget.Media
60
              type='video'
61
              image={imagePreview}
62
              src={video}
63
              onClick={toggleModal}
64
            />
65
          </>
66
        )
67
      }
68
      case 'image': {
69
        return (
70
          <>
71
            <FeedDescription description={description} />
72
            <Widget.Media src={image} onClick={toggleModal} />
73
          </>
74
        )
75
      }
76
      case 'document': {
77
        return (
78
          <>
79
            <FeedDescription description={description} />
80
            <a href={document} target='_blank' rel='noreferrer'>
81
              <img className='pdf' src='/images/extension/pdf.png' alt='pdf' />
82
            </a>
83
          </>
84
        )
85
      }
86
      case 'fast-survey': {
87
        return (
88
          <SurveyForm
89
            active={description.active}
90
            question={description.question}
91
            answers={answers}
92
            votes={votes}
93
            time={description.time_remaining}
94
            voteUrl={voteUrl}
95
            voted={description.voted}
96
          />
97
        )
98
      }
99
      case 'shared': {
100
        return (
101
          <>
102
            <FeedDescription description={description} />
103
            <Widget>
104
              <Widget.Header
105
                avatar={sharedImage}
106
                title={sharedName}
107
                subheader={sharedTimeElapse}
108
              />
109
              <Widget.Body>
110
                {renderContent({
111
                  type: sharedContentType,
112
                  description: sharedFeedDescription,
113
                  document: sharedFileDocument,
114
                  image: sharedFileImage,
115
                  video: sharedFileVideo,
116
                  imagePreview: sharedFileImagePreview
117
                })}
118
              </Widget.Body>
119
            </Widget>
120
          </>
121
        )
122
      }
123
      default:
2841 stevensc 124
        return <FeedDescription description={description} />
2838 stevensc 125
    }
126
  }
127
 
128
  return (
129
    <Widget.Body
130
      styles={{
131
        padding: 0,
132
        '& > *:not(img, video)': {
133
          padding: '0 1rem'
134
        },
135
        '& > p, & > span': {
136
          fontSize: '1rem',
137
          wordWrap: 'break-word',
138
          fontWeight: 'normal'
139
        },
140
        '& > img, & > video ': {
141
          width: '100%',
142
          maxHeight: '600px',
143
          objectFit: 'contain'
144
        }
145
      }}
146
    >
147
      {renderContent({
148
        description,
149
        image,
150
        document,
151
        video,
152
        imagePreview,
153
        voteUrl,
2841 stevensc 154
        type: sharedName ? 'shared' : contentType,
2838 stevensc 155
        sharedName,
156
        sharedImage,
157
        sharedTimeElapse
158
      })}
159
    </Widget.Body>
160
  )
161
}