| 5 |
stevensc |
1 |
import React, { useState, useRef, useEffect } from 'react'
|
|
|
2 |
import { axios, jsonToParams } from '../../utils'
|
|
|
3 |
import { Col, Container, Row } from 'react-bootstrap'
|
|
|
4 |
import { useHistory, useLocation } from 'react-router-dom'
|
|
|
5 |
|
|
|
6 |
import Spinner from '../../components/UI/Spinner'
|
|
|
7 |
import SearchItem from '../../components/search/SearchItem'
|
|
|
8 |
import SearchInput from '../../components/UI/SearchInput'
|
|
|
9 |
import EmptySection from '../../components/UI/EmptySection'
|
|
|
10 |
import PaginationComponent from '../../components/UI/PaginationComponent'
|
|
|
11 |
import FiltersSidebar from '../../components/search/FiltersSidebar'
|
|
|
12 |
import CategoryFilter from '../../components/search/CategoryFilter'
|
|
|
13 |
import LocationFilter from '../../components/search/LocationFilter'
|
|
|
14 |
|
|
|
15 |
const SearchPage = () => {
|
|
|
16 |
const [entities, setEntities] = useState([])
|
|
|
17 |
const [loading, setLoading] = useState(true)
|
|
|
18 |
const [category, setCategory] = useState('user')
|
|
|
19 |
const [entity, setEntity] = useState('')
|
|
|
20 |
const [currentPage, setCurrentPage] = useState(1)
|
|
|
21 |
const [pages, setPages] = useState(1)
|
|
|
22 |
const activeFilters = useRef([])
|
|
|
23 |
const addressKeys = useRef([])
|
|
|
24 |
|
|
|
25 |
const { search, pathname } = useLocation()
|
|
|
26 |
const history = useHistory()
|
|
|
27 |
|
|
|
28 |
const params = new URLSearchParams(search)
|
|
|
29 |
const keyword = params.get('keyword')
|
|
|
30 |
|
|
|
31 |
const loadEntities = async (page = 1, keyword = '', category = 'user') => {
|
|
|
32 |
setLoading(true)
|
|
|
33 |
setCurrentPage(page)
|
|
|
34 |
|
|
|
35 |
const urlParams = { page, keyword }
|
|
|
36 |
|
|
|
37 |
addressKeys.current.forEach(({ name, type }) => {
|
|
|
38 |
urlParams[type] = name
|
|
|
39 |
})
|
|
|
40 |
|
|
|
41 |
activeFilters.current.forEach(({ name, value }) => {
|
|
|
42 |
urlParams[name] = value
|
|
|
43 |
})
|
|
|
44 |
|
|
|
45 |
await axios
|
|
|
46 |
.get(`/search/entity/${category}?${jsonToParams(urlParams)}`)
|
|
|
47 |
.then((response) => {
|
|
|
48 |
const { success, data } = response.data
|
|
|
49 |
|
|
|
50 |
if (success) {
|
|
|
51 |
setEntities(data.current.items)
|
|
|
52 |
setPages(data.total.pages)
|
|
|
53 |
}
|
|
|
54 |
})
|
|
|
55 |
setLoading(false)
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
const onChangePageHandler = (currentPage) => {
|
|
|
59 |
setCurrentPage(currentPage)
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
const getAddressHandler = (addresObject) => {
|
|
|
63 |
const { address_components } = addresObject
|
|
|
64 |
if (address_components) {
|
|
|
65 |
addressKeys.current = []
|
|
|
66 |
address_components.map((address_component) => {
|
|
|
67 |
const address_component_name = address_component.long_name
|
|
|
68 |
const address_component_type = address_component.types[0]
|
|
|
69 |
switch (address_component_type) {
|
|
|
70 |
case 'route':
|
|
|
71 |
addressKeys.current = [
|
|
|
72 |
...addressKeys.current,
|
|
|
73 |
{ type: 'route', name: address_component_name },
|
|
|
74 |
]
|
|
|
75 |
break
|
|
|
76 |
case 'sublocality':
|
|
|
77 |
addressKeys.current = [
|
|
|
78 |
...addressKeys.current,
|
|
|
79 |
{ type: 'sublocality', name: address_component_name },
|
|
|
80 |
]
|
|
|
81 |
break
|
|
|
82 |
case 'locality':
|
|
|
83 |
addressKeys.current = [
|
|
|
84 |
...addressKeys.current,
|
|
|
85 |
{ type: 'locality', name: address_component_name },
|
|
|
86 |
]
|
|
|
87 |
break
|
|
|
88 |
case 'administrative_area_level_2':
|
|
|
89 |
addressKeys.current = [
|
|
|
90 |
...addressKeys.current,
|
|
|
91 |
{
|
|
|
92 |
type: 'administrative_area_level_2',
|
|
|
93 |
name: address_component_name,
|
|
|
94 |
},
|
|
|
95 |
]
|
|
|
96 |
break
|
|
|
97 |
case 'administrative_area_level_1':
|
|
|
98 |
addressKeys.current = [
|
|
|
99 |
...addressKeys.current,
|
|
|
100 |
{
|
|
|
101 |
type: 'administrative_area_level_1',
|
|
|
102 |
name: address_component_name,
|
|
|
103 |
},
|
|
|
104 |
]
|
|
|
105 |
break
|
|
|
106 |
case 'country':
|
|
|
107 |
addressKeys.current = [
|
|
|
108 |
...addressKeys.current,
|
|
|
109 |
{ type: 'country', name: address_component_name },
|
|
|
110 |
]
|
|
|
111 |
break
|
|
|
112 |
case 'postal_code':
|
|
|
113 |
addressKeys.current = [
|
|
|
114 |
...addressKeys.current,
|
|
|
115 |
{ type: 'postal_code', name: address_component_name },
|
|
|
116 |
]
|
|
|
117 |
break
|
|
|
118 |
default:
|
|
|
119 |
break
|
|
|
120 |
}
|
|
|
121 |
})
|
|
|
122 |
|
|
|
123 |
loadEntities()
|
|
|
124 |
} else {
|
|
|
125 |
if (addressKeys.current.length) {
|
|
|
126 |
loadEntities()
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
const onSubmitHandler = (e) => {
|
|
|
132 |
e.preventDefault()
|
|
|
133 |
history.push({ pathname, search: `?keyword=${entity}` })
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
const changeCategory = (newCategory) => {
|
|
|
137 |
const urlParams = { keyword }
|
|
|
138 |
|
|
|
139 |
activeFilters.current.forEach(({ name, value }) => {
|
|
|
140 |
urlParams[name] = value
|
|
|
141 |
})
|
|
|
142 |
|
|
|
143 |
setCategory(newCategory)
|
|
|
144 |
history.push(`/search/entity/${newCategory}?${jsonToParams(urlParams)}`)
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
useEffect(() => {
|
|
|
148 |
loadEntities(currentPage, keyword, category)
|
|
|
149 |
setEntity(keyword)
|
|
|
150 |
return () => {
|
|
|
151 |
setCategory('user')
|
|
|
152 |
setEntity('')
|
|
|
153 |
activeFilters.current = []
|
|
|
154 |
addressKeys.current = []
|
|
|
155 |
}
|
|
|
156 |
}, [keyword, category, currentPage])
|
|
|
157 |
|
|
|
158 |
return (
|
|
|
159 |
<>
|
|
|
160 |
<Container as="main">
|
|
|
161 |
<SearchInput
|
|
|
162 |
as="form"
|
|
|
163 |
onSubmit={onSubmitHandler}
|
|
|
164 |
onChange={(e) => setEntity(e.target.value)}
|
|
|
165 |
value={entity}
|
|
|
166 |
plackeyword
|
|
|
167 |
style={{ backgroundColor: 'var(--bg-color)' }}
|
|
|
168 |
/>
|
|
|
169 |
<Row className="mt-3">
|
|
|
170 |
<Col as="aside" md="4">
|
|
|
171 |
<FiltersSidebar>
|
|
|
172 |
<CategoryFilter onChange={changeCategory} />
|
|
|
173 |
<LocationFilter onChange={getAddressHandler} />
|
|
|
174 |
</FiltersSidebar>
|
|
|
175 |
</Col>
|
|
|
176 |
<Col as="section" md="8">
|
|
|
177 |
<div className="posts-section">
|
|
|
178 |
{loading && <Spinner />}
|
|
|
179 |
{entities.length ? (
|
|
|
180 |
entities.map((entity) => (
|
|
|
181 |
<SearchItem
|
|
|
182 |
key={entity.id}
|
|
|
183 |
onChangePage={onChangePageHandler}
|
|
|
184 |
{...entity}
|
|
|
185 |
/>
|
|
|
186 |
))
|
|
|
187 |
) : (
|
|
|
188 |
<EmptySection message="No hay resultados" />
|
|
|
189 |
)}
|
|
|
190 |
</div>
|
|
|
191 |
<PaginationComponent
|
|
|
192 |
pages={pages}
|
|
|
193 |
currentActivePage={currentPage}
|
|
|
194 |
onChangePage={loadEntities}
|
|
|
195 |
isRow
|
|
|
196 |
/>
|
|
|
197 |
</Col>
|
|
|
198 |
</Row>
|
|
|
199 |
</Container>
|
|
|
200 |
</>
|
|
|
201 |
)
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
export default SearchPage
|