Rev 6732 | Rev 6736 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useEffect, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { debounce } from '../../utils'
import { searchEntities } from '../../services/items'
import { addNotification } from '../../redux/notification/notification.actions'
import Spinner from '../../components/UI/Spinner'
import SearchBar from '../../components/UI/SearchBar'
import ProfileItem from '../../components/profile/ProfileItem'
import TitleSection from '../../components/UI/TitleSection'
import EmptySection from '../../components/UI/EmptySection'
import AddProfileModal from '../../components/modals/AddProfileModal'
const MyProfilesPage = () => {
const [isShowAddModal, setIsShowAddModal] = useState(false)
const [loading, setLoading] = useState(false)
const [myProfiles, setMyProfiles] = useState([])
const [search, setSearch] = useState('')
const labels = useSelector(({ intl }) => intl.labels)
const dispatch = useDispatch()
const getMyProfiles = async (search = '') => {
setLoading(true)
try {
const { success, data } = await searchEntities(
'profile/my-profiles',
search
)
if (!success) {
dispatch(addNotification({ style: 'danger', msg: data }))
setLoading(false)
return
}
setMyProfiles(data)
} catch (error) {
console.log(error)
throw new Error(error)
} finally {
setLoading(false)
}
}
const handleSearch = debounce((value) => setSearch(value), 500)
const toggleModal = () => {
setIsShowAddModal(!isShowAddModal)
}
useEffect(() => {
getMyProfiles(search)
}, [search])
return (
<main className="companies-info container">
<TitleSection
title={labels.my_profiles}
onAdd={toggleModal}
addLabel={labels.add}
/>
<SearchBar onChange={handleSearch} />
{loading ? (
<Spinner />
) : (
<ul className="companies-list">
{myProfiles.length ? (
myProfiles.map(({ id, link_edit, link_delete, ...rest }) => (
<ProfileItem
key={id}
{...rest}
link_edit={link_edit}
link_delete={link_delete}
fetchCallback={getMyProfiles}
/>
))
) : (
<EmptySection
align="left"
message={labels.datatable_szerorecords}
/>
)}
</ul>
)}
<AddProfileModal
show={isShowAddModal}
getProfiles={getMyProfiles}
onHide={toggleModal}
/>
</main>
)
}
export default MyProfilesPage