Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1592 | Rev 1595 | 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 { useForm } from "react-hook-form";
import {axios} from "../../../utils";
import Spinner from "../../../shared/loading-spinner/Spinner";
import CompanyTemplate from "./companyTemplate/CompanyTemplate";
import { connect } from "react-redux";
import { addNotification } from "../../../redux/notification/notification.actions";
import SearchList from "../../../components/SearchList";
import Profile from "../../../components/Profile";

const IWorkWith = (props) => {
  // redux destructuring
  const { addNotification } = props;

  // states
  const [companies, setCompanies] = useState([]);
  const [loading, setLoading] = useState(true);

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

  let axiosThrottle = null;

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

  const fetchCompanies = async (searchParam='') => {
    setLoading(true);
    await axios
      .get(
        "/company/i-work-with?search="+searchParam)
      .then((response) => {
        const resData = response.data;
        if (resData.success) {
          setCompanies(resData.data);
        }
      });
    setLoading(false);
  };

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

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

  return (
    <section className="companies-info">
      <div className="container">
        <SearchList
          title="Empresas donde trabajo"
          fetchCallback={fetchCompanies}
        />
        <div className="companies-list">
          <div
            className="row"
            id="profiles-container"
            style={{
              position: "relative",
            }}
          >
            {companies.length > 0 ? (
              companies.map(({image, link_leave, link_view, name}, id) => (
                <Profile
                  key={id}
                  image={image}
                  link_leave={link_leave}
                  link_view={link_view}
                  name={name}
                />
              ))
            ) : (
              <div style={{ margin: "auto", textAlign: "center" }}>
                Ningún registro coincidio con su consulta
              </div>
            )}
            {loading && (
              <div className="spinner-container">
                <Spinner />
              </div>
            )}
          </div>
          {/* <!--product-feed-tab end--> */}
        </div>
      </div>
    </section>
  );
};

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

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

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