Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7231 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
7065 stevensc 1
import React, { useEffect, useState } from 'react'
7066 stevensc 2
import { axios } from '../../utils'
7062 stevensc 3
import { useParams } from 'react-router-dom'
7066 stevensc 4
import { Col, Container, Row } from 'react-bootstrap'
5
import { addNotification } from '../../redux/notification/notification.actions'
6
import { useDispatch, useSelector } from 'react-redux'
7
import {
8
  Card,
9
  CardActions,
10
  CardContent,
11
  CardMedia,
12
  Typography,
13
} from '@mui/material'
14
import parse from 'html-react-parser'
7065 stevensc 15
import styled from 'styled-components'
7066 stevensc 16
import FileDownloadIcon from '@mui/icons-material/FileDownload'
7062 stevensc 17
 
7066 stevensc 18
import ReactionsButton from '../../components/UI/buttons/ReactionsButton'
7230 stevensc 19
import { CommentForm, CommentsList } from '../../components/feed/CommentSection'
7066 stevensc 20
 
7065 stevensc 21
const KnowledgeCard = styled(Card)`
22
  background-color: var(--bg-color);
23
  border-radius: var(--border-radius);
7068 stevensc 24
  overflow: visible;
7065 stevensc 25
  height: fit-content;
7068 stevensc 26
 
27
  & > img {
28
    border-top-right-radius: var(--border-radius);
29
    border-top-left-radius: var(--border-radius);
30
  }
7065 stevensc 31
`
32
 
7066 stevensc 33
const KnowledgeActions = styled(CardActions)`
7067 stevensc 34
  & > * {
7066 stevensc 35
    flex: 1;
36
    max-width: calc(100% / 3);
7231 stevensc 37
    position: relative;
7066 stevensc 38
  }
39
`
40
 
7062 stevensc 41
const KnowledgeViewPage = () => {
7080 stevensc 42
  const [comments, setComments] = useState([])
7065 stevensc 43
  const [knowledge, setKnowledge] = useState({
44
    category: '',
45
    title: '',
46
    description: '',
47
    link: null,
48
    image: '',
49
    attachment: '',
50
    reaction: '',
51
    routeComments: '',
52
    routeCommentAdd: '',
53
    routeSaveReaction: '',
54
    routeDeleteReaction: '',
55
  })
56
  const labels = useSelector(({ intl }) => intl.labels)
57
  const dispatch = useDispatch()
7062 stevensc 58
  const { uuid } = useParams()
59
 
7230 stevensc 60
  const addComment = ({ comment }) => {
61
    const formData = new FormData()
62
    formData.append('comment', comment)
63
 
64
    axios.post(knowledge.routeCommentAdd, formData).then((response) => {
65
      const { success, data } = response.data
66
 
67
      if (!success) {
68
        const errorMessage =
69
          typeof data === 'string' ? data : 'Error interno. Intente más tarde.'
70
 
71
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
72
        return
73
      }
74
 
75
      setComments((prevMessages) => [...prevMessages, data])
76
    })
77
  }
78
 
79
  const deleteComment = (commentUnique, deleteCommentUrl) => {
80
    axios
81
      .post(deleteCommentUrl)
82
      .then((response) => {
83
        const { success, data } = response.data
84
 
85
        if (!success) {
86
          const errorMessage =
87
            typeof data === 'string'
88
              ? data
89
              : 'Error interno. Intente más tarde.'
90
 
91
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
92
          return
93
        }
94
 
95
        setComments((prevComments) =>
96
          prevComments.filter((comment) => comment.unique !== commentUnique)
97
        )
98
        dispatch(addNotification({ style: 'success', msg: data }))
99
      })
100
      .catch((error) => {
101
        dispatch(addNotification({ style: 'danger', msg: error }))
102
        throw new Error(error)
103
      })
104
  }
105
 
7062 stevensc 106
  useEffect(() => {
107
    axios
108
      .get(`/knowledge-area/view/${uuid}`, {
109
        headers: {
110
          'Content-Type': 'application/json',
111
        },
112
      })
7065 stevensc 113
      .then((response) => {
114
        const { data, success } = response.data
115
        if (!success) {
116
          const errorMessage =
7201 stevensc 117
            typeof data === 'string'
118
              ? data
119
              : 'Error interno. Por favor, inténtelo de nuevo más tarde.'
7065 stevensc 120
 
121
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
122
          return
123
        }
124
 
125
        setKnowledge(data)
126
      })
127
      .catch((err) => {
128
        dispatch(
129
          addNotification({
130
            style: 'danger',
7201 stevensc 131
            msg: 'Error interno. Por favor, inténtelo de nuevo más tarde.',
7065 stevensc 132
          })
133
        )
134
        throw new Error(err)
135
      })
7062 stevensc 136
  }, [uuid])
137
 
7080 stevensc 138
  useEffect(() => {
139
    if (!knowledge.routeComments) return
140
 
141
    axios
142
      .get(knowledge.routeComments)
143
      .then((response) => {
144
        const { data, success } = response.data
145
 
146
        if (!success) {
147
          const errorMessage =
7201 stevensc 148
            typeof data === 'string'
149
              ? data
150
              : 'Error interno. Por favor, inténtelo de nuevo más tarde.'
7080 stevensc 151
 
152
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
153
          return
154
        }
155
 
156
        setComments(data)
157
      })
158
      .catch((err) => {
159
        dispatch(
160
          addNotification({
161
            style: 'danger',
7201 stevensc 162
            msg: 'Error interno. Por favor, inténtelo de nuevo más tarde.',
7080 stevensc 163
          })
164
        )
165
        throw new Error(err)
166
      })
167
  }, [knowledge])
168
 
7065 stevensc 169
  return (
170
    <Container as="section">
7082 stevensc 171
      <h2 className="text-center mb-3">{labels.knowledge_area_title}</h2>
7066 stevensc 172
      <Row>
173
        <Col className="mx-auto" md="8">
174
          <KnowledgeCard>
175
            <CardMedia
176
              component="img"
7067 stevensc 177
              height="250"
7066 stevensc 178
              image={knowledge.image}
179
              alt={`${knowledge.title} image`}
180
            />
181
            <CardContent>
182
              <Typography variant="h5">{knowledge.title}</Typography>
183
              <Typography variant="subtitle1" color="text.secondary">
184
                {knowledge.category}
185
              </Typography>
186
              {knowledge.description && parse(knowledge.description)}
187
            </CardContent>
7067 stevensc 188
            <KnowledgeActions>
7068 stevensc 189
              <ReactionsButton
7232 stevensc 190
                className="btn feed__share-option"
7068 stevensc 191
                currentReaction={knowledge.reaction}
192
                withLabel
193
                deleteUrl={knowledge.routeDeleteReaction}
7101 stevensc 194
                saveUrl={knowledge.routeSaveReaction}
7068 stevensc 195
              />
196
 
7066 stevensc 197
              {knowledge.attachment && (
7068 stevensc 198
                <a
199
                  href={knowledge.attachment}
200
                  download
7231 stevensc 201
                  className="btn feed__share-option"
7068 stevensc 202
                >
203
                  <FileDownloadIcon />
204
                  {labels.knowledge_area_download_attachment}
205
                </a>
7066 stevensc 206
              )}
207
              {knowledge.link && (
7068 stevensc 208
                <a
209
                  href={knowledge.link}
7231 stevensc 210
                  className="btn feed__share-option"
7068 stevensc 211
                  target="_blank"
212
                  rel="noreferrer"
213
                >
214
                  {labels.knowledge_area_go_to_link}
215
                </a>
7066 stevensc 216
              )}
7081 stevensc 217
            </KnowledgeActions>
218
            <div className="px-3">
7230 stevensc 219
              <CommentForm onSubmit={addComment} />
220
              <CommentsList comments={comments} onDelete={deleteComment} />
7081 stevensc 221
            </div>
7066 stevensc 222
          </KnowledgeCard>
223
        </Col>
224
      </Row>
7065 stevensc 225
    </Container>
226
  )
7062 stevensc 227
}
228
 
229
export default KnowledgeViewPage