Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1 | Rev 1590 | Ir a la última revisión | | Comparar con el anterior | 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 { useForm } from "react-hook-form";
4
import styled from "styled-components";
1158 stevensc 5
import { axios } from "../../../utils";
1 www 6
import Company from "./company/Company";
7
import Spinner from "../../../shared/loading-spinner/Spinner";
8
import { connect } from "react-redux";
9
import { addNotification } from "../../../redux/notification/notification.actions";
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 FollowingCompanies = (props) => {
25
  // states
26
  const [companies, setCompanies] = 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
    fetchCompanies();
39
    return () => {
40
      clearTimeout(axiosThrottle);
41
    };
42
  }, []);
43
 
1158 stevensc 44
  const fetchCompanies = async (searchParam = '') => {
1 www 45
    setLoading(true);
46
    await axios
47
      .get(
1158 stevensc 48
        "/company/following-companies?search=" + searchParam)
1 www 49
      .then((response) => {
50
        const resData = response.data;
51
        if (resData.success) {
52
          setCompanies(resData.data);
53
        }
54
      });
55
    setLoading(false);
56
  };
57
 
58
  const handleSearch = () => {
59
    //  (getValues());
60
    clearTimeout(axiosThrottle);
61
    // setLoading(true);
62
    const searchValue = getValues("search");
63
    axiosThrottle = setTimeout(() => {
64
      fetchCompanies(searchValue);
65
    }, 500);
66
  };
67
 
68
  const handleUnfollow = async (link_unfollow, indexToFilter) => {
69
    setLoading(true);
70
    await axios.post(link_unfollow).then((response) => {
71
      const resData = response.data;
72
      if (resData.success) {
73
        const newCompanies = companies.filter(
74
          (_company, index) => index !== indexToFilter
75
        );
76
        setCompanies(newCompanies);
77
      } else {
78
        if (resError.constructor.name !== "Object") {
79
          addNotification({
80
            style: "danger",
81
            msg: resData.data,
82
          });
83
        }
84
      }
85
    });
86
    setLoading(false);
87
  };
88
 
89
  return (
90
    <React.Fragment>
91
      <section className="companies-info">
92
        <div className="container">
93
          <div className="company-title">
94
            <div className="section_admin_title_buttons">
95
              <div style={{ float: "left" }}>
96
                <h1 className="title">Empresas que sigo</h1>
97
              </div>
98
            </div>
99
          </div>
100
          <div className="company-title">
101
            <div className="section_admin_title_buttons">
102
              <form
103
                name="form-connection-search"
104
                id="form-connection-search"
105
                onSubmit={(event) => event.preventDefault()}
106
              >
107
                <div className="form-group">
108
                  <input
109
                    type="text"
110
                    name="search"
111
                    id="search"
112
                    className="form-control"
113
                    placeholder="Buscar"
114
                    ref={register}
115
                    onChange={handleSearch}
116
                  />
117
                </div>
118
              </form>
119
            </div>
120
          </div>
121
 
122
          <div className="companies-list">
123
            <div
124
              className="row"
125
              id="profiles-container"
126
              style={{
127
                position: "relative",
128
              }}
129
            >
1158 stevensc 130
              {
131
                companies.length
132
                  ?
133
                  companies.map(({ image, name, link_view, link_unfollow }, index) => (
1 www 134
                    <Company
135
                      image={image}
136
                      name={name}
137
                      link_view={link_view}
138
                      link_unfollow={link_unfollow}
139
                      onUnfollow={handleUnfollow}
140
                      index={index}
141
                      key={index}
142
                    />
1158 stevensc 143
                  ))
144
                  :
145
                  <div style={{ margin: "auto", textAlign: "center" }}>
146
                    Ningún registro coincidio con su consulta
147
                  </div>
148
              }
149
              {
150
                loading
151
                &&
1 www 152
                <StyledSpinnerContainer>
153
                  <Spinner />
154
                </StyledSpinnerContainer>
1158 stevensc 155
              }
1 www 156
            </div>
157
            {/* <!--product-feed-tab end--> */}
158
          </div>
159
        </div>
160
      </section>
161
    </React.Fragment>
162
  );
163
};
164
 
165
// const mapStateToProps = (state) => ({
166
 
167
// })
168
 
169
const mapDispatchToProps = {
170
  addNotification: (notification) => addNotification(notification),
171
};
172
 
173
export default connect(null, mapDispatchToProps)(FollowingCompanies);