Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
5388 stevensc 1
import React, { useState, useRef, useEffect } from 'react'
5122 stevensc 2
import { useLocation } from 'react-router-dom'
3
import { useForm } from 'react-hook-form'
4
import { axios, jsonToParams } from '../../../utils'
5
import Spinner from '../../../shared/loading-spinner/Spinner'
6
import EntityTemplate from '../entity-template/EntityTemplate'
7
import PaginationComponent from '../../../shared/pagination/PaginationComponent'
5388 stevensc 8
import EmptySection from '../../../shared/empty-section/EmptySection'
6363 stevensc 9
import FiltersSidebar from '../../components/FiltersSidebar'
1 www 10
 
6363 stevensc 11
const MainSection = ({ filters }) => {
5122 stevensc 12
  const [entities, setEntities] = useState([])
13
  const [loading, setLoading] = useState(true)
14
  const [searchType, setSearchType] = useState('user')
15
  const [currentPage, setCurrentPage] = useState(1)
16
  const [pages, setPages] = useState(1)
6363 stevensc 17
 
5122 stevensc 18
  const activeFilters = useRef([])
19
  const addressFilter = useRef([])
20
  const formRef = useRef()
6363 stevensc 21
 
5122 stevensc 22
  const location = useLocation()
6363 stevensc 23
 
5122 stevensc 24
  const { register, setValue } = useForm()
1 www 25
 
26
  // getting keyword
5122 stevensc 27
  const locationParams = new URLSearchParams(location.search)
28
  const keyword = locationParams.get('keyword')
2275 stevensc 29
 
1018 stevensc 30
  useEffect(() => {
5122 stevensc 31
    loadEntities()
32
    formRef.current.reset()
33
    if (activeFilters.current.length) activeFilters.current = []
34
  }, [searchType, keyword])
1 www 35
 
36
  const onCheckHandler = (event, filterType) => {
5122 stevensc 37
    const checkBoxEl = event.target
1 www 38
    if (checkBoxEl.checked) {
39
      const newActiveFilters = [
40
        ...activeFilters.current,
6363 stevensc 41
        { name: checkBoxEl.name, value: 1, type: filterType },
5122 stevensc 42
      ]
43
      activeFilters.current = newActiveFilters
1 www 44
    } else {
45
      const newActiveFilters = activeFilters.current.filter(
46
        (activeFilter) => activeFilter.name !== checkBoxEl.name
5122 stevensc 47
      )
48
      activeFilters.current = newActiveFilters
1 www 49
    }
5122 stevensc 50
    loadEntities()
51
  }
1 www 52
 
53
  const onUncheckAllHandler = (event, filterType) => {
5122 stevensc 54
    event.preventDefault()
1 www 55
    const activeFiltersToDisable = activeFilters.current.filter(
56
      ({ type }) => type === filterType
5122 stevensc 57
    )
1 www 58
    activeFiltersToDisable.map(({ name }) => {
5122 stevensc 59
      setValue(name, '')
60
    })
1 www 61
 
62
    if (activeFilters.current.length) {
63
      const newActiveFilters = activeFilters.current.filter(
64
        ({ type }) => type !== filterType
5122 stevensc 65
      )
66
      activeFilters.current = newActiveFilters
67
      loadEntities()
1 www 68
    }
5122 stevensc 69
  }
1 www 70
 
71
  const loadEntities = async (page = 1) => {
5122 stevensc 72
    setLoading(true)
1 www 73
    setCurrentPage(page)
5122 stevensc 74
    const urlParams = {
1 www 75
      page,
6363 stevensc 76
      keyword,
1 www 77
    }
78
    addressFilter.current.forEach(({ name, type }) => {
79
      urlParams[type] = name
5122 stevensc 80
    })
1 www 81
    activeFilters.current.forEach(({ name, value }) => {
82
      urlParams[name] = value
5122 stevensc 83
    })
1 www 84
    await axios
85
      .get(`/search/entity/${searchType}?${jsonToParams(urlParams)}`)
86
      .then((response) => {
5122 stevensc 87
        const responseData = response.data
1 www 88
        if (responseData.success) {
5122 stevensc 89
          setEntities(responseData.data.current.items)
90
          setPages(responseData.data.total.pages)
1 www 91
        }
5122 stevensc 92
      })
93
    setLoading(false)
94
  }
1 www 95
 
96
  const getAddressHandler = (addresObject) => {
5122 stevensc 97
    const { address_components } = addresObject
1 www 98
    if (address_components) {
5122 stevensc 99
      addressFilter.current = []
1 www 100
      address_components.map((address_component) => {
5122 stevensc 101
        const address_component_name = address_component.long_name
102
        const address_component_type = address_component.types[0]
1 www 103
        switch (address_component_type) {
5122 stevensc 104
          case 'route':
1 www 105
            addressFilter.current = [
106
              ...addressFilter.current,
6363 stevensc 107
              { type: 'route', name: address_component_name },
5122 stevensc 108
            ]
109
            break
110
          case 'sublocality':
1 www 111
            addressFilter.current = [
112
              ...addressFilter.current,
6363 stevensc 113
              { type: 'sublocality', name: address_component_name },
5122 stevensc 114
            ]
115
            break
116
          case 'locality':
1 www 117
            addressFilter.current = [
118
              ...addressFilter.current,
6363 stevensc 119
              { type: 'locality', name: address_component_name },
5122 stevensc 120
            ]
121
            break
122
          case 'administrative_area_level_2':
1 www 123
            addressFilter.current = [
124
              ...addressFilter.current,
125
              {
5122 stevensc 126
                type: 'administrative_area_level_2',
6363 stevensc 127
                name: address_component_name,
128
              },
5122 stevensc 129
            ]
130
            break
131
          case 'administrative_area_level_1':
1 www 132
            addressFilter.current = [
133
              ...addressFilter.current,
134
              {
5122 stevensc 135
                type: 'administrative_area_level_1',
6363 stevensc 136
                name: address_component_name,
137
              },
5122 stevensc 138
            ]
139
            break
140
          case 'country':
1 www 141
            addressFilter.current = [
142
              ...addressFilter.current,
6363 stevensc 143
              { type: 'country', name: address_component_name },
5122 stevensc 144
            ]
145
            break
146
          case 'postal_code':
1 www 147
            addressFilter.current = [
148
              ...addressFilter.current,
6363 stevensc 149
              { type: 'postal_code', name: address_component_name },
5122 stevensc 150
            ]
151
            break
1 www 152
          default:
5122 stevensc 153
            break
1 www 154
        }
5122 stevensc 155
      })
156
      loadEntities()
1 www 157
    } else {
158
      if (addressFilter.current.length) {
5122 stevensc 159
        addressFilter.current = []
160
        loadEntities()
1 www 161
      }
162
    }
5122 stevensc 163
  }
1 www 164
 
165
  const onChangePageHandler = (currentPage) => {
5122 stevensc 166
    setCurrentPage(currentPage)
167
  }
1 www 168
 
169
  return (
5389 stevensc 170
    <main className="main-section-data container px-0 mt-3">
6363 stevensc 171
      <FiltersSidebar
172
        filtersOptions={filters}
173
        getAddressHandler={getAddressHandler}
174
        onCheckHandler={onCheckHandler}
175
        onUncheckAllHandler={onUncheckAllHandler}
176
        filters={searchType}
177
        setFilters={(value) => setSearchType(value)}
178
        register={register}
179
      />
5388 stevensc 180
      <div className="main-ws-sec">
181
        <div className="posts-section">
182
          {loading && <Spinner />}
6363 stevensc 183
          {entities.length ? (
184
            entities.map((entity) => (
5390 stevensc 185
              <EntityTemplate
186
                entity={entity}
187
                key={entity.id}
188
                onChangePage={onChangePageHandler}
189
              />
5388 stevensc 190
            ))
6363 stevensc 191
          ) : (
192
            <EmptySection message="No hay resultados" />
193
          )}
5388 stevensc 194
        </div>
195
        <PaginationComponent
196
          pages={pages}
197
          currentActivePage={currentPage}
198
          onChangePage={loadEntities}
199
        />
200
      </div>
201
      <div className="right-sidebar" />
202
    </main>
5122 stevensc 203
  )
204
}
1 www 205
 
5122 stevensc 206
export default MainSection