Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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