Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 972 | Rev 974 | 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'
970 stevensc 5
import { useHistory, useLocation, useRouteMatch } from 'react-router-dom'
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
 
970 stevensc 53
  const onChangeKeyword = debounce((value) => {
54
    params.set('page', '1')
649 stevensc 55
 
970 stevensc 56
    if (value) {
57
      params.set('keyword', value)
58
    } else {
59
      params.delete('keyword')
60
    }
649 stevensc 61
 
970 stevensc 62
    history.replace(`${pathname}?${params.toString()}`)
63
  }, 500)
64
 
65
  const onChangePage = (currentPage = 1) => {
66
    params.set('page', `${currentPage}`)
67
    history.replace(`${pathname}?${params.toString()}`)
68
  }
69
 
70
  const onChangeCategory = (newCategory = 'user') => {
71
    params.set('page', '1')
72
    history.replace(`/search/entity/${newCategory}?${params.toString()}`)
73
  }
74
 
649 stevensc 75
  useEffect(() => {
970 stevensc 76
    searchEntities(address)
973 stevensc 77
  }, [address, search, pathname])
5 stevensc 78
 
79
  return (
80
    <>
649 stevensc 81
      <Container as='main'>
375 stevensc 82
        <Input
208 stevensc 83
          icon={SearchIcon}
970 stevensc 84
          onChange={(e) => onChangeKeyword(e.target.value)}
85
          defaultValue={keyword}
651 stevensc 86
          placeholder={labels.search}
5 stevensc 87
        />
970 stevensc 88
 
649 stevensc 89
        <Row className='mt-3'>
90
          <Col as='aside' md='4'>
5 stevensc 91
            <FiltersSidebar>
970 stevensc 92
              <CategoryFilter onChange={onChangeCategory} />
93
 
650 stevensc 94
              <LocationFilter
95
                onChange={(newAddress) => setAddress(newAddress)}
96
              />
5 stevensc 97
            </FiltersSidebar>
98
          </Col>
970 stevensc 99
 
649 stevensc 100
          <Col as='section' md='8'>
101
            <div className='posts-section'>
5 stevensc 102
              {loading && <Spinner />}
103
              {entities.length ? (
104
                entities.map((entity) => (
649 stevensc 105
                  <SearchItem key={entity.id} {...entity} />
5 stevensc 106
                ))
107
              ) : (
649 stevensc 108
                <EmptySection message='No hay resultados' />
5 stevensc 109
              )}
110
            </div>
111
            <PaginationComponent
112
              pages={pages}
113
              currentActivePage={currentPage}
970 stevensc 114
              onChangePage={onChangePage}
5 stevensc 115
              isRow
116
            />
117
          </Col>
118
        </Row>
119
      </Container>
120
    </>
485 stevensc 121
  )
122
}
5 stevensc 123
 
485 stevensc 124
export default SearchPage