Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1569 | 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 styled from "styled-components";
import {axios} from "../../../utils";
import { addNotification } from "../../../redux/notification/notification.actions";
import Spinner from "../../../shared/loading-spinner/Spinner";
import Entity from "./entity/Entity";

const StyledSpinnerContainer = styled.div`
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.4);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 300;
`;

const MyConnections = (props) => {
  // states
  const [myConnections, setMyConnections] = useState([]);
  const [loading, setLoading] = useState(true);

  // redux destructuring
  const { addNotification } = props;

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

  let axiosThrottle = null;

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

  const handleBlock = (link) => {
    handleAction(link);
  };

  const handleCancel = (link) => {
    handleAction(link);
  };

  const handleAction = (link) => {
    setLoading(true);
    axios
      .post(link)
      .then((response) => {
        const resData = response.data;
         (resData);
        if (resData.success) {
          const msg = resData.data;
          addNotification({
            style: "success",
            msg: msg,
          });
          fetchMyConnections();
        } else {
          setLoading(false);
        }
      })
      .catch((error) => {
        setLoading(false);
      });
  };

  const fetchMyConnections = async (searchParam='') => {
    setLoading(true);
    await axios
      .get(
        "/connection/my-connections?search="+searchParam)
      .then((response) => {
        const resData = response.data;
        if (resData.success) {
          setMyConnections(resData.data);
        }
      });
    setLoading(false);
  };

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

  return (
    <section className="companies-info">
      <div className="container">
        <div className="company-title">
          <div className="section_admin_title_buttons">
            <div style={{ float: "left" }}>
              <h1 className="title">Personas con relación directa, de 1er nivel</h1>
            </div>
          </div>
        </div>

        <div className="company-title">
          <div className="section_admin_title_buttons">
            <form
              name="form-connection-search"
              id="form-connection-search"
              onSubmit={(event) => event.preventDefault()}
            >
              <div className="form-group">
                <input
                  type="text"
                  name="search"
                  id="search"
                  className="form-control"
                  placeholder="Buscar"
                  ref={register}
                  onChange={handleSearch}
                />
              </div>
            </form>
          </div>
        </div>

        <div className="companies-list">
          <div
            className="row"
            id="profiles-container"
            style={{
              position: "relative",
              padding: "0 15px",
            }}
          >
            {myConnections.length > 0 ? (
              myConnections.map(
                (
                  {
                    image,
                    name,
                    link_view,
                    link_inmail,
                    link_cancel,
                    link_block,
                  },
                  id
                ) => (
                  <Entity
                    image={image}
                    name={name}
                    link_view={link_view}
                    link_inmail={link_inmail}
                    link_cancel={link_cancel}
                    link_block={link_block}
                    key={id}
                    handleCancel={handleCancel}
                    handleBlock={handleBlock}
                  />
                )
              )
            ) : (
              <p>No hay resultados</p>
            )}
            {loading && (
              <StyledSpinnerContainer>
                <Spinner />
              </StyledSpinnerContainer>
            )}
          </div>
          {/* <!--product-feed-tab end--> */}
        </div>
      </div>
    </section>
  );
};

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

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

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