Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7003 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
7002 stevensc 1
import React, { useState, useRef, useEffect } from 'react'
2
import { Container } from 'react-bootstrap'
3
import { useLocation } from 'react-router-dom'
4
import { useForm } from 'react-hook-form'
5
import { axios, jsonToParams } from '../../utils'
6
 
7
import Spinner from '../../components/UI/Spinner'
8
import SearchItem from '../../components/search/SearchItem'
9
import SearchInput from '../../components/UI/SearchInput'
10
import EmptySection from '../../components/UI/EmptySection'
11
import PaginationComponent from '../../components/UI/PaginationComponent'
12
 
13
const SearchPage = () => {
14
  const [entities, setEntities] = useState([])
15
  const [loading, setLoading] = useState(true)
16
  const [category, setCategory] = useState('user')
17
  const [currentPage, setCurrentPage] = useState(1)
18
  const [pages, setPages] = useState(1)
19
 
20
  const activeFilters = useRef([])
21
  const addressFilter = useRef([])
22
 
23
  const { search, pathname } = useLocation()
24
  const { register, handleSubmit, setValue, getValues } = useForm()
25
 
26
  // getting keyword
27
  const locationParams = new URLSearchParams(search)
28
  const keyword = locationParams.get('keyword')
29
 
30
  useEffect(() => {
31
    loadEntities()
32
  }, [keyword])
33
 
34
  const loadEntities = async (page = 1) => {
35
    setLoading(true)
36
    setCurrentPage(page)
37
 
38
    const urlParams = { page, keyword }
39
 
40
    addressFilter.current.forEach(({ name, type }) => {
41
      urlParams[type] = name
42
    })
43
    activeFilters.current.forEach(({ name, value }) => {
44
      urlParams[name] = value
45
    })
46
 
47
    await axios
48
      .get(`/search/entity/${category}?${jsonToParams(urlParams)}`)
49
      .then((response) => {
50
        const { success, data } = response.data
51
        if (success) {
52
          setEntities(data.current.items)
53
          setPages(data.total.pages)
54
        }
55
      })
56
    setLoading(false)
57
  }
58
 
59
  const onChangePageHandler = (currentPage) => {
60
    setCurrentPage(currentPage)
61
  }
62
 
63
  useEffect(() => {
64
    setValue('keyword', keyword)
65
    register('keyword')
66
  }, [])
67
 
68
  const onSubmitHandler = handleSubmit(({ keyword }) => {
69
    history.push({ pathname, search: `?keyword=${keyword}` })
70
  })
71
 
72
  return (
73
    <>
74
      <SearchInput
75
        as="form"
76
        onSubmit={onSubmitHandler}
77
        onChange={(e) => setValue('keyword', e.target.value)}
78
        value={getValues('keyword')}
79
      />
80
      <Container className="main-section-data mt-3">
81
        <div></div>
82
        <div className="main-ws-sec">
83
          <div className="posts-section">
84
            {loading && <Spinner />}
85
            {entities.length ? (
86
              entities.map((entity) => (
87
                <SearchItem
88
                  key={entity.id}
89
                  onChangePage={onChangePageHandler}
90
                  {...entity}
91
                />
92
              ))
93
            ) : (
94
              <EmptySection message="No hay resultados" />
95
            )}
96
          </div>
97
          <PaginationComponent
98
            pages={pages}
99
            currentActivePage={currentPage}
100
            onChangePage={loadEntities}
101
            isRow
102
          />
103
        </div>
104
        <div className="right-sidebar" />
105
      </Container>
106
    </>
107
  )
108
}
109
 
110
export default SearchPage