Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1564 | Ir a la última revisión | | 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 Spinner from "../../../shared/loading-spinner/Spinner";
9
import Entity from "./entity/Entity";
10
 
11
const StyledSpinnerContainer = styled.div`
12
  position: absolute;
13
  left: 0;
14
  top: 0;
15
  width: 100%;
16
  height: 100%;
17
  background: rgba(255, 255, 255, 0.4);
18
  display: flex;
19
  justify-content: center;
20
  align-items: center;
21
  z-index: 300;
22
`;
23
 
24
const MyConnections = (props) => {
25
  // states
26
  const [myConnections, setMyConnections] = useState([]);
27
  const [loading, setLoading] = useState(true);
28
 
29
  // redux destructuring
30
  const { addNotification } = props;
31
 
32
  // React hook form
33
  const { register, getValues } = useForm();
34
 
35
  let axiosThrottle = null;
36
 
37
  useEffect(() => {
38
    fetchMyConnections();
39
    return () => {
40
      clearTimeout(axiosThrottle);
41
    };
42
  }, []);
43
 
44
  const handleBlock = (link) => {
45
    handleAction(link);
46
  };
47
 
48
  const handleCancel = (link) => {
49
    handleAction(link);
50
  };
51
 
52
  const handleAction = (link) => {
53
    setLoading(true);
54
    axios
55
      .post(link)
56
      .then((response) => {
57
        const resData = response.data;
58
         (resData);
59
        if (resData.success) {
60
          const msg = resData.data;
61
          addNotification({
62
            style: "success",
63
            msg: msg,
64
          });
65
          fetchMyConnections();
66
        } else {
67
          setLoading(false);
68
        }
69
      })
70
      .catch((error) => {
71
        setLoading(false);
72
      });
73
  };
74
 
75
  const fetchMyConnections = async (searchParam='') => {
76
    setLoading(true);
77
    await axios
78
      .get(
79
        "/connection/my-connections?search="+searchParam)
80
      .then((response) => {
81
        const resData = response.data;
82
        if (resData.success) {
83
          setMyConnections(resData.data);
84
        }
85
      });
86
    setLoading(false);
87
  };
88
 
89
  const handleSearch = () => {
90
    //  (getValues());
91
    clearTimeout(axiosThrottle);
92
    // setLoading(true);
93
    const searchValue = getValues("search");
94
    axiosThrottle = setTimeout(() => {
95
      fetchMyConnections(searchValue);
96
    }, 500);
97
  };
98
 
99
  return (
100
    <section className="companies-info">
101
      <div className="container">
102
        <div className="company-title">
103
          <div className="section_admin_title_buttons">
104
            <div style={{ float: "left" }}>
105
              <h1 className="title">Personas con relación directa, de 1er nivel</h1>
106
            </div>
107
          </div>
108
        </div>
109
 
110
        <div className="company-title">
111
          <div className="section_admin_title_buttons">
112
            <form
113
              name="form-connection-search"
114
              id="form-connection-search"
115
              onSubmit={(event) => event.preventDefault()}
116
            >
117
              <div className="form-group">
118
                <input
119
                  type="text"
120
                  name="search"
121
                  id="search"
122
                  className="form-control"
123
                  placeholder="Buscar"
124
                  ref={register}
125
                  onChange={handleSearch}
126
                />
127
              </div>
128
            </form>
129
          </div>
130
        </div>
131
 
132
        <div className="companies-list">
133
          <div
134
            className="row"
135
            id="profiles-container"
136
            style={{
137
              position: "relative",
138
              padding: "0 15px",
139
            }}
140
          >
141
            {myConnections.length > 0 ? (
142
              myConnections.map(
143
                (
144
                  {
145
                    image,
146
                    name,
147
                    link_view,
148
                    link_inmail,
149
                    link_cancel,
150
                    link_block,
151
                  },
152
                  id
153
                ) => (
154
                  <Entity
155
                    image={image}
156
                    name={name}
157
                    link_view={link_view}
158
                    link_inmail={link_inmail}
159
                    link_cancel={link_cancel}
160
                    link_block={link_block}
161
                    key={id}
162
                    handleCancel={handleCancel}
163
                    handleBlock={handleBlock}
164
                  />
165
                )
166
              )
167
            ) : (
168
              <p>No hay resultados</p>
169
            )}
170
            {loading && (
171
              <StyledSpinnerContainer>
172
                <Spinner />
173
              </StyledSpinnerContainer>
174
            )}
175
          </div>
176
          {/* <!--product-feed-tab end--> */}
177
        </div>
178
      </div>
179
    </section>
180
  );
181
};
182
 
183
// const mapStateToProps = (state) => ({});
184
 
185
const mapDispatchToProps = {
186
  addNotification: (notification) => addNotification(notification),
187
};
188
 
189
export default connect(null, mapDispatchToProps)(MyConnections);