Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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