Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
7545 stevensc 1
import React from "react";
2
import { useEffect, useState } from "react";
3
import { useForm } from "react-hook-form";
4
import axios from "axios";
5
import Follower from "./follower/Follower";
6
import PaginationComponent from "../../shared/PaginationComponent";
7
 
8
const Followers = ({ backendVars }) => {
9
  // backendVars destructuring
10
  const { table_link } = backendVars;
11
  // states
12
  const [followers, setFollowers] = useState([]);
13
  const [loading, setLoading] = useState(true);
14
  const [pages, setPages] = useState(1);
15
  const [currentPage, setCurrentPage] = useState(1);
16
 
17
  // React hook form
18
  const { register, getValues } = useForm();
19
 
20
  let axiosThrottle = null;
21
 
22
  useEffect(() => {
23
    fetchFollowers();
24
    return () => {
25
      clearTimeout(axiosThrottle);
26
    };
27
  }, [currentPage]);
28
 
29
  const fetchFollowers = async () => {
30
    setLoading(true);
31
    let params = {
32
      params: {
33
        page: currentPage,
34
      },
35
    };
36
    if (getValues("search")) {
37
      params = { params: { ...params.params, search: getValues("search") } };
38
    }
39
    await axios
40
      .get(table_link, params)
7550 stevensc 41
      .then(({ data }) => {
42
        if (data.success) {
43
          setFollowers(data.data.items);
7545 stevensc 44
        }
45
      });
46
    setLoading(false);
47
  };
48
 
49
  const handleSearch = () => {
50
    //  (getValues());
51
    clearTimeout(axiosThrottle);
52
    // setLoading(true);
53
    axiosThrottle = setTimeout(() => {
54
      fetchFollowers();
55
    }, 500);
56
  };
57
 
58
  const handleChangePage = (newPage) => {
59
    setCurrentPage(newPage);
60
  };
61
 
62
  return (
63
    <section className="companies-info">
64
      <div className="container">
65
        <div className="company-title">
66
          <div className="section_admin_title_buttons">
67
            <div style={{ float: "left" }}>
68
              <h1 className="title">Seguidores</h1>
69
            </div>
70
          </div>
71
        </div>
72
 
73
        <div className="company-title">
74
          <div className="section_admin_title_buttons">
75
            <form
76
              name="form-connection-search"
77
              id="form-connection-search"
78
              onSubmit={(event) => event.preventDefault()}
79
            >
80
              <div className="form-group">
81
                <input
82
                  type="text"
83
                  name="search"
84
                  id="search"
85
                  className="form-control"
86
                  placeholder="Buscar"
87
                  ref={register}
88
                  onChange={handleSearch}
89
                />
90
              </div>
91
            </form>
92
          </div>
93
        </div>
94
 
95
        <div className="companies-list">
96
          <div
97
            className="row"
98
            id="profiles-container"
99
            style={{
100
              position: "relative",
101
            }}
102
          >
103
            {followers.map(({ image, name, link_view, link_inmail }, index) => (
104
              <Follower
105
                key={index}
106
                image={image}
107
                name={name}
108
                link_view={link_view}
109
                link_inmail={link_inmail}
110
              />
111
            ))}
112
            <PaginationComponent
113
              pages={pages}
114
              currentPage={currentPage}
115
              onChangePage={handleChangePage}
116
            />
117
          </div>
118
          {/* <!--posts-section end--> */}
119
        </div>
120
      </div>
121
    </section>
122
  );
123
};
124
 
125
export default Followers;