Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
735 stevensc 1
import React, { useEffect, useState } from 'react'
2
import { axios } from '../../utils'
3
import { useLocation } from 'react-router-dom'
4
import { getBackendVars } from '../../services/backendVars'
5
import { addNotification } from '../../redux/notification/notification.actions'
6
import { useDispatch, useSelector } from 'react-redux'
740 stevensc 7
import { Container, Grid } from '@mui/material'
735 stevensc 8
import parse from 'html-react-parser'
5 stevensc 9
 
735 stevensc 10
import SendOutlinedIcon from '@mui/icons-material/SendOutlined'
11
import ChatOutlinedIcon from '@mui/icons-material/ChatOutlined'
5 stevensc 12
 
735 stevensc 13
import HomeNews from '../../components/widgets/default/HomeNews'
14
import withExternalShare from '../../components/dashboard/linkedin/withExternalShare'
15
import Paraphrase from '../../components/UI/Paraphrase'
1507 stevensc 16
import WidgetWrapper from '../../components/widgets/WidgetLayout'
735 stevensc 17
import withReactions from '../../hocs/withReaction'
18
import MobileShare from '../../components/dashboard/linkedin/mobile-share/MobileShare'
1650 stevensc 19
import CommentForm from '@app/components/dashboard/linkedin/comments/comment-form'
20
import CommentsList from '@app/components/dashboard/linkedin/comments/comment-list'
2162 stevensc 21
import Button from '@app/components/UI/buttons/Buttons'
22
import FeedReactions from '@app/components/dashboard/linkedin/feed/FeedReactions'
2169 stevensc 23
import PostFile from '@app/components/post/PostFile'
2187 stevensc 24
import useMobile from '@app/hooks/useMobile'
520 stevensc 25
 
5 stevensc 26
const PostViewPage = () => {
735 stevensc 27
  const [post, setPost] = useState({})
28
  const [totalSends, setTotalSends] = useState(0)
29
  const [reactions, setReactions] = useState([])
30
  const [myReaction, setMyReaction] = useState('')
31
  const [comments, setComments] = useState([])
32
  const [showComments, setShowComments] = useState(false)
2187 stevensc 33
  const isMobile = useMobile()
740 stevensc 34
  const { pathname } = useLocation()
735 stevensc 35
  const labels = useSelector(({ intl }) => intl.labels)
36
  const dispatch = useDispatch()
5 stevensc 37
 
38
  const displayCommentSection = () => {
735 stevensc 39
    setShowComments(!showComments)
40
  }
5 stevensc 41
 
2189 stevensc 42
  const getComments = (url) => {
43
    axios.get(url).then((response) => {
735 stevensc 44
      const { data, success } = response.data
5 stevensc 45
 
46
      if (!success) {
47
        const errorMessage =
735 stevensc 48
          typeof data === 'string' ? data : 'Error interno. Intente más tarde.'
5 stevensc 49
 
735 stevensc 50
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
51
        return
5 stevensc 52
      }
53
 
735 stevensc 54
      setComments(data)
55
    })
56
  }
5 stevensc 57
 
58
  const handleExternalShare = (value) => {
735 stevensc 59
    setTotalSends(value)
60
  }
5 stevensc 61
 
2162 stevensc 62
  const ExternalShareButton = withExternalShare(Button, post.share_external_url)
5 stevensc 63
 
2162 stevensc 64
  const ReactionButton = withReactions(Button)
639 stevensc 65
 
735 stevensc 66
  const addComment = (comment) => {
1979 stevensc 67
    const formData = new FormData()
68
    formData.append('comment', comment)
69
 
70
    axios.post(post.comments_add_url, formData).then((response) => {
735 stevensc 71
      const { success, data } = response.data
5 stevensc 72
 
73
      if (!success) {
74
        const errorMessage =
735 stevensc 75
          typeof data === 'string' ? data : 'Error interno. Intente más tarde.'
5 stevensc 76
 
735 stevensc 77
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
78
        return
5 stevensc 79
      }
80
 
735 stevensc 81
      setComments((prevMessages) => [...prevMessages, data])
82
    })
83
  }
5 stevensc 84
 
85
  const deleteComment = (commentUnique, deleteCommentUrl) => {
86
    axios
87
      .post(deleteCommentUrl)
88
      .then((response) => {
735 stevensc 89
        const { success, data } = response.data
5 stevensc 90
 
91
        if (!success) {
92
          const errorMessage =
735 stevensc 93
            typeof data === 'string'
5 stevensc 94
              ? data
735 stevensc 95
              : 'Error interno. Intente más tarde.'
5 stevensc 96
 
735 stevensc 97
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
98
          return
5 stevensc 99
        }
100
 
101
        setComments((prevComments) =>
102
          prevComments.filter((comment) => comment.unique !== commentUnique)
735 stevensc 103
        )
104
        dispatch(addNotification({ style: 'success', msg: data }))
5 stevensc 105
      })
106
      .catch((error) => {
735 stevensc 107
        dispatch(addNotification({ style: 'danger', msg: error }))
108
        throw new Error(error)
109
      })
110
  }
5 stevensc 111
 
112
  useEffect(() => {
113
    getBackendVars(pathname)
114
      .then((post) => {
735 stevensc 115
        setMyReaction(post.my_reaction)
116
        setTotalSends(post.total_share_external)
2165 stevensc 117
        setReactions(post.reactions)
735 stevensc 118
        setPost(post)
5 stevensc 119
      })
520 stevensc 120
      .catch(() => {
5 stevensc 121
        dispatch(
122
          addNotification({
735 stevensc 123
            style: 'danger',
124
            message: 'Error interno. Por favor, inténtelo de nuevo más tarde.'
5 stevensc 125
          })
735 stevensc 126
        )
127
      })
128
  }, [pathname])
5 stevensc 129
 
130
  useEffect(() => {
2189 stevensc 131
    if (post.comments_url) getComments(post.comments_url)
2187 stevensc 132
  }, [post])
5 stevensc 133
 
134
  return (
740 stevensc 135
    <Container as='main' className='px-0'>
136
      <Grid container spacing={2}>
137
        <Grid item xs={12} md={8}>
1507 stevensc 138
          <WidgetWrapper>
769 stevensc 139
            <img
140
              src={post.image}
141
              style={{
142
                width: '100%',
143
                maxHeight: '450px',
144
                objectFit: 'contain'
145
              }}
146
            />
1507 stevensc 147
            <WidgetWrapper.Body>
769 stevensc 148
              <h2>{post.title}</h2>
776 stevensc 149
              <Paraphrase>{post.description}</Paraphrase>
2169 stevensc 150
              <PostFile file={post.file} type={post.type} />
1507 stevensc 151
            </WidgetWrapper.Body>
740 stevensc 152
 
735 stevensc 153
            <div className='d-flex justify-content-between align-items-center px-3'>
2162 stevensc 154
              <FeedReactions
155
                reactions={reactions}
156
                reactionsUrl={post.reactions_url}
157
              />
158
 
5 stevensc 159
              {!!totalSends && (
160
                <span>{`${totalSends} ${labels.sends?.toLowerCase()}`}</span>
161
              )}
162
            </div>
740 stevensc 163
 
1507 stevensc 164
            <WidgetWrapper.Actions>
769 stevensc 165
              <ReactionButton
2164 stevensc 166
                currentReactionType={myReaction}
769 stevensc 167
                saveUrl={post.save_reaction_url}
168
                deleteUrl={post.delete_reaction_url}
2162 stevensc 169
                onReaction={({ reactions }, currentReaction) => {
769 stevensc 170
                  setReactions(reactions)
171
                  setMyReaction(currentReaction)
172
                }}
173
              />
774 stevensc 174
 
2162 stevensc 175
              <Button onClick={displayCommentSection}>
176
                <ChatOutlinedIcon style={{ color: 'gray' }} />
177
                {labels.comment}
178
              </Button>
179
 
777 stevensc 180
              {!isMobile ? (
181
                <ExternalShareButton
2188 stevensc 182
                  shorterUrl={post.share_increment_external_counter_url}
777 stevensc 183
                  setValue={handleExternalShare}
2188 stevensc 184
                >
185
                  <SendOutlinedIcon style={{ color: 'gray' }} />
186
                  {labels.send}
187
                </ExternalShareButton>
777 stevensc 188
              ) : (
189
                <MobileShare
190
                  shareData={{
191
                    title: 'Leaders Linked',
192
                    text: parse(post.description ?? ''),
193
                    url: post.share_external_url
194
                  }}
195
                >
196
                  <SendOutlinedIcon />
197
                  {labels.send}
198
                </MobileShare>
199
              )}
1507 stevensc 200
            </WidgetWrapper.Actions>
740 stevensc 201
 
5 stevensc 202
            {showComments && (
735 stevensc 203
              <div className='px-3 pb-2'>
5 stevensc 204
                <CommentForm onSubmit={addComment} />
205
                <CommentsList comments={comments} onDelete={deleteComment} />
735 stevensc 206
              </div>
5 stevensc 207
            )}
1507 stevensc 208
          </WidgetWrapper>
740 stevensc 209
        </Grid>
210
 
211
        <Grid item xs={12} md={4}>
776 stevensc 212
          <HomeNews currentPost={post.uuid} />
740 stevensc 213
        </Grid>
214
      </Grid>
5 stevensc 215
    </Container>
735 stevensc 216
  )
217
}
5 stevensc 218
 
2162 stevensc 219
export const renderContent = ({ type, file }) => {
220
  switch (type) {
221
    case 'video': {
2165 stevensc 222
      return (
223
        <video src={file} controls preload='none' controlsList='nodownload' />
224
      )
2162 stevensc 225
    }
226
 
227
    case 'image': {
228
      return <img src={file} />
229
    }
230
 
231
    case 'document': {
232
      return (
233
        <a href={file} target='_blank' rel='noreferrer'>
234
          <img className='pdf' src='/images/extension/pdf.png' alt='pdf' />
235
        </a>
236
      )
237
    }
2169 stevensc 238
 
2180 stevensc 239
    case 'audio': {
240
      return (
241
        <audio controls>
242
          <source src={file} />
243
        </audio>
244
      )
245
    }
246
 
2169 stevensc 247
    default: {
248
      return (
249
        <a href={file} target='_blank' rel='noreferrer'>
250
          <img className='pdf' src='/images/extension/pdf.png' alt='pdf' />
251
        </a>
252
      )
253
    }
2162 stevensc 254
  }
255
}
256
 
735 stevensc 257
export default PostViewPage