Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1216 | Rev 2154 | 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 { addNotification } from '../../redux/notification/notification.actions'
5
import { searchEntities } from '../../services/items'
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
import AddCompanyModal from '../../components/modals/AddCompanyModal'
14
 
15
const MyCompanies = () => {
16
  const [companies, setCompanies] = useState([])
17
  const [loading, setLoading] = useState(true)
18
  const [search, setSearch] = useState('')
19
  const [showAddCompanyModal, setShowCompanyModal] = useState(false)
20
  const labels = useSelector(({ intl }) => intl.labels)
21
  const dispatch = useDispatch()
22
 
23
  const getMyCompanies = async (searchValue = '') => {
24
    setLoading(true)
25
    const response = await searchEntities('company/my-companies', searchValue)
26
 
27
    if (!response.success) {
28
      dispatch(addNotification({ style: 'danger', msg: response.data }))
29
      setLoading(false)
30
      return
31
    }
32
 
33
    setCompanies(response.data)
34
    setLoading(false)
35
  }
36
 
37
  const toggleShowCompanyModal = () => {
38
    setShowCompanyModal(!showAddCompanyModal)
39
  }
40
 
41
  const handleSearch = debounce((value) => setSearch(value), 500)
42
 
43
  useEffect(() => {
2072 stevensc 44
    getMyCompanies(search)
5 stevensc 45
  }, [search])
46
 
47
  return (
1215 stevensc 48
    <main className='companies-info container'>
5 stevensc 49
      <TitleSection
50
        title={labels.my_companies}
51
        onAdd={toggleShowCompanyModal}
1216 stevensc 52
        addLabel={`${labels.add} ${labels.company.toLowerCase()}`}
5 stevensc 53
      />
2072 stevensc 54
 
5 stevensc 55
      <SearchBar onChange={handleSearch} />
2072 stevensc 56
 
5 stevensc 57
      {loading ? (
58
        <LoaderContainer>
59
          <Spinner />
60
        </LoaderContainer>
61
      ) : (
1215 stevensc 62
        <ul className='companies-list'>
5 stevensc 63
          {companies.length ? (
64
            companies.map(({ id, link_my_company, ...rest }) => (
65
              <ProfileItem
66
                key={id}
67
                link_admin={link_my_company}
68
                btnAcceptTitle={labels.view_company}
69
                {...rest}
70
              />
71
            ))
72
          ) : (
73
            <EmptySection
1215 stevensc 74
              align='left'
5 stevensc 75
              message={labels.datatable_szerorecords}
76
            />
77
          )}
78
        </ul>
79
      )}
80
      <AddCompanyModal
81
        show={showAddCompanyModal}
82
        onHide={toggleShowCompanyModal}
83
        fetchCompanies={getMyCompanies}
84
      />
85
    </main>
86
  )
87
}
88
 
89
export default MyCompanies