Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7030 | Rev 7032 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
7015 stevensc 1
import React, { useEffect, useState } from 'react'
7025 stevensc 2
import { useDispatch, useSelector } from 'react-redux'
7030 stevensc 3
import { Card, Col, Container, Row } from 'react-bootstrap'
7020 stevensc 4
import { axios, debounce, jsonToParams } from '../../utils'
7015 stevensc 5
import { addNotification } from '../../redux/notification/notification.actions'
7025 stevensc 6
import styled from 'styled-components'
7
 
8
import SearchInput from '../../components/UI/SearchInput'
7019 stevensc 9
import EmptySection from '../../components/UI/EmptySection'
7025 stevensc 10
import WidgetLayout from '../../components/widgets/WidgetLayout'
7020 stevensc 11
import PaginationComponent from '../../components/UI/PaginationComponent'
7030 stevensc 12
import {
13
  CardActions,
14
  CardContent,
15
  CardMedia,
16
  IconButton,
17
  Typography,
18
} from '@mui/material'
19
import { Delete, Edit } from '@mui/icons-material'
7015 stevensc 20
 
7024 stevensc 21
const KnowledgeCategories = styled(WidgetLayout)`
22
  padding: 1rem;
7027 stevensc 23
  ul {
24
    display: flex;
25
    flex-direction: column;
26
    gap: 0.5rem;
27
  }
7024 stevensc 28
`
29
 
7026 stevensc 30
const KnowledgeGrid = styled.div`
31
  display: grid;
32
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
33
  gap: 1rem;
34
`
35
 
7030 stevensc 36
const KnowledgeCard = styled(Card)`
7031 stevensc 37
  background-color: var(--bg-color);
38
  border-radius: var(--border-radius);
39
  overflow: hidden;
7027 stevensc 40
`
41
 
7024 stevensc 42
const KnowledgeSearch = styled(SearchInput)`
43
  background-color: var(--bg-color);
44
`
45
 
7020 stevensc 46
const KnowledgeAreaPage = () => {
7015 stevensc 47
  const [knowledges, setKnowledges] = useState([])
48
  const [knowledgesCategories, setKnowledgesCategories] = useState([])
7020 stevensc 49
  const [search, setSearch] = useState('')
7023 stevensc 50
  const [category, setCategory] = useState('')
7020 stevensc 51
  const [currentPage, setCurrentPage] = useState(1)
52
  const [totalPages, setTotalPages] = useState(1)
53
  const [allowAdd, setAllowAdd] = useState(false)
7015 stevensc 54
  const labels = useSelector(({ intl }) => intl.labels)
55
  const dispatch = useDispatch()
56
 
7023 stevensc 57
  const getKnowledgesInfo = (search, page, category) => {
58
    const urlParams = { search, page, category }
7015 stevensc 59
    axios
7021 stevensc 60
      .get(`/knowledge-area?${jsonToParams(urlParams)}`, {
7015 stevensc 61
        headers: {
62
          'Content-Type': 'application/json',
63
        },
64
      })
65
      .then((response) => {
7019 stevensc 66
        const { data, success } = response.data
67
 
68
        if (!success) {
69
          const errorMessage =
70
            typeof data === 'string'
71
              ? data
72
              : 'Ha ocurrido un error, por favor intente más tarde.'
73
 
74
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
75
          return
76
        }
77
 
78
        setKnowledges(data.items)
7020 stevensc 79
        setKnowledgesCategories(data.categories)
80
        setCurrentPage(data.page)
81
        setTotalPages(data.total_pages)
82
        setAllowAdd(Boolean(data.link_add))
7015 stevensc 83
      })
84
      .catch((error) => {
85
        dispatch(
86
          addNotification({
87
            style: 'danger',
88
            msg: 'Ha ocurrido un error, por favor intente más tarde.',
89
          })
90
        )
91
        throw new Error(error)
92
      })
7020 stevensc 93
  }
7015 stevensc 94
 
7022 stevensc 95
  const handleInputChange = debounce((e) => setSearch(e.target.value), 500)
7020 stevensc 96
 
97
  useEffect(() => {
7023 stevensc 98
    getKnowledgesInfo(search, currentPage, category)
7030 stevensc 99
  }, [search, currentPage, category])
7020 stevensc 100
 
7015 stevensc 101
  return (
7020 stevensc 102
    <Container as="section" className="companies-info px-0">
7015 stevensc 103
      <div className="company-title">
104
        <h1 className="title mx-auto">{labels.knowledge_area_title}</h1>
7020 stevensc 105
        {allowAdd && (
7021 stevensc 106
          <h2 className="title cursor-pointer">{labels.knowledge_area_add}</h2>
7015 stevensc 107
        )}
108
      </div>
109
 
7020 stevensc 110
      <Row className="gap-3">
111
        <Col md="3">
7025 stevensc 112
          <KnowledgeCategories>
7026 stevensc 113
            <ul>
114
              <li className="knowledge-category-li knowledge-category-li-selected">
7023 stevensc 115
                <input
116
                  type="radio"
7026 stevensc 117
                  id="category-all"
7027 stevensc 118
                  value=""
7023 stevensc 119
                  onChange={(e) => setCategory(e.target.value)}
120
                  hidden
121
                />
7026 stevensc 122
                <label htmlFor="category-all">
123
                  {labels.knowledge_area_category_all}
124
                </label>
7015 stevensc 125
              </li>
7026 stevensc 126
              {knowledgesCategories.map(({ uuid, name }) => (
127
                <li className="knowledge-category-li" key={uuid}>
128
                  <input
129
                    type="radio"
130
                    id={`category-${name}`}
131
                    value={uuid}
132
                    onChange={(e) => setCategory(e.target.value)}
133
                    hidden
134
                  />
135
                  <label htmlFor={`category-${name}`}>{name}</label>
136
                </li>
137
              ))}
138
            </ul>
7024 stevensc 139
          </KnowledgeCategories>
7020 stevensc 140
        </Col>
7015 stevensc 141
 
7020 stevensc 142
        <Col className="px-0">
7024 stevensc 143
          <KnowledgeSearch
7020 stevensc 144
            onChange={handleInputChange}
145
            placeholder={labels.search}
146
          />
7015 stevensc 147
 
7026 stevensc 148
          <KnowledgeGrid className="mt-3">
7019 stevensc 149
            {knowledges.length ? (
150
              knowledges.map((knowledge, index) => (
151
                <Item key={index} {...knowledge} />
152
              ))
153
            ) : (
154
              <EmptySection
155
                message={labels.error_no_record_matched_your_query}
156
              />
157
            )}
7026 stevensc 158
          </KnowledgeGrid>
7020 stevensc 159
          <PaginationComponent
160
            onChangePage={(newPage) => setCurrentPage(newPage)}
161
            currentActivePage={currentPage}
162
            pages={totalPages}
163
            isRow
164
          />
165
        </Col>
166
      </Row>
167
    </Container>
7015 stevensc 168
  )
169
}
170
 
7019 stevensc 171
const Item = ({
172
  link_delete,
173
  link_view,
174
  link_edit,
175
  category,
176
  description,
177
  image,
178
  title,
179
}) => {
180
  return (
7030 stevensc 181
    <>
182
      <KnowledgeCard>
183
        <CardMedia
184
          component="img"
185
          height="194"
186
          image={image}
187
          alt={`${title} image`}
188
        />
189
        <CardContent>
7031 stevensc 190
          <Typography variant="h2">{title}</Typography>
191
          <Typography variant="subtitle1" color="text.secondary">
192
            {category}
193
          </Typography>
7030 stevensc 194
          <Typography variant="body2" color="text.secondary">
195
            {description}
196
          </Typography>
197
        </CardContent>
198
        <CardActions disableSpacing>
199
          {link_edit && (
200
            <IconButton aria-label="edit">
201
              <Edit />
202
            </IconButton>
203
          )}
204
          {link_delete && (
205
            <IconButton aria-label="delete">
206
              <Delete />
207
            </IconButton>
208
          )}
209
        </CardActions>
210
      </KnowledgeCard>
211
    </>
7019 stevensc 212
  )
213
}
214
 
7015 stevensc 215
export default KnowledgeAreaPage