Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1976 | Rev 2162 | 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) => {
1979 stevensc 100
    const formData = new FormData()
101
    formData.append('comment', comment)
102
 
103
    axios.post(post.comments_add_url, formData).then((response) => {
735 stevensc 104
      const { success, data } = response.data
5 stevensc 105
 
106
      if (!success) {
107
        const errorMessage =
735 stevensc 108
          typeof data === 'string' ? data : 'Error interno. Intente más tarde.'
5 stevensc 109
 
735 stevensc 110
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
111
        return
5 stevensc 112
      }
113
 
735 stevensc 114
      setComments((prevMessages) => [...prevMessages, data])
115
    })
116
  }
5 stevensc 117
 
118
  const deleteComment = (commentUnique, deleteCommentUrl) => {
119
    axios
120
      .post(deleteCommentUrl)
121
      .then((response) => {
735 stevensc 122
        const { success, data } = response.data
5 stevensc 123
 
124
        if (!success) {
125
          const errorMessage =
735 stevensc 126
            typeof data === 'string'
5 stevensc 127
              ? data
735 stevensc 128
              : 'Error interno. Intente más tarde.'
5 stevensc 129
 
735 stevensc 130
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
131
          return
5 stevensc 132
        }
133
 
134
        setComments((prevComments) =>
135
          prevComments.filter((comment) => comment.unique !== commentUnique)
735 stevensc 136
        )
137
        dispatch(addNotification({ style: 'success', msg: data }))
5 stevensc 138
      })
139
      .catch((error) => {
735 stevensc 140
        dispatch(addNotification({ style: 'danger', msg: error }))
141
        throw new Error(error)
142
      })
143
  }
5 stevensc 144
 
145
  useEffect(() => {
146
    getBackendVars(pathname)
147
      .then((post) => {
735 stevensc 148
        setMyReaction(post.my_reaction)
149
        setTotalSends(post.total_share_external)
150
        setPost(post)
5 stevensc 151
      })
520 stevensc 152
      .catch(() => {
5 stevensc 153
        dispatch(
154
          addNotification({
735 stevensc 155
            style: 'danger',
156
            message: 'Error interno. Por favor, inténtelo de nuevo más tarde.'
5 stevensc 157
          })
735 stevensc 158
        )
159
      })
160
  }, [pathname])
5 stevensc 161
 
162
  useEffect(() => {
163
    if (showComments && !comments.length) {
735 stevensc 164
      getComments()
5 stevensc 165
    }
735 stevensc 166
  }, [showComments])
5 stevensc 167
 
168
  useEffect(() => {
169
    const feedReactions = reactions.reduce(
170
      (acc, reaction) => acc + Number(reaction.total),
171
 
735 stevensc 172
    )
5 stevensc 173
 
735 stevensc 174
    setTotalReactions(feedReactions)
175
  }, [reactions])
5 stevensc 176
 
639 stevensc 177
  useEffect(() => {
735 stevensc 178
    const ua = navigator.userAgent.toLowerCase()
179
    const isAndroid = ua.includes('android')
639 stevensc 180
 
181
    if (isAndroid) {
735 stevensc 182
      setIsMobile(true)
639 stevensc 183
    }
735 stevensc 184
  }, [])
639 stevensc 185
 
5 stevensc 186
  return (
740 stevensc 187
    <Container as='main' className='px-0'>
188
      <Grid container spacing={2}>
189
        <Grid item xs={12} md={8}>
1507 stevensc 190
          <WidgetWrapper>
769 stevensc 191
            <img
192
              src={post.image}
193
              style={{
194
                width: '100%',
195
                maxHeight: '450px',
196
                objectFit: 'contain'
197
              }}
198
            />
1507 stevensc 199
            <WidgetWrapper.Body>
769 stevensc 200
              <h2>{post.title}</h2>
776 stevensc 201
              <Paraphrase>{post.description}</Paraphrase>
5 stevensc 202
              {post.file && (
203
                <a href={post.file} download>
735 stevensc 204
                  <img src='/images/extension/pdf.png' alt='pdf' />
5 stevensc 205
                </a>
206
              )}
1507 stevensc 207
            </WidgetWrapper.Body>
740 stevensc 208
 
735 stevensc 209
            <div className='d-flex justify-content-between align-items-center px-3'>
210
              <div className='reactions-counter'>
5 stevensc 211
                {reactionsOptions
212
                  .filter((option) =>
213
                    reactions.find(({ reaction }) => reaction === option.type)
214
                  )
215
                  .map((reaction) => reaction.icon)}
216
                <span>{totalReactions} reacciones</span>
217
              </div>
218
              {!!totalSends && (
219
                <span>{`${totalSends} ${labels.sends?.toLowerCase()}`}</span>
220
              )}
221
            </div>
740 stevensc 222
 
1507 stevensc 223
            <WidgetWrapper.Actions>
769 stevensc 224
              <ReactionButton
225
                currentReaction={myReaction}
226
                saveUrl={post.save_reaction_url}
227
                deleteUrl={post.delete_reaction_url}
228
                onReaction={({ reactions, currentReaction }) => {
229
                  setReactions(reactions)
230
                  setMyReaction(currentReaction)
231
                }}
232
              />
5 stevensc 233
              <InputOption
520 stevensc 234
                icon={ChatOutlinedIcon}
740 stevensc 235
                icGridor='gray'
639 stevensc 236
                label={labels.comment}
5 stevensc 237
                onClick={displayCommentSection}
238
              />
774 stevensc 239
 
777 stevensc 240
              {!isMobile ? (
241
                <ExternalShareButton
242
                  icon={SendOutlinedIcon}
243
                  iconColor='gray'
244
                  label={labels.send}
245
                  shareUrl={post.share_increment_external_counter_url}
246
                  setValue={handleExternalShare}
247
                />
248
              ) : (
249
                <MobileShare
250
                  shareData={{
251
                    title: 'Leaders Linked',
252
                    text: parse(post.description ?? ''),
253
                    url: post.share_external_url
254
                  }}
255
                >
256
                  <SendOutlinedIcon />
257
                  {labels.send}
258
                </MobileShare>
259
              )}
1507 stevensc 260
            </WidgetWrapper.Actions>
740 stevensc 261
 
5 stevensc 262
            {showComments && (
735 stevensc 263
              <div className='px-3 pb-2'>
5 stevensc 264
                <CommentForm onSubmit={addComment} />
265
                <CommentsList comments={comments} onDelete={deleteComment} />
735 stevensc 266
              </div>
5 stevensc 267
            )}
1507 stevensc 268
          </WidgetWrapper>
740 stevensc 269
        </Grid>
270
 
271
        <Grid item xs={12} md={4}>
776 stevensc 272
          <HomeNews currentPost={post.uuid} />
740 stevensc 273
        </Grid>
274
      </Grid>
5 stevensc 275
    </Container>
735 stevensc 276
  )
277
}
5 stevensc 278
 
735 stevensc 279
export default PostViewPage