Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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