Proyectos de Subversion LeadersLinked - SPA

Rev

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