Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 7550 | Ir a la última revisión | | 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)
41
      .then((response) => {
42
        const resData = response.data;
43
        if (resData.success) {
44
          setFollowers(resData.data.current.items);
45
          setPages(resData.data.total.pages);
46
        }
47
      });
48
    setLoading(false);
49
  };
50
 
51
  const handleSearch = () => {
52
    //  (getValues());
53
    clearTimeout(axiosThrottle);
54
    // setLoading(true);
55
    axiosThrottle = setTimeout(() => {
56
      fetchFollowers();
57
    }, 500);
58
  };
59
 
60
  const handleChangePage = (newPage) => {
61
    setCurrentPage(newPage);
62
  };
63
 
64
  return (
65
    <section className="companies-info">
66
      <div className="container">
67
        <div className="company-title">
68
          <div className="section_admin_title_buttons">
69
            <div style={{ float: "left" }}>
70
              <h1 className="title">Seguidores</h1>
71
            </div>
72
          </div>
73
        </div>
74
 
75
        <div className="company-title">
76
          <div className="section_admin_title_buttons">
77
            <form
78
              name="form-connection-search"
79
              id="form-connection-search"
80
              onSubmit={(event) => event.preventDefault()}
81
            >
82
              <div className="form-group">
83
                <input
84
                  type="text"
85
                  name="search"
86
                  id="search"
87
                  className="form-control"
88
                  placeholder="Buscar"
89
                  ref={register}
90
                  onChange={handleSearch}
91
                />
92
              </div>
93
            </form>
94
          </div>
95
        </div>
96
 
97
        <div className="companies-list">
98
          <div
99
            className="row"
100
            id="profiles-container"
101
            style={{
102
              position: "relative",
103
            }}
104
          >
105
            {followers.map(({ image, name, link_view, link_inmail }, index) => (
106
              <Follower
107
                key={index}
108
                image={image}
109
                name={name}
110
                link_view={link_view}
111
                link_inmail={link_inmail}
112
              />
113
            ))}
114
            <PaginationComponent
115
              pages={pages}
116
              currentPage={currentPage}
117
              onChangePage={handleChangePage}
118
            />
119
          </div>
120
          {/* <!--posts-section end--> */}
121
        </div>
122
      </div>
123
    </section>
124
  );
125
};
126
 
127
export default Followers;