Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6886 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6734 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'
6727 stevensc 6
 
6734 stevensc 7
import Spinner from '../../components/UI/Spinner'
8
import SearchBar from '../../components/UI/SearchBar'
9
import ProfileItem from '../../components/profile/ProfileItem'
6727 stevensc 10
import TitleSection from '../../components/UI/TitleSection'
6734 stevensc 11
import EmptySection from '../../components/UI/EmptySection'
6727 stevensc 12
import AddProfileModal from '../../components/modals/AddProfileModal'
6736 stevensc 13
import LoaderContainer from '../../components/UI/LoaderContainer'
6727 stevensc 14
 
15
const MyProfilesPage = () => {
16
  const [isShowAddModal, setIsShowAddModal] = useState(false)
6738 stevensc 17
  const [myProfiles, setMyProfiles] = useState([])
6734 stevensc 18
  const [loading, setLoading] = useState(false)
19
  const [search, setSearch] = useState('')
6738 stevensc 20
 
6727 stevensc 21
  const labels = useSelector(({ intl }) => intl.labels)
6734 stevensc 22
  const dispatch = useDispatch()
6727 stevensc 23
 
6734 stevensc 24
  const getMyProfiles = async (search = '') => {
25
    setLoading(true)
26
    try {
27
      const { success, data } = await searchEntities(
28
        'profile/my-profiles',
29
        search
30
      )
6727 stevensc 31
 
6734 stevensc 32
      if (!success) {
33
        dispatch(addNotification({ style: 'danger', msg: data }))
34
        setLoading(false)
35
        return
36
      }
37
 
38
      setMyProfiles(data)
39
    } catch (error) {
40
      console.log(error)
41
      throw new Error(error)
42
    } finally {
43
      setLoading(false)
44
    }
45
  }
46
 
47
  const handleSearch = debounce((value) => setSearch(value), 500)
48
 
6731 stevensc 49
  const toggleModal = () => {
50
    setIsShowAddModal(!isShowAddModal)
51
  }
6727 stevensc 52
 
6734 stevensc 53
  useEffect(() => {
54
    getMyProfiles(search)
55
  }, [search])
56
 
6727 stevensc 57
  return (
58
    <main className="companies-info container">
59
      <TitleSection
60
        title={labels.my_profiles}
6732 stevensc 61
        onAdd={toggleModal}
6727 stevensc 62
        addLabel={labels.add}
63
      />
6734 stevensc 64
      <SearchBar onChange={handleSearch} />
6888 stevensc 65
 
66
      <LoaderContainer>
67
        <Spinner />
68
      </LoaderContainer>
69
 
70
      <ul className="companies-list">
71
        {myProfiles.length ? (
72
          myProfiles.map(({ id, ...rest }) => (
73
            <ProfileItem key={id} {...rest} fetchCallback={getMyProfiles} />
74
          ))
75
        ) : (
76
          <EmptySection align="left" message={labels.datatable_szerorecords} />
77
        )}
78
      </ul>
79
 
6732 stevensc 80
      <AddProfileModal
81
        show={isShowAddModal}
6886 stevensc 82
        getProfiles={() => getMyProfiles()}
6732 stevensc 83
        onHide={toggleModal}
84
      />
6727 stevensc 85
    </main>
86
  )
87
}
88
 
89
export default MyProfilesPage