6753 |
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)
|
6861 |
stevensc |
18 |
const [search, setSearch] = useState('')
|
6753 |
stevensc |
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(() => {
|
|
|
44 |
getMyCompanies()
|
|
|
45 |
}, [search])
|
|
|
46 |
|
|
|
47 |
return (
|
|
|
48 |
<main className="companies-info container">
|
|
|
49 |
<TitleSection
|
|
|
50 |
title={labels.my_companies}
|
|
|
51 |
onAdd={toggleShowCompanyModal}
|
|
|
52 |
addLabel={labels.add_company}
|
|
|
53 |
/>
|
|
|
54 |
<SearchBar onChange={handleSearch} />
|
|
|
55 |
{loading ? (
|
|
|
56 |
<LoaderContainer>
|
|
|
57 |
<Spinner />
|
|
|
58 |
</LoaderContainer>
|
|
|
59 |
) : (
|
|
|
60 |
<ul className="companies-list">
|
|
|
61 |
{companies.length ? (
|
|
|
62 |
companies.map(({ id, link_my_company, ...rest }) => (
|
|
|
63 |
<ProfileItem
|
|
|
64 |
key={id}
|
|
|
65 |
link_admin={link_my_company}
|
|
|
66 |
btnAcceptTitle={labels.view_company}
|
|
|
67 |
{...rest}
|
|
|
68 |
/>
|
|
|
69 |
))
|
|
|
70 |
) : (
|
|
|
71 |
<EmptySection
|
|
|
72 |
align="left"
|
|
|
73 |
message={labels.datatable_szerorecords}
|
|
|
74 |
/>
|
|
|
75 |
)}
|
|
|
76 |
</ul>
|
|
|
77 |
)}
|
|
|
78 |
<AddCompanyModal
|
|
|
79 |
show={showAddCompanyModal}
|
|
|
80 |
onHide={toggleShowCompanyModal}
|
|
|
81 |
fetchCompanies={getMyCompanies}
|
|
|
82 |
/>
|
|
|
83 |
</main>
|
|
|
84 |
)
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
export default MyCompanies
|