Proyectos de Subversion LeadersLinked - SPA

Rev

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