Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6738 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6738 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 LoaderContainer from '../../components/UI/LoaderContainer'
13
import AddGroupModal from '../../components/modals/AddGroupModal'
14
 
15
const MyGroupsPage = () => {
16
  const [isShowAddModal, setIsShowAddModal] = useState(false)
17
  const [myGroups, setMyGroups] = 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 getMyGroups = async (search = '') => {
25
    setLoading(true)
26
    try {
27
      const { success, data } = await searchEntities('group/my-groups', search)
28
 
29
      if (!success) {
30
        dispatch(addNotification({ style: 'danger', msg: data }))
31
        setLoading(false)
32
        return
33
      }
34
 
35
      setMyGroups(data)
36
    } catch (error) {
37
      console.log(error)
38
      throw new Error(error)
39
    } finally {
40
      setLoading(false)
41
    }
42
  }
43
 
44
  const handleSearch = debounce((value) => setSearch(value), 500)
45
 
46
  const toggleModal = () => {
47
    setIsShowAddModal(!isShowAddModal)
48
  }
49
 
50
  useEffect(() => {
51
    getMyGroups(search)
52
  }, [search])
53
 
54
  return (
55
    <main className="companies-info container">
56
      <TitleSection
6742 stevensc 57
        title={labels.my_groups}
6738 stevensc 58
        onAdd={toggleModal}
6742 stevensc 59
        addLabel={labels.create_group}
6738 stevensc 60
      />
61
      <SearchBar onChange={handleSearch} />
62
      {loading ? (
63
        <LoaderContainer>
64
          <Spinner />
65
        </LoaderContainer>
66
      ) : (
67
        <ul className="companies-list">
68
          {myGroups.length ? (
69
            myGroups.map(({ id, privacy, ...rest }) => (
70
              <ProfileItem
71
                key={id}
72
                {...rest}
73
                status={privacy}
74
                btnAcceptTitle={labels.group_view}
75
                btnEditTitle={`${labels.edit} ${labels.group?.toLowerCase()}`}
76
                btnCancelTitle={`${
77
                  labels.delete
78
                } ${labels.group?.toLowerCase()}`}
79
                fetchCallback={getMyGroups}
80
              />
81
            ))
82
          ) : (
83
            <EmptySection
84
              align="left"
85
              message={labels.datatable_szerorecords}
86
            />
87
          )}
88
        </ul>
89
      )}
90
      <AddGroupModal
91
        show={isShowAddModal}
92
        groupTypes={{}}
93
        industries={{}}
94
        onHide={toggleModal}
95
        fetchGroups={getMyGroups}
96
      />
97
    </main>
98
  )
99
}
100
 
101
export default MyGroupsPage