Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7081 | Rev 7101 | Ir a la última revisión | | 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'
7080 stevensc 19
import FeedCommentSection 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);
37
  }
38
`
39
 
7062 stevensc 40
const KnowledgeViewPage = () => {
7080 stevensc 41
  const [comments, setComments] = useState([])
7065 stevensc 42
  const [knowledge, setKnowledge] = useState({
43
    category: '',
44
    title: '',
45
    description: '',
46
    link: null,
47
    image: '',
48
    attachment: '',
49
    reaction: '',
50
    routeComments: '',
51
    routeCommentAdd: '',
52
    routeSaveReaction: '',
53
    routeDeleteReaction: '',
54
  })
55
  const labels = useSelector(({ intl }) => intl.labels)
56
  const dispatch = useDispatch()
7062 stevensc 57
  const { uuid } = useParams()
58
 
7066 stevensc 59
  const changeReaction = (reaction) => {
7080 stevensc 60
    setKnowledge((prevKnowledge) => ({ ...prevKnowledge }))
7066 stevensc 61
  }
62
 
7062 stevensc 63
  useEffect(() => {
64
    axios
65
      .get(`/knowledge-area/view/${uuid}`, {
66
        headers: {
67
          'Content-Type': 'application/json',
68
        },
69
      })
7065 stevensc 70
      .then((response) => {
71
        const { data, success } = response.data
72
        if (!success) {
73
          const errorMessage =
74
            typeof data === 'string' ? data : labels.error_there_was_an_error
75
 
76
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
77
          return
78
        }
79
 
80
        setKnowledge(data)
81
      })
82
      .catch((err) => {
83
        dispatch(
84
          addNotification({
85
            style: 'danger',
86
            msg: labels.error_there_was_an_error,
87
          })
88
        )
89
        throw new Error(err)
90
      })
7062 stevensc 91
  }, [uuid])
92
 
7080 stevensc 93
  useEffect(() => {
94
    if (!knowledge.routeComments) return
95
 
96
    axios
97
      .get(knowledge.routeComments)
98
      .then((response) => {
99
        const { data, success } = response.data
100
 
101
        if (!success) {
102
          const errorMessage =
103
            typeof data === 'string' ? data : labels.error_there_was_an_error
104
 
105
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
106
          return
107
        }
108
 
109
        setComments(data)
110
      })
111
      .catch((err) => {
112
        dispatch(
113
          addNotification({
114
            style: 'danger',
115
            msg: labels.error_there_was_an_error,
116
          })
117
        )
118
        throw new Error(err)
119
      })
120
  }, [knowledge])
121
 
7065 stevensc 122
  return (
123
    <Container as="section">
7082 stevensc 124
      <h2 className="text-center mb-3">{labels.knowledge_area_title}</h2>
7066 stevensc 125
      <Row>
126
        <Col className="mx-auto" md="8">
127
          <KnowledgeCard>
128
            <CardMedia
129
              component="img"
7067 stevensc 130
              height="250"
7066 stevensc 131
              image={knowledge.image}
132
              alt={`${knowledge.title} image`}
133
            />
134
            <CardContent>
135
              <Typography variant="h5">{knowledge.title}</Typography>
136
              <Typography variant="subtitle1" color="text.secondary">
137
                {knowledge.category}
138
              </Typography>
139
              {knowledge.description && parse(knowledge.description)}
140
            </CardContent>
7067 stevensc 141
            <KnowledgeActions>
7068 stevensc 142
              <ReactionsButton
143
                onChange={changeReaction}
144
                currentReaction={knowledge.reaction}
145
                withLabel
146
                deleteUrl={knowledge.routeDeleteReaction}
147
                reactionTypesUrl={{
148
                  r: knowledge.routeSaveReaction,
149
                  s: knowledge.routeSaveReaction,
150
                  l: knowledge.routeSaveReaction,
151
                  i: knowledge.routeSaveReaction,
152
                  f: knowledge.routeSaveReaction,
153
                }}
154
              />
155
 
7066 stevensc 156
              {knowledge.attachment && (
7068 stevensc 157
                <a
158
                  href={knowledge.attachment}
159
                  download
160
                  className="btn reaction-btn"
161
                >
162
                  <FileDownloadIcon />
163
                  {labels.knowledge_area_download_attachment}
164
                </a>
7066 stevensc 165
              )}
166
              {knowledge.link && (
7068 stevensc 167
                <a
168
                  href={knowledge.link}
169
                  className="btn reaction-btn"
170
                  target="_blank"
171
                  rel="noreferrer"
172
                >
173
                  {labels.knowledge_area_go_to_link}
174
                </a>
7066 stevensc 175
              )}
7081 stevensc 176
            </KnowledgeActions>
177
            <div className="px-3">
7080 stevensc 178
              <FeedCommentSection
179
                currentComments={comments}
180
                addUrl={knowledge.routeCommentAdd}
181
                isShow
182
              />
7081 stevensc 183
            </div>
7066 stevensc 184
          </KnowledgeCard>
185
        </Col>
186
      </Row>
7065 stevensc 187
    </Container>
188
  )
7062 stevensc 189
}
190
 
191
export default KnowledgeViewPage