Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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