Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1156 | 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 { useForm } from "react-hook-form";
4
import {axios} from "../../../utils";
5
import Spinner from "../../../shared/loading-spinner/Spinner";
6
import { connect } from "react-redux";
7
import { addNotification } from "../../../redux/notification/notification.actions";
8
import CompanyTemplate from "./companyTemplate/CompanyTemplate";
9
 
10
const RequestSent = (props) => {
11
  // redux destructuring
12
  const { addNotification } = props;
13
 
14
  // states
15
  const [companies, setCompanies] = useState([]);
16
  const [loading, setLoading] = useState(true);
17
 
18
  // React hook form
19
  const { register, getValues } = useForm();
20
 
21
  let axiosThrottle = null;
22
 
23
  useEffect(() => {
24
    fetchCompanies();
25
    return () => {
26
      clearTimeout(axiosThrottle);
27
    };
28
  }, []);
29
 
30
  const fetchCompanies = async (searchParam='') => {
31
    setLoading(true);
32
    await axios
33
      .get(
34
        "/company/requests-sent?search"+searchParam)
35
      .then((response) => {
36
        const resData = response.data;
37
         (resData);
38
        if (resData.success) {
39
          setCompanies(resData.data);
40
        }
41
      });
42
    setLoading(false);
43
  };
44
 
45
  const handleSearch = () => {
46
    //  (getValues());
47
    clearTimeout(axiosThrottle);
48
    // setLoading(true);
49
    const searchValue = getValues("search");
50
    axiosThrottle = setTimeout(() => {
51
      fetchCompanies(searchValue);
52
    }, 500);
53
  };
54
 
55
  const handleCancelRequest = (link) => {
56
    setLoading(true);
57
    axios.post(link).then((response) => {
58
      const resData = response.data;
59
      if (resData.success) {
60
        addNotification({
61
          style: "success",
62
          msg: resData.data,
63
        });
64
        fetchCompanies();
65
      } else {
66
        setLoading(false);
67
        addNotification({
68
          style: "danger",
69
          msg: resData.data ?? "ha ocurrido un error",
70
        });
71
      }
72
    });
73
  };
74
 
75
  return (
76
    <section className="companies-info">
77
      <div className="container">
78
        <div className="company-title">
79
          <div className="section_admin_title_buttons">
80
            <div>
81
              <h1 className="title">Solicitudes Enviadas</h1>
82
            </div>
83
          </div>
84
        </div>
85
        <div className="company-title">
86
          <div className="section_admin_title_buttons">
87
            <form
88
              name="form-connection-search"
89
              id="form-connection-search"
90
              onSubmit={(event) => event.preventDefault()}
91
            >
92
              <div className="form-group">
93
                <input
94
                  type="text"
95
                  name="search"
96
                  id="search"
97
                  className="form-control"
98
                  placeholder="Buscar"
99
                  ref={register}
100
                  onChange={handleSearch}
101
                />
102
              </div>
103
            </form>
104
          </div>
105
        </div>
106
        <div className="companies-list">
107
          <div
108
            className="row"
109
            id="profiles-container"
110
            style={{
111
              position: "relative",
112
            }}
113
          >
114
            {companies.length > 0 ? (
115
              companies.map((company, id) => (
116
                <CompanyTemplate
117
                  company={company}
118
                  key={id}
119
                  onCancel={handleCancelRequest}
120
                />
121
              ))
122
            ) : (
123
              <div style={{ margin: "auto", textAlign: "center" }}>
124
                Ningún registro coincidio con su consulta
125
              </div>
126
            )}
127
            {loading && (
128
              <div className="spinner-container">
129
                <Spinner />
130
              </div>
131
            )}
132
          </div>
133
          {/* <!--product-feed-tab end--> */}
134
        </div>
135
      </div>
136
    </section>
137
  );
138
};
139
 
140
// const mapStateToProps = (state) => ({});
141
 
142
const mapDispatchToProps = {
143
  addNotification: (notification) => addNotification(notification),
144
};
145
 
146
export default connect(null, mapDispatchToProps)(RequestSent);