Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
import React from "react";
2
import { useEffect, useState } from "react";
3
import { connect } from "react-redux";
4
import { useForm } from "react-hook-form";
5
import styled from "styled-components";
6
import {axios} from "../../../../utils";
7
import { addNotification } from "../../../../redux/notification/notification.actions";
8
import Follower from "./follower/Follower";
9
import PaginationComponent from "../../../../shared/pagination/PaginationComponent";
10
import Spinner from "../../../../shared/loading-spinner/Spinner";
11
 
12
const StyledSpinnerContainer = styled.div`
13
  position: absolute;
14
  left: 0;
15
  top: 0;
16
  width: 100%;
17
  height: 100%;
18
  background: rgba(255, 255, 255, 0.4);
19
  display: flex;
20
  justify-content: center;
21
  align-items: center;
22
  z-index: 300;
23
`;
24
 
25
const Followers = (props) => {
26
  // backendVars destructuring
27
  const { companyId } = props.backendVars;
28
  // states
29
  const [followers, setFollowers] = useState([]);
30
  const [loading, setLoading] = useState(true);
31
  const [pages, setPages] = useState(1);
32
  const [currentPage, setCurrentPage] = useState(1);
33
 
34
  // redux destructuring
35
  const { addNotification } = props;
36
 
37
  // React hook form
38
  const { register, getValues } = useForm();
39
 
40
  let axiosThrottle = null;
41
 
42
  useEffect(() => {
43
    fetchFollowers();
44
    return () => {
45
      clearTimeout(axiosThrottle);
46
    };
47
  }, [currentPage]);
48
 
49
  const fetchFollowers = async () => {
50
    setLoading(true);
51
    let params = {
52
      params: {
53
        page: currentPage,
54
      },
55
    };
56
    if (getValues("search")) {
57
      params = { params: { ...params.params, search: getValues("search") } };
58
    }
59
    await axios
60
      .get(`/my-company/${companyId}/follower`, params)
61
      .then((response) => {
62
        const resData = response.data;
63
        if (resData.success) {
64
          setFollowers(resData.data.current.items);
65
          setPages(resData.data.total.pages);
66
        }
67
      });
68
    setLoading(false);
69
  };
70
 
71
  const handleSearch = () => {
72
    //  (getValues());
73
    clearTimeout(axiosThrottle);
74
    // setLoading(true);
75
    axiosThrottle = setTimeout(() => {
76
      fetchFollowers();
77
    }, 500);
78
  };
79
 
80
  const handleChangePage = (newPage) => {
81
    setCurrentPage(newPage);
82
  };
83
 
84
  return (
85
    <section className="companies-info">
86
      <div className="container">
87
        <div className="company-title">
88
          <div className="section_admin_title_buttons">
89
            <div style={{ float: "left" }}>
90
              <h1 className="title">Seguidores</h1>
91
            </div>
92
          </div>
93
        </div>
94
 
95
        <div className="company-title">
96
          <div className="section_admin_title_buttons">
97
            <form
98
              name="form-connection-search"
99
              id="form-connection-search"
100
              onSubmit={(event) => event.preventDefault()}
101
            >
102
              <div className="form-group">
103
                <input
104
                  type="text"
105
                  name="search"
106
                  id="search"
107
                  className="form-control"
108
                  placeholder="Buscar"
109
                  ref={register}
110
                  onChange={handleSearch}
111
                />
112
              </div>
113
            </form>
114
          </div>
115
        </div>
116
 
117
        <div className="companies-list">
118
          <div
119
            className="row"
120
            id="profiles-container"
121
            style={{
122
              position: "relative",
123
            }}
124
          >
125
            {followers.map(({ image, name, link_view, link_inmail }, index) => (
126
              <Follower
127
                key={index}
128
                image={image}
129
                name={name}
130
                link_view={link_view}
131
                link_inmail={link_inmail}
132
              />
133
            ))}
134
            <PaginationComponent
135
              pages={pages}
136
              currentPage={currentPage}
137
              onChangePage={handleChangePage}
138
            />
139
            {loading && (
140
              <StyledSpinnerContainer>
141
                <Spinner />
142
              </StyledSpinnerContainer>
143
            )}
144
          </div>
145
          {/* <!--posts-section end--> */}
146
        </div>
147
      </div>
148
    </section>
149
  );
150
};
151
 
152
const mapDispatchToProps = {
153
  addNotification: (notification) => addNotification(notification),
154
};
155
 
156
export default connect(null, mapDispatchToProps)(Followers);