Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5072 stevensc 1
/* eslint-disable camelcase */
2
import React, { useEffect, useState } from 'react'
3
import { useForm } from 'react-hook-form'
4
import { connect } from 'react-redux'
5
import { axios, jsonToParams } from '../../utils'
6
import { addNotification } from '../../redux/notification/notification.actions'
7
import Profile from '../../components/Profile'
8
import Spinner from '../../shared/loading-spinner/Spinner'
9
import PaginationComponent from '../../shared/pagination/PaginationComponent'
10
 
11
const Impersonate = () => {
12
  const [users, setUsers] = useState([])
13
  const [loading, setLoading] = useState(true)
14
  const [error, setError] = useState('')
15
  const [currentPage, setCurrentPage] = useState(1)
16
  const [pages, setPages] = useState(1)
17
  const { register, getValues } = useForm()
18
 
19
  useEffect(() => {
20
    fetchUsers()
21
  }, [])
22
 
23
  const handleSearch = () => {
24
    const searchValue = getValues('search')
25
    setTimeout(() => fetchUsers(1, searchValue), [500])
26
  }
27
 
28
  const fetchUsers = async (page = 1, keyword = '') => {
29
    setLoading(true)
30
    setError(false)
31
    setCurrentPage(page)
32
    const urlParams = {
33
      page,
34
      keyword
35
    }
36
 
37
    await axios
38
      .get(`/impersonate?${jsonToParams(urlParams)}`)
39
      .then((response) => {
40
        const responseData = response.data
41
        if (responseData.success) {
42
          setUsers(responseData.data.current.items)
43
          setPages(responseData.data.total.pages)
44
        } else {
45
          const newError = responseData.data
46
          setError(newError)
47
        }
48
      })
49
    setLoading(false)
50
  }
51
 
52
  return (
53
    <section className="companies-info">
54
      <div className="container">
55
        <div className="company-title">
56
          <div className="section_admin_title_buttons">
57
            <h1 className="title">Usuarios disponibles a personalizar</h1>
58
          </div>
59
        </div>
60
        <div className="company-title">
61
          <div className="search-box border-gray border-radius">
62
            <div className="form-group">
63
              <input
64
                type="text"
65
                name="search"
66
                id="search"
67
                className="form-control"
68
                placeholder=""
69
                ref={register}
70
                onKeyUp={handleSearch}
71
              />
72
            </div>
73
          </div>
74
        </div>
75
 
76
        <div className="companies-list position-relative" id="profiles-container">
77
          {users.length > 0
78
            ? (users.map(({ image, name, email, network, link_impersonate }, id) => (
79
              <Profile
80
                isTopData
81
                key={id}
82
                image={image}
83
                name={name}
84
                email={email}
85
                network={network}
86
                link_impersonate={link_impersonate}
87
              />)))
88
            : <p>No hay resultados</p>
89
          }
90
          {loading && <Spinner />}
91
        </div>
92
      </div>
93
      <PaginationComponent
94
        pages={pages}
95
        currentActivePage={currentPage}
96
        onChangePage={fetchUsers}
97
      />
98
    </section>
99
  )
100
}
101
 
102
const mapDispatchToProps = {
103
  addNotification: (notification) => addNotification(notification)
104
}
105
 
106
export default connect(null, mapDispatchToProps)(Impersonate)