Proyectos de Subversion LeadersLinked - SPA

Rev

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