Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
5 stevensc 1
import React, { useEffect, useState } from 'react'
2
import { useDispatch, useSelector } from 'react-redux'
3
import { debounce } from '../../utils'
4
import { searchEntities } from '../../services/items'
5
import { addNotification } from '../../redux/notification/notification.actions'
6
 
7
import Spinner from '../../components/UI/Spinner'
8
import SearchBar from '../../components/UI/SearchBar'
9
import TitleSection from '../../components/UI/TitleSection'
10
import EmptySection from '../../components/UI/EmptySection'
11
import ProfileItem from '../../components/profile/ProfileItem'
12
import PaginationComponent from '../../components/UI/PaginationComponent'
13
import LoaderContainer from '../../components/UI/LoaderContainer'
14
 
15
const MyConnectionsPage = () => {
16
  const [myConnections, setMyConnections] = useState([])
17
  const [currentPage, setCurrentPage] = useState(1)
18
  const [totalPages, setTotalPages] = useState(1)
19
  const [loading, setLoading] = useState(false)
20
  const [search, setSearch] = useState('')
21
 
22
  const labels = useSelector(({ intl }) => intl.labels)
23
  const dispatch = useDispatch()
24
 
25
  const getMyConnections = async (search = '', page = 1) => {
26
    setLoading(true)
27
    try {
28
      const { success, data } = await searchEntities(
29
        'connection/my-connections',
30
        search,
31
        page
32
      )
33
 
34
      if (!success) {
35
        dispatch(addNotification({ style: 'danger', msg: data }))
36
        setLoading(false)
37
        return
38
      }
39
 
40
      setMyConnections(data.current.items)
41
      setCurrentPage(data.current.page)
42
      setTotalPages(data.total.pages)
43
    } catch (error) {
2194 stevensc 44
      dispatch(addNotification({ style: 'danger', msg: error.message }))
5 stevensc 45
    } finally {
46
      setLoading(false)
47
    }
48
  }
49
 
50
  const handleSearch = debounce((value) => setSearch(value), 500)
51
 
52
  const onChangePageHandler = (currentPage) => {
53
    setCurrentPage(currentPage)
54
  }
55
 
56
  useEffect(() => {
57
    getMyConnections(search, currentPage)
58
  }, [search, currentPage])
59
 
60
  return (
2194 stevensc 61
    <main className='companies-info container'>
5 stevensc 62
      <TitleSection title={labels.first_level_persons} />
63
      <SearchBar onChange={handleSearch} />
64
      {loading ? (
65
        <LoaderContainer>
66
          <Spinner />
67
        </LoaderContainer>
68
      ) : (
2194 stevensc 69
        <ul className='companies-list'>
5 stevensc 70
          {myConnections.length ? (
71
            myConnections.map(({ id, ...rest }) => (
72
              <ProfileItem
73
                key={id}
74
                isTopData
75
                {...rest}
76
                fetchCallback={getMyConnections}
77
              />
78
            ))
79
          ) : (
80
            <EmptySection
2194 stevensc 81
              align='left'
5 stevensc 82
              message={labels.datatable_szerorecords}
83
            />
84
          )}
85
        </ul>
86
      )}
87
      <PaginationComponent
88
        isRow
89
        pages={totalPages}
90
        currentActivePage={currentPage}
91
        onChangePage={onChangePageHandler}
92
      />
93
    </main>
94
  )
95
}
96
 
97
export default MyConnectionsPage