Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
651 stevensc 1
import React, { useState, useEffect } from 'react'
2
import { useDispatch, useSelector } from 'react-redux'
650 stevensc 3
import { addNotification } from '../../redux/notification/notification.actions'
4
import { Col, Container, Row } from 'react-bootstrap'
974 stevensc 5
import { useHistory, useLocation } from 'react-router-dom'
970 stevensc 6
import { axios, debounce } from '../../utils'
485 stevensc 7
import SearchIcon from '@mui/icons-material/Search'
5 stevensc 8
 
650 stevensc 9
import Input from 'components/UI/Input'
10
import Spinner from 'components/UI/Spinner'
11
import SearchItem from 'components/search/SearchItem'
12
import EmptySection from 'components/UI/EmptySection'
13
import FiltersSidebar from 'components/search/FiltersSidebar'
14
import CategoryFilter from 'components/search/CategoryFilter'
15
import LocationFilter from 'components/search/LocationFilter'
16
import PaginationComponent from 'components/UI/PaginationComponent'
5 stevensc 17
 
18
const SearchPage = () => {
485 stevensc 19
  const [entities, setEntities] = useState([])
20
  const [loading, setLoading] = useState(true)
21
  const [address, setAddress] = useState({})
22
  const [pages, setPages] = useState(1)
651 stevensc 23
  const labels = useSelector(({ intl }) => intl.labels)
970 stevensc 24
  const { search, pathname } = useLocation()
651 stevensc 25
  const dispatch = useDispatch()
485 stevensc 26
  const history = useHistory()
5 stevensc 27
 
485 stevensc 28
  const params = new URLSearchParams(search)
29
  const keyword = params.get('keyword')
970 stevensc 30
  const currentPage = params.get('page')
5 stevensc 31
 
970 stevensc 32
  const searchEntities = (address = {}) => {
485 stevensc 33
    setLoading(true)
5 stevensc 34
 
649 stevensc 35
    axios
971 stevensc 36
      .get(`${pathname}?${params.toString()}`)
649 stevensc 37
      .then(({ data: responseData }) => {
38
        const { success, data } = responseData
5 stevensc 39
 
649 stevensc 40
        if (!success) {
41
          throw new Error(data)
5 stevensc 42
        }
649 stevensc 43
 
44
        setEntities(data.current.items)
45
        setPages(data.total.pages)
485 stevensc 46
      })
649 stevensc 47
      .catch((err) => {
655 stevensc 48
        dispatch(addNotification({ style: 'danger', msg: err.message }))
649 stevensc 49
      })
50
      .finally(() => setLoading(false))
485 stevensc 51
  }
5 stevensc 52
 
975 stevensc 53
  const onChangeKeyword = debounce((value = '') => {
970 stevensc 54
    params.set('page', '1')
975 stevensc 55
    params.set('keyword', value)
970 stevensc 56
    history.replace(`${pathname}?${params.toString()}`)
57
  }, 500)
58
 
59
  const onChangePage = (currentPage = 1) => {
60
    params.set('page', `${currentPage}`)
61
    history.replace(`${pathname}?${params.toString()}`)
62
  }
63
 
64
  const onChangeCategory = (newCategory = 'user') => {
65
    params.set('page', '1')
66
    history.replace(`/search/entity/${newCategory}?${params.toString()}`)
67
  }
68
 
649 stevensc 69
  useEffect(() => {
970 stevensc 70
    searchEntities(address)
973 stevensc 71
  }, [address, search, pathname])
5 stevensc 72
 
73
  return (
74
    <>
649 stevensc 75
      <Container as='main'>
375 stevensc 76
        <Input
208 stevensc 77
          icon={SearchIcon}
970 stevensc 78
          onChange={(e) => onChangeKeyword(e.target.value)}
79
          defaultValue={keyword}
651 stevensc 80
          placeholder={labels.search}
5 stevensc 81
        />
970 stevensc 82
 
649 stevensc 83
        <Row className='mt-3'>
84
          <Col as='aside' md='4'>
5 stevensc 85
            <FiltersSidebar>
970 stevensc 86
              <CategoryFilter onChange={onChangeCategory} />
87
 
650 stevensc 88
              <LocationFilter
89
                onChange={(newAddress) => setAddress(newAddress)}
90
              />
5 stevensc 91
            </FiltersSidebar>
92
          </Col>
970 stevensc 93
 
649 stevensc 94
          <Col as='section' md='8'>
95
            <div className='posts-section'>
5 stevensc 96
              {loading && <Spinner />}
97
              {entities.length ? (
98
                entities.map((entity) => (
649 stevensc 99
                  <SearchItem key={entity.id} {...entity} />
5 stevensc 100
                ))
101
              ) : (
649 stevensc 102
                <EmptySection message='No hay resultados' />
5 stevensc 103
              )}
104
            </div>
105
            <PaginationComponent
106
              pages={pages}
107
              currentActivePage={currentPage}
970 stevensc 108
              onChangePage={onChangePage}
5 stevensc 109
              isRow
110
            />
111
          </Col>
112
        </Row>
113
      </Container>
114
    </>
485 stevensc 115
  )
116
}
5 stevensc 117
 
485 stevensc 118
export default SearchPage