Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 775 | Rev 777 | 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 { CommentForm, CommentsList } from '../../components/feed/CommentSection'
8
import { Container, Grid } from '@mui/material'
735 stevensc 9
import parse from 'html-react-parser'
5 stevensc 10
 
735 stevensc 11
import TungstenIcon from '@mui/icons-material/Tungsten'
12
import RecommendIcon from '@mui/icons-material/Recommend'
13
import FavoriteIcon from '@mui/icons-material/FavoriteTwoTone'
14
import SendOutlinedIcon from '@mui/icons-material/SendOutlined'
15
import ChatOutlinedIcon from '@mui/icons-material/ChatOutlined'
16
import EmojiEmotionsIcon from '@mui/icons-material/EmojiEmotions'
17
import VolunteerActivismIcon from '@mui/icons-material/VolunteerActivism'
5 stevensc 18
 
735 stevensc 19
import HomeNews from '../../components/widgets/default/HomeNews'
20
import InputOption from '../../components/dashboard/linkedin/action-button/InputOption'
21
import withExternalShare from '../../components/dashboard/linkedin/withExternalShare'
22
import Paraphrase from '../../components/UI/Paraphrase'
23
import StyledContainer from '../../components/widgets/WidgetLayout'
24
import withReactions from '../../hocs/withReaction'
25
import MobileShare from '../../components/dashboard/linkedin/mobile-share/MobileShare'
520 stevensc 26
 
5 stevensc 27
const PostViewPage = () => {
735 stevensc 28
  const [post, setPost] = useState({})
29
  const [totalSends, setTotalSends] = useState(0)
30
  const [reactions, setReactions] = useState([])
31
  const [myReaction, setMyReaction] = useState('')
32
  const [totalReactions, setTotalReactions] = useState(0)
33
  const [isMobile, setIsMobile] = useState(false)
34
  const [comments, setComments] = useState([])
35
  const [showComments, setShowComments] = useState(false)
740 stevensc 36
  const { pathname } = useLocation()
735 stevensc 37
  const labels = useSelector(({ intl }) => intl.labels)
38
  const dispatch = useDispatch()
5 stevensc 39
 
40
  const reactionsOptions = [
41
    {
735 stevensc 42
      type: 'r',
43
      icon: <RecommendIcon style={{ color: '#7405f9' }} />
5 stevensc 44
    },
45
    {
735 stevensc 46
      type: 's',
47
      icon: <VolunteerActivismIcon style={{ color: '#6495ED' }} />
5 stevensc 48
    },
49
    {
735 stevensc 50
      type: 'l',
51
      icon: <FavoriteIcon style={{ color: '#DF704D' }} />
5 stevensc 52
    },
53
    {
735 stevensc 54
      type: 'i',
5 stevensc 55
      icon: (
56
        <TungstenIcon
735 stevensc 57
          style={{ color: '#F5BB5C', transform: 'rotate(180deg)' }}
5 stevensc 58
        />
735 stevensc 59
      )
5 stevensc 60
    },
61
    {
735 stevensc 62
      type: 'f',
63
      icon: <EmojiEmotionsIcon style={{ color: '#FF7F50' }} />
64
    }
65
  ]
5 stevensc 66
 
67
  const displayCommentSection = () => {
735 stevensc 68
    setShowComments(!showComments)
69
  }
5 stevensc 70
 
71
  const getComments = () => {
72
    axios.get(post.comments_url).then((response) => {
735 stevensc 73
      const { data, success } = response.data
5 stevensc 74
 
75
      if (!success) {
76
        const errorMessage =
735 stevensc 77
          typeof data === 'string' ? data : 'Error interno. Intente más tarde.'
5 stevensc 78
 
735 stevensc 79
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
80
        return
5 stevensc 81
      }
82
 
735 stevensc 83
      setComments(data)
84
    })
85
  }
5 stevensc 86
 
87
  const handleExternalShare = (value) => {
735 stevensc 88
    setTotalSends(value)
89
  }
5 stevensc 90
 
91
  const ExternalShareButton = withExternalShare(
92
    InputOption,
520 stevensc 93
    post.share_external_url
735 stevensc 94
  )
5 stevensc 95
 
769 stevensc 96
  const ReactionButton = withReactions(InputOption)
639 stevensc 97
 
735 stevensc 98
  const addComment = (comment) => {
99
    const formData = new FormData()
100
    formData.append('comment', comment)
5 stevensc 101
 
102
    axios.post(post.comments_add_url, formData).then((response) => {
735 stevensc 103
      const { success, data } = response.data
5 stevensc 104
 
105
      if (!success) {
106
        const errorMessage =
735 stevensc 107
          typeof data === 'string' ? data : 'Error interno. Intente más tarde.'
5 stevensc 108
 
735 stevensc 109
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
110
        return
5 stevensc 111
      }
112
 
735 stevensc 113
      setComments((prevMessages) => [...prevMessages, data])
114
    })
115
  }
5 stevensc 116
 
117
  const deleteComment = (commentUnique, deleteCommentUrl) => {
118
    axios
119
      .post(deleteCommentUrl)
120
      .then((response) => {
735 stevensc 121
        const { success, data } = response.data
5 stevensc 122
 
123
        if (!success) {
124
          const errorMessage =
735 stevensc 125
            typeof data === 'string'
5 stevensc 126
              ? data
735 stevensc 127
              : 'Error interno. Intente más tarde.'
5 stevensc 128
 
735 stevensc 129
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
130
          return
5 stevensc 131
        }
132
 
133
        setComments((prevComments) =>
134
          prevComments.filter((comment) => comment.unique !== commentUnique)
735 stevensc 135
        )
136
        dispatch(addNotification({ style: 'success', msg: data }))
5 stevensc 137
      })
138
      .catch((error) => {
735 stevensc 139
        dispatch(addNotification({ style: 'danger', msg: error }))
140
        throw new Error(error)
141
      })
142
  }
5 stevensc 143
 
144
  useEffect(() => {
145
    getBackendVars(pathname)
146
      .then((post) => {
735 stevensc 147
        setMyReaction(post.my_reaction)
148
        setTotalSends(post.total_share_external)
149
        setPost(post)
5 stevensc 150
      })
520 stevensc 151
      .catch(() => {
5 stevensc 152
        dispatch(
153
          addNotification({
735 stevensc 154
            style: 'danger',
155
            message: 'Error interno. Por favor, inténtelo de nuevo más tarde.'
5 stevensc 156
          })
735 stevensc 157
        )
158
      })
159
  }, [pathname])
5 stevensc 160
 
161
  useEffect(() => {
162
    if (showComments && !comments.length) {
735 stevensc 163
      getComments()
5 stevensc 164
    }
735 stevensc 165
  }, [showComments])
5 stevensc 166
 
167
  useEffect(() => {
168
    const feedReactions = reactions.reduce(
169
      (acc, reaction) => acc + Number(reaction.total),
170
 
735 stevensc 171
    )
5 stevensc 172
 
735 stevensc 173
    setTotalReactions(feedReactions)
174
  }, [reactions])
5 stevensc 175
 
639 stevensc 176
  useEffect(() => {
735 stevensc 177
    const ua = navigator.userAgent.toLowerCase()
178
    const isAndroid = ua.includes('android')
639 stevensc 179
 
180
    if (isAndroid) {
735 stevensc 181
      setIsMobile(true)
639 stevensc 182
    }
735 stevensc 183
  }, [])
639 stevensc 184
 
5 stevensc 185
  return (
740 stevensc 186
    <Container as='main' className='px-0'>
187
      <Grid container spacing={2}>
188
        <Grid item xs={12} md={8}>
639 stevensc 189
          <StyledContainer>
769 stevensc 190
            <img
191
              src={post.image}
192
              style={{
193
                width: '100%',
194
                maxHeight: '450px',
195
                objectFit: 'contain'
196
              }}
197
            />
639 stevensc 198
            <StyledContainer.Body>
769 stevensc 199
              <h2>{post.title}</h2>
776 stevensc 200
              <Paraphrase>{post.description}</Paraphrase>
5 stevensc 201
              {post.file && (
202
                <a href={post.file} download>
735 stevensc 203
                  <img src='/images/extension/pdf.png' alt='pdf' />
5 stevensc 204
                </a>
205
              )}
639 stevensc 206
            </StyledContainer.Body>
740 stevensc 207
 
735 stevensc 208
            <div className='d-flex justify-content-between align-items-center px-3'>
209
              <div className='reactions-counter'>
5 stevensc 210
                {reactionsOptions
211
                  .filter((option) =>
212
                    reactions.find(({ reaction }) => reaction === option.type)
213
                  )
214
                  .map((reaction) => reaction.icon)}
215
                <span>{totalReactions} reacciones</span>
216
              </div>
217
              {!!totalSends && (
218
                <span>{`${totalSends} ${labels.sends?.toLowerCase()}`}</span>
219
              )}
220
            </div>
740 stevensc 221
 
639 stevensc 222
            <StyledContainer.Actions>
769 stevensc 223
              <ReactionButton
224
                currentReaction={myReaction}
225
                saveUrl={post.save_reaction_url}
226
                deleteUrl={post.delete_reaction_url}
227
                onReaction={({ reactions, currentReaction }) => {
228
                  setReactions(reactions)
229
                  setMyReaction(currentReaction)
230
                }}
231
              />
5 stevensc 232
              <InputOption
520 stevensc 233
                icon={ChatOutlinedIcon}
740 stevensc 234
                icGridor='gray'
639 stevensc 235
                label={labels.comment}
5 stevensc 236
                onClick={displayCommentSection}
237
              />
774 stevensc 238
 
776 stevensc 239
              <ExternalShareButton
240
                icon={SendOutlinedIcon}
241
                iconColor='gray'
242
                label={labels.send}
243
                shareUrl={post.share_increment_external_counter_url}
244
                setValue={handleExternalShare}
245
              />
639 stevensc 246
            </StyledContainer.Actions>
740 stevensc 247
 
5 stevensc 248
            {showComments && (
735 stevensc 249
              <div className='px-3 pb-2'>
5 stevensc 250
                <CommentForm onSubmit={addComment} />
251
                <CommentsList comments={comments} onDelete={deleteComment} />
735 stevensc 252
              </div>
5 stevensc 253
            )}
639 stevensc 254
          </StyledContainer>
740 stevensc 255
        </Grid>
256
 
257
        <Grid item xs={12} md={4}>
776 stevensc 258
          <HomeNews currentPost={post.uuid} />
740 stevensc 259
        </Grid>
260
      </Grid>
5 stevensc 261
    </Container>
735 stevensc 262
  )
263
}
5 stevensc 264
 
735 stevensc 265
export default PostViewPage