Proyectos de Subversion LeadersLinked - SPA

Rev

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