Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 975 | Rev 979 | 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'
976 stevensc 5
import { useHistory, useLocation, useParams } 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)
976 stevensc 23
 
651 stevensc 24
  const labels = useSelector(({ intl }) => intl.labels)
970 stevensc 25
  const { search, pathname } = useLocation()
976 stevensc 26
  const { category } = useParams()
651 stevensc 27
  const dispatch = useDispatch()
485 stevensc 28
  const history = useHistory()
5 stevensc 29
 
485 stevensc 30
  const params = new URLSearchParams(search)
31
  const keyword = params.get('keyword')
970 stevensc 32
  const currentPage = params.get('page')
5 stevensc 33
 
970 stevensc 34
  const searchEntities = (address = {}) => {
485 stevensc 35
    setLoading(true)
5 stevensc 36
 
649 stevensc 37
    axios
971 stevensc 38
      .get(`${pathname}?${params.toString()}`)
649 stevensc 39
      .then(({ data: responseData }) => {
40
        const { success, data } = responseData
5 stevensc 41
 
649 stevensc 42
        if (!success) {
43
          throw new Error(data)
5 stevensc 44
        }
649 stevensc 45
 
46
        setEntities(data.current.items)
47
        setPages(data.total.pages)
485 stevensc 48
      })
649 stevensc 49
      .catch((err) => {
655 stevensc 50
        dispatch(addNotification({ style: 'danger', msg: err.message }))
649 stevensc 51
      })
52
      .finally(() => setLoading(false))
485 stevensc 53
  }
5 stevensc 54
 
975 stevensc 55
  const onChangeKeyword = debounce((value = '') => {
970 stevensc 56
    params.set('page', '1')
975 stevensc 57
    params.set('keyword', value)
970 stevensc 58
    history.replace(`${pathname}?${params.toString()}`)
59
  }, 500)
60
 
61
  const onChangePage = (currentPage = 1) => {
62
    params.set('page', `${currentPage}`)
63
    history.replace(`${pathname}?${params.toString()}`)
64
  }
65
 
66
  const onChangeCategory = (newCategory = 'user') => {
67
    params.set('page', '1')
68
    history.replace(`/search/entity/${newCategory}?${params.toString()}`)
69
  }
70
 
649 stevensc 71
  useEffect(() => {
970 stevensc 72
    searchEntities(address)
973 stevensc 73
  }, [address, search, pathname])
5 stevensc 74
 
75
  return (
76
    <>
649 stevensc 77
      <Container as='main'>
375 stevensc 78
        <Input
208 stevensc 79
          icon={SearchIcon}
970 stevensc 80
          onChange={(e) => onChangeKeyword(e.target.value)}
81
          defaultValue={keyword}
651 stevensc 82
          placeholder={labels.search}
5 stevensc 83
        />
970 stevensc 84
 
649 stevensc 85
        <Row className='mt-3'>
86
          <Col as='aside' md='4'>
5 stevensc 87
            <FiltersSidebar>
976 stevensc 88
              <CategoryFilter
89
                currentCategory={category}
90
                onChange={onChangeCategory}
91
              />
970 stevensc 92
 
650 stevensc 93
              <LocationFilter
94
                onChange={(newAddress) => setAddress(newAddress)}
95
              />
5 stevensc 96
            </FiltersSidebar>
97
          </Col>
970 stevensc 98
 
649 stevensc 99
          <Col as='section' md='8'>
100
            <div className='posts-section'>
5 stevensc 101
              {loading && <Spinner />}
102
              {entities.length ? (
103
                entities.map((entity) => (
649 stevensc 104
                  <SearchItem key={entity.id} {...entity} />
5 stevensc 105
                ))
106
              ) : (
649 stevensc 107
                <EmptySection message='No hay resultados' />
5 stevensc 108
              )}
109
            </div>
976 stevensc 110
 
5 stevensc 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