| 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 AddProfileModal from '../../components/modals/AddProfileModal'
|
|
|
13 |
import LoaderContainer from '../../components/UI/LoaderContainer'
|
|
|
14 |
|
|
|
15 |
const MyProfilesPage = () => {
|
|
|
16 |
const [isShowAddModal, setIsShowAddModal] = useState(false)
|
|
|
17 |
const [myProfiles, setMyProfiles] = 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 getMyProfiles = async (search = '') => {
|
|
|
25 |
setLoading(true)
|
|
|
26 |
try {
|
|
|
27 |
const { success, data } = await searchEntities(
|
|
|
28 |
'profile/my-profiles',
|
|
|
29 |
search
|
|
|
30 |
)
|
|
|
31 |
|
|
|
32 |
if (!success) {
|
|
|
33 |
dispatch(addNotification({ style: 'danger', msg: data }))
|
|
|
34 |
setLoading(false)
|
|
|
35 |
return
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
setMyProfiles(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 |
const toggleModal = () => {
|
|
|
49 |
setIsShowAddModal(!isShowAddModal)
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
useEffect(() => {
|
|
|
53 |
getMyProfiles(search)
|
|
|
54 |
}, [search])
|
|
|
55 |
|
|
|
56 |
return (
|
| 2194 |
stevensc |
57 |
<main className='companies-info container'>
|
| 5 |
stevensc |
58 |
<TitleSection
|
|
|
59 |
title={labels.my_profiles}
|
|
|
60 |
onAdd={toggleModal}
|
|
|
61 |
addLabel={labels.add}
|
|
|
62 |
/>
|
|
|
63 |
<SearchBar onChange={handleSearch} />
|
|
|
64 |
{loading ? (
|
|
|
65 |
<LoaderContainer>
|
|
|
66 |
<Spinner />
|
|
|
67 |
</LoaderContainer>
|
|
|
68 |
) : (
|
| 2194 |
stevensc |
69 |
<ul className='companies-list'>
|
| 5 |
stevensc |
70 |
{myProfiles.length ? (
|
|
|
71 |
myProfiles.map(({ id, ...rest }) => (
|
|
|
72 |
<ProfileItem key={id} {...rest} fetchCallback={getMyProfiles} />
|
|
|
73 |
))
|
|
|
74 |
) : (
|
|
|
75 |
<EmptySection
|
| 2194 |
stevensc |
76 |
align='left'
|
| 5 |
stevensc |
77 |
message={labels.datatable_szerorecords}
|
|
|
78 |
/>
|
|
|
79 |
)}
|
|
|
80 |
</ul>
|
|
|
81 |
)}
|
|
|
82 |
<AddProfileModal
|
|
|
83 |
show={isShowAddModal}
|
|
|
84 |
getProfiles={() => getMyProfiles()}
|
|
|
85 |
onHide={toggleModal}
|
|
|
86 |
/>
|
|
|
87 |
</main>
|
|
|
88 |
)
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
export default MyProfilesPage
|