Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2189 | Rev 2191 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 2189 Rev 2190
Línea 1... Línea -...
1
import React, { useEffect, useState } from 'react'
-
 
2
import { axios } from '../../utils'
1
import React from 'react'
3
import { useLocation } from 'react-router-dom'
2
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'
-
 
7
import { Container, Grid } from '@mui/material'
3
import { Container, Grid } from '@mui/material'
8
import parse from 'html-react-parser'
-
 
Línea 9... Línea -...
9
 
-
 
10
import SendOutlinedIcon from '@mui/icons-material/SendOutlined'
4
 
Línea 11... Línea -...
11
import ChatOutlinedIcon from '@mui/icons-material/ChatOutlined'
-
 
12
 
-
 
13
import HomeNews from '../../components/widgets/default/HomeNews'
-
 
14
import withExternalShare from '../../components/dashboard/linkedin/withExternalShare'
-
 
15
import Paraphrase from '../../components/UI/Paraphrase'
-
 
16
import WidgetWrapper from '../../components/widgets/WidgetLayout'
-
 
17
import withReactions from '../../hocs/withReaction'
-
 
18
import MobileShare from '../../components/dashboard/linkedin/mobile-share/MobileShare'
-
 
19
import CommentForm from '@app/components/dashboard/linkedin/comments/comment-form'
-
 
20
import CommentsList from '@app/components/dashboard/linkedin/comments/comment-list'
-
 
21
import Button from '@app/components/UI/buttons/Buttons'
5
import usePosts from '@app/hooks/usePosts'
22
import FeedReactions from '@app/components/dashboard/linkedin/feed/FeedReactions'
6
 
Línea 23... Línea 7...
23
import PostFile from '@app/components/post/PostFile'
7
import PostCard from '@app/components/post/PostCard'
24
import useMobile from '@app/hooks/useMobile'
-
 
25
 
-
 
26
const PostViewPage = () => {
-
 
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([])
8
import HomeNews from '@app/components/widgets/default/HomeNews'
32
  const [showComments, setShowComments] = useState(false)
-
 
33
  const isMobile = useMobile()
-
 
34
  const { pathname } = useLocation()
-
 
35
  const labels = useSelector(({ intl }) => intl.labels)
-
 
36
  const dispatch = useDispatch()
-
 
37
 
-
 
38
  const displayCommentSection = () => {
-
 
39
    setShowComments(!showComments)
-
 
40
  }
-
 
41
 
-
 
42
  const getComments = (url) => {
-
 
43
    axios.get(url).then((response) => {
-
 
44
      const { data, success } = response.data
-
 
45
 
-
 
46
      if (!success) {
-
 
47
        const errorMessage =
-
 
48
          typeof data === 'string' ? data : 'Error interno. Intente más tarde.'
-
 
49
 
-
 
50
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
-
 
51
        return
-
 
52
      }
-
 
53
 
-
 
54
      setComments(data)
-
 
55
    })
-
 
56
  }
-
 
57
 
-
 
58
  const handleExternalShare = (value) => {
-
 
59
    setTotalSends(value)
-
 
60
  }
-
 
61
 
-
 
62
  const ExternalShareButton = withExternalShare(Button, post.share_external_url)
-
 
63
 
-
 
64
  const ReactionButton = withReactions(Button)
-
 
65
 
-
 
66
  const addComment = (comment) => {
-
 
67
    const formData = new FormData()
-
 
68
    formData.append('comment', comment)
-
 
69
 
-
 
70
    axios.post(post.comments_add_url, formData).then((response) => {
-
 
71
      const { success, data } = response.data
-
 
72
 
-
 
73
      if (!success) {
-
 
74
        const errorMessage =
-
 
75
          typeof data === 'string' ? data : 'Error interno. Intente más tarde.'
-
 
76
 
9
 
77
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
-
 
78
        return
-
 
79
      }
-
 
80
 
-
 
81
      setComments((prevMessages) => [...prevMessages, data])
-
 
82
    })
-
 
83
  }
10
const PostViewPage = () => {
84
 
-
 
85
  const deleteComment = (commentUnique, deleteCommentUrl) => {
-
 
86
    axios
-
 
87
      .post(deleteCommentUrl)
-
 
88
      .then((response) => {
-
 
89
        const { success, data } = response.data
-
 
90
 
-
 
91
        if (!success) {
-
 
92
          const errorMessage =
-
 
93
            typeof data === 'string'
-
 
94
              ? data
-
 
95
              : 'Error interno. Intente más tarde.'
11
  const { pathname } = useLocation()
96
 
-
 
97
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
-
 
98
          return
-
 
99
        }
-
 
100
 
-
 
101
        setComments((prevComments) =>
-
 
102
          prevComments.filter((comment) => comment.unique !== commentUnique)
-
 
103
        )
-
 
104
        dispatch(addNotification({ style: 'success', msg: data }))
-
 
105
      })
-
 
106
      .catch((error) => {
-
 
107
        dispatch(addNotification({ style: 'danger', msg: error }))
-
 
108
        throw new Error(error)
-
 
109
      })
-
 
110
  }
12
  const {
111
 
13
    post,
112
  useEffect(() => {
-
 
113
    getBackendVars(pathname)
-
 
114
      .then((post) => {
-
 
115
        setMyReaction(post.my_reaction)
14
    addComment,
116
        setTotalSends(post.total_share_external)
-
 
117
        setReactions(post.reactions)
-
 
118
        setPost(post)
-
 
119
      })
-
 
120
      .catch(() => {
-
 
121
        dispatch(
-
 
122
          addNotification({
-
 
123
            style: 'danger',
-
 
124
            message: 'Error interno. Por favor, inténtelo de nuevo más tarde.'
-
 
125
          })
15
    updateTotalShare,
126
        )
-
 
127
      })
-
 
128
  }, [pathname])
-
 
129
 
-
 
Línea 130... Línea 16...
130
  useEffect(() => {
16
    updateMyReaction,
131
    if (post.comments_url) getComments(post.comments_url)
17
    updateReactions
132
  }, [post])
18
  } = usePosts(pathname)
133
 
19
 
134
  return (
20
  return (
135
    <Container as='main' className='px-0'>
-
 
136
      <Grid container spacing={2}>
21
    <Container as='main' className='px-0'>
137
        <Grid item xs={12} md={8}>
-
 
138
          <WidgetWrapper>
-
 
139
            <img
-
 
140
              src={post.image}
22
      <Grid container spacing={2}>
141
              style={{
-
 
142
                width: '100%',
-
 
143
                maxHeight: '450px',
-
 
144
                objectFit: 'contain'
-
 
145
              }}
-
 
146
            />
-
 
147
            <WidgetWrapper.Body>
-
 
148
              <h2>{post.title}</h2>
-
 
149
              <Paraphrase>{post.description}</Paraphrase>
-
 
150
              <PostFile file={post.file} type={post.type} />
-
 
151
            </WidgetWrapper.Body>
-
 
152
 
23
        <Grid item xs={12} md={8}>
153
            <div className='d-flex justify-content-between align-items-center px-3'>
-
 
154
              <FeedReactions
-
 
155
                reactions={reactions}
-
 
156
                reactionsUrl={post.reactions_url}
-
 
157
              />
-
 
158
 
-
 
159
              {!!totalSends && (
-
 
160
                <span>{`${totalSends} ${labels.sends?.toLowerCase()}`}</span>
-
 
161
              )}
-
 
162
            </div>
24
          <PostCard
163
 
-
 
164
            <WidgetWrapper.Actions>
-
 
165
              <ReactionButton
-
 
166
                currentReactionType={myReaction}
25
            post={post}
167
                saveUrl={post.save_reaction_url}
-
 
168
                deleteUrl={post.delete_reaction_url}
-
 
169
                onReaction={({ reactions }, currentReaction) => {
26
            addComment={addComment}
170
                  setReactions(reactions)
-
 
171
                  setMyReaction(currentReaction)
-
 
172
                }}
-
 
173
              />
-
 
174
 
-
 
175
              <Button onClick={displayCommentSection}>
-
 
176
                <ChatOutlinedIcon style={{ color: 'gray' }} />
-
 
177
                {labels.comment}
-
 
178
              </Button>
-
 
179
 
-
 
180
              {!isMobile ? (
-
 
181
                <ExternalShareButton
-
 
182
                  shorterUrl={post.share_increment_external_counter_url}
-
 
183
                  setValue={handleExternalShare}
-
 
184
                >
-
 
185
                  <SendOutlinedIcon style={{ color: 'gray' }} />
-
 
186
                  {labels.send}
-
 
187
                </ExternalShareButton>
-
 
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
              )}
-
 
200
            </WidgetWrapper.Actions>
-
 
201
 
-
 
202
            {showComments && (
-
 
203
              <div className='px-3 pb-2'>
-
 
204
                <CommentForm onSubmit={addComment} />
-
 
205
                <CommentsList comments={comments} onDelete={deleteComment} />
27
            updateTotalShare={updateTotalShare}
Línea 206... Línea 28...
206
              </div>
28
            updateMyReaction={updateMyReaction}
207
            )}
29
            updateReactions={updateReactions}
208
          </WidgetWrapper>
30
          />