Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1158 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import React from "react";
import { useEffect, useState } from "react";
import { connect } from "react-redux";
import { useForm } from "react-hook-form";
import {axios} from "../../../utils";
import { addNotification } from "../../../redux/notification/notification.actions";
import Spinner from "../../../shared/loading-spinner/Spinner";
import RequestTemplate from "./requestTemplate/RequestTemplate";

const RequestsSent = (props) => {
  // states
  const [sentRequests, setSentRequests] = useState([]);
  const [loading, setLoading] = useState(true);

  // redux destructuring
  const { addNotification } = props;

  // React hook form
  const { register, getValues } = useForm();

  let axiosThrottle = null;

  useEffect(() => {
    fetchSentRequests();
    return () => {
      clearTimeout(axiosThrottle);
    };
  }, []);

  const fetchSentRequests = async (searchParam='') => {
    setLoading(true);
    await axios
      .get(
        "/group/requests-sent?search="+searchParam)
      .then((response) => {
        const resData = response.data;
        if (resData.success) {
          setSentRequests(resData.data);
        }
      });
    setLoading(false);
  };

  const handleCancel = (link) => {
    setLoading(true);
    axios.post(link).then((response) => {
      const resData = response.data;
      if (resData.success) {
        addNotification({
          style: "success",
          msg: resData.data,
        });
        fetchSentRequests();
      } else {
        setLoading(false);
        addNotification({
          style: "danger",
          msg: resData.data ?? "ha ocurrido un error",
        });
      }
    });
  };

  const handleSearch = () => {
    //  (getValues());
    clearTimeout(axiosThrottle);
    // setLoading(true);
    const searchValue = getValues("search");
    axiosThrottle = setTimeout(() => {
      fetchSentRequests(searchValue);
    }, 500);
  };

  return (
    <section className="companies-info" style={{ position: "relative" }}>
      <div className="container">
        <div className="company-title">
          <div className="section_admin_title_buttons">
            <div style={{ float: "left" }}>
              <h1 className="title">Solicitudes Enviadas</h1>
            </div>
          </div>
        </div>

        <div className="company-title">
          <div className="section_admin_title_buttons">
            <div className="form-group">
              <input
                type="text"
                name="search"
                id="search"
                className="form-control"
                placeholder="Buscar"
                ref={register}
                onChange={handleSearch}
              />
            </div>
          </div>
        </div>
        <div
          className="companies-list"
          style={{
            padding: "0 15px",
          }}
        >
          <div
            className="row"
            id="profiles-container"
          >
            {
            !!sentRequests.length
            
            ?
              sentRequests.map((request, index) => {
                  return(
                    <RequestTemplate
                      key={index}
                      request={request}
                      onCancel={handleCancel}
                    />
                  )
                }
              )
            :
              <p>No hay resultados</p>
            }
          </div>
          {/* <!--product-feed-tab end--> */}
        </div>
      </div>
      {loading && (
        <div className="spinner-container">
          <Spinner />
        </div>
      )}
    </section>
  );
};

// const mapStateToProps = (state) => ({});

const mapDispatchToProps = {
  addNotification: (notification) => addNotification(notification),
};

export default connect(null, mapDispatchToProps)(RequestsSent);