Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
7033 stevensc 1
import React, { useEffect, useRef, 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'
7033 stevensc 20
import KnowledgeEditModal from '../../components/knowledge/KnowledgeEditModal'
7043 stevensc 21
import ConfirmModal from '../../components/modals/ConfirmModal'
7062 stevensc 22
import { Link } from 'react-router-dom'
7015 stevensc 23
 
7024 stevensc 24
const KnowledgeCategories = styled(WidgetLayout)`
25
  padding: 1rem;
7027 stevensc 26
  ul {
27
    display: flex;
28
    flex-direction: column;
7040 stevensc 29
    gap: 1rem;
7039 stevensc 30
    li,
31
    label {
7033 stevensc 32
      cursor: pointer;
33
    }
7039 stevensc 34
    .selected label {
7033 stevensc 35
      font-weight: 600;
36
    }
7027 stevensc 37
  }
7024 stevensc 38
`
39
 
7026 stevensc 40
const KnowledgeGrid = styled.div`
41
  display: grid;
7058 stevensc 42
  grid-template-columns: repeat(auto-fit, 250px);
7026 stevensc 43
  gap: 1rem;
44
`
45
 
7030 stevensc 46
const KnowledgeCard = styled(Card)`
7031 stevensc 47
  background-color: var(--bg-color);
48
  border-radius: var(--border-radius);
49
  overflow: hidden;
7032 stevensc 50
  height: fit-content;
7027 stevensc 51
`
52
 
7024 stevensc 53
const KnowledgeSearch = styled(SearchInput)`
54
  background-color: var(--bg-color);
55
`
56
 
7020 stevensc 57
const KnowledgeAreaPage = () => {
7015 stevensc 58
  const [knowledges, setKnowledges] = useState([])
59
  const [knowledgesCategories, setKnowledgesCategories] = useState([])
7020 stevensc 60
  const [search, setSearch] = useState('')
7023 stevensc 61
  const [category, setCategory] = useState('')
7020 stevensc 62
  const [currentPage, setCurrentPage] = useState(1)
63
  const [totalPages, setTotalPages] = useState(1)
64
  const [allowAdd, setAllowAdd] = useState(false)
7033 stevensc 65
  const [modalShow, setModalShow] = useState(null)
7045 stevensc 66
  const addUrl = useRef('')
7034 stevensc 67
  const actionUrl = useRef('')
7015 stevensc 68
  const labels = useSelector(({ intl }) => intl.labels)
69
  const dispatch = useDispatch()
70
 
7033 stevensc 71
  const getKnowledgesInfo = (search = '', page = 1, category_id = '') => {
72
    const urlParams = { search, page, category_id }
7015 stevensc 73
    axios
7021 stevensc 74
      .get(`/knowledge-area?${jsonToParams(urlParams)}`, {
7015 stevensc 75
        headers: {
76
          'Content-Type': 'application/json',
77
        },
78
      })
79
      .then((response) => {
7019 stevensc 80
        const { data, success } = response.data
81
 
82
        if (!success) {
83
          const errorMessage =
84
            typeof data === 'string'
85
              ? data
86
              : 'Ha ocurrido un error, por favor intente más tarde.'
87
 
88
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
89
          return
90
        }
91
 
7051 stevensc 92
        const categories = Object.entries(data.categories).map(
93
          (values) => values[1]
94
        )
95
 
96
        setKnowledges(data.items)
97
        setKnowledgesCategories(categories)
7020 stevensc 98
        setCurrentPage(data.page)
99
        setTotalPages(data.total_pages)
100
        setAllowAdd(Boolean(data.link_add))
7045 stevensc 101
        addUrl.current = data.link_add
7015 stevensc 102
      })
103
      .catch((error) => {
104
        dispatch(
105
          addNotification({
106
            style: 'danger',
107
            msg: 'Ha ocurrido un error, por favor intente más tarde.',
108
          })
109
        )
110
        throw new Error(error)
111
      })
7020 stevensc 112
  }
7015 stevensc 113
 
7043 stevensc 114
  const confirmDelete = () => {
115
    axios
116
      .post(actionUrl.current)
117
      .then((response) => {
118
        const { data, success } = response.data
119
 
120
        if (!success) {
121
          const errorMessage =
122
            typeof data === 'string'
123
              ? data
124
              : 'Ha ocurrido un error, por favor intente más tarde.'
125
 
126
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
127
          return
128
        }
129
 
130
        dispatch(addNotification({ style: 'success', msg: data }))
7044 stevensc 131
        getKnowledgesInfo()
132
        closeModal()
7043 stevensc 133
      })
134
      .catch((error) => {
135
        dispatch(
136
          addNotification({
137
            style: 'danger',
138
            msg: 'Ha ocurrido un error, por favor intente más tarde.',
139
          })
140
        )
141
        throw new Error(error)
142
      })
143
  }
144
 
7045 stevensc 145
  const addKnowledge = (url) => {
146
    actionUrl.current = url
147
    setModalShow('add')
148
  }
149
 
7033 stevensc 150
  const editKnowledge = (url) => {
151
    actionUrl.current = url
152
    setModalShow('edit')
153
  }
154
 
7043 stevensc 155
  const deleteKnowledge = (url) => {
156
    actionUrl.current = url
157
    setModalShow('delete')
158
  }
159
 
7033 stevensc 160
  const closeModal = () => {
7036 stevensc 161
    actionUrl.current = ''
7033 stevensc 162
    setModalShow(null)
163
  }
164
 
7022 stevensc 165
  const handleInputChange = debounce((e) => setSearch(e.target.value), 500)
7020 stevensc 166
 
167
  useEffect(() => {
7023 stevensc 168
    getKnowledgesInfo(search, currentPage, category)
7030 stevensc 169
  }, [search, currentPage, category])
7020 stevensc 170
 
7015 stevensc 171
  return (
7033 stevensc 172
    <>
7058 stevensc 173
      <Container as="section" className="companies-info">
7033 stevensc 174
        <div className="company-title">
175
          <h1 className="title mx-auto">{labels.knowledge_area_title}</h1>
176
          {allowAdd && (
7045 stevensc 177
            <h2
178
              className="title cursor-pointer"
179
              onClick={() => addKnowledge(addUrl.current)}
180
            >
7033 stevensc 181
              {labels.knowledge_area_add}
182
            </h2>
183
          )}
184
        </div>
7015 stevensc 185
 
7033 stevensc 186
        <Row className="gap-3">
187
          <Col md="3">
188
            <KnowledgeCategories>
189
              <ul>
7039 stevensc 190
                <li className={!category && 'selected'}>
7026 stevensc 191
                  <input
192
                    type="radio"
7033 stevensc 193
                    id="category-all"
194
                    value=""
7043 stevensc 195
                    name="category"
7026 stevensc 196
                    onChange={(e) => setCategory(e.target.value)}
197
                    hidden
198
                  />
7039 stevensc 199
                  <label htmlFor="category-all">
7033 stevensc 200
                    {labels.knowledge_area_category_all}
201
                  </label>
7026 stevensc 202
                </li>
7033 stevensc 203
                {knowledgesCategories.map(({ uuid, name }) => (
7039 stevensc 204
                  <li className={category === uuid && 'selected'} key={uuid}>
7033 stevensc 205
                    <input
206
                      type="radio"
207
                      id={`category-${name}`}
7042 stevensc 208
                      name="category"
7033 stevensc 209
                      value={uuid}
210
                      onChange={(e) => setCategory(e.target.value)}
211
                      hidden
212
                    />
7039 stevensc 213
                    <label htmlFor={`category-${name}`}>{name}</label>
7033 stevensc 214
                  </li>
215
                ))}
216
              </ul>
217
            </KnowledgeCategories>
218
          </Col>
7015 stevensc 219
 
7033 stevensc 220
          <Col className="px-0">
221
            <KnowledgeSearch
222
              onChange={handleInputChange}
223
              placeholder={labels.search}
224
            />
7015 stevensc 225
 
7033 stevensc 226
            <KnowledgeGrid className="mt-3">
227
              {knowledges.length ? (
228
                knowledges.map((knowledge, index) => (
7043 stevensc 229
                  <Item
230
                    key={index}
231
                    {...knowledge}
232
                    onEdit={editKnowledge}
233
                    onDelete={deleteKnowledge}
234
                  />
7033 stevensc 235
                ))
236
              ) : (
237
                <EmptySection
238
                  message={labels.error_no_record_matched_your_query}
239
                />
240
              )}
241
            </KnowledgeGrid>
242
            <PaginationComponent
243
              onChangePage={(newPage) => setCurrentPage(newPage)}
244
              currentActivePage={currentPage}
245
              pages={totalPages}
246
              isRow
247
            />
248
          </Col>
249
        </Row>
250
      </Container>
251
      <KnowledgeEditModal
252
        show={modalShow === 'edit'}
253
        url={actionUrl.current}
254
        categories={knowledgesCategories}
255
        onComplete={getKnowledgesInfo}
256
        onClose={closeModal}
7045 stevensc 257
        isEdit
7033 stevensc 258
      />
7045 stevensc 259
      <KnowledgeEditModal
260
        show={modalShow === 'add'}
261
        url={actionUrl.current}
262
        categories={knowledgesCategories}
263
        onComplete={getKnowledgesInfo}
264
        onClose={closeModal}
265
      />
7043 stevensc 266
      <ConfirmModal
267
        show={modalShow === 'delete'}
268
        onClose={closeModal}
269
        onAccept={confirmDelete}
270
      />
7033 stevensc 271
    </>
7015 stevensc 272
  )
273
}
274
 
7019 stevensc 275
const Item = ({
276
  link_delete,
277
  link_view,
278
  link_edit,
279
  category,
280
  description,
281
  image,
282
  title,
7033 stevensc 283
  onEdit,
7043 stevensc 284
  onDelete,
7019 stevensc 285
}) => {
286
  return (
7030 stevensc 287
    <>
288
      <KnowledgeCard>
289
        <CardMedia
290
          component="img"
291
          height="194"
292
          image={image}
293
          alt={`${title} image`}
294
        />
295
        <CardContent>
7062 stevensc 296
          <Link to={link_view}>
297
            <Typography variant="h5">{title}</Typography>
298
          </Link>
7031 stevensc 299
          <Typography variant="subtitle1" color="text.secondary">
300
            {category}
301
          </Typography>
7030 stevensc 302
          <Typography variant="body2" color="text.secondary">
303
            {description}
304
          </Typography>
305
        </CardContent>
306
        <CardActions disableSpacing>
307
          {link_edit && (
7033 stevensc 308
            <IconButton aria-label="edit" onClick={() => onEdit(link_edit)}>
7030 stevensc 309
              <Edit />
310
            </IconButton>
311
          )}
312
          {link_delete && (
7043 stevensc 313
            <IconButton
314
              aria-label="delete"
315
              onClick={() => onDelete(link_delete)}
316
            >
7030 stevensc 317
              <Delete />
318
            </IconButton>
319
          )}
320
        </CardActions>
321
      </KnowledgeCard>
322
    </>
7019 stevensc 323
  )
324
}
325
 
7015 stevensc 326
export default KnowledgeAreaPage