Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1312 | Rev 2803 | 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 ProfileItem from '../../components/profile/ProfileItem'
10
import TitleSection from '../../components/UI/TitleSection'
11
import EmptySection from '../../components/UI/EmptySection'
12
import LoaderContainer from '../../components/UI/LoaderContainer'
13
 
14
const CompaniesWhenIWorkPage = () => {
15
  const [companiesWhenIWork, setCompaniesWhenIWork] = useState([])
16
  const [loading, setLoading] = useState(false)
17
  const [search, setSearch] = useState('')
18
 
19
  const labels = useSelector(({ intl }) => intl.labels)
20
  const dispatch = useDispatch()
21
 
22
  const getCompaniesWhenIWork = async (search = '') => {
23
    setLoading(true)
24
    try {
25
      const { success, data } = await searchEntities(
26
        'company/i-work-with',
27
        search
28
      )
29
 
30
      if (!success) {
31
        dispatch(addNotification({ style: 'danger', msg: data }))
32
        setLoading(false)
33
        return
34
      }
35
 
36
      setCompaniesWhenIWork(data)
37
    } catch (error) {
2194 stevensc 38
      dispatch(addNotification({ style: 'danger', msg: error.message }))
5 stevensc 39
    } finally {
40
      setLoading(false)
41
    }
42
  }
43
 
44
  const handleSearch = debounce((value) => setSearch(value), 500)
45
 
46
  useEffect(() => {
47
    getCompaniesWhenIWork(search)
48
  }, [search])
49
 
50
  return (
1312 stevensc 51
    <main className='companies-info container'>
5 stevensc 52
      <TitleSection title={labels.companies_i_work_with} />
53
      <SearchBar onChange={handleSearch} />
54
      {loading ? (
55
        <LoaderContainer>
56
          <Spinner />
57
        </LoaderContainer>
58
      ) : (
1312 stevensc 59
        <ul className='companies-list'>
5 stevensc 60
          {companiesWhenIWork.length ? (
61
            companiesWhenIWork.map(({ id, link_my_company, ...rest }) => (
62
              <ProfileItem
63
                key={id}
64
                link_admin={link_my_company}
65
                fetchCallback={getCompaniesWhenIWork}
66
                btnAcceptTitle={labels.view_company}
1312 stevensc 67
                btnLeaveTitle={labels.company_leave}
5 stevensc 68
                {...rest}
69
              />
70
            ))
71
          ) : (
72
            <EmptySection
1312 stevensc 73
              align='left'
5 stevensc 74
              message={labels.datatable_szerorecords}
75
            />
76
          )}
77
        </ul>
78
      )}
79
    </main>
80
  )
81
}
82
 
83
export default CompaniesWhenIWorkPage