Rev 1590 | 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 styled from "styled-components";
import {axios} from "../../../utils";
import Company from "./company/Company";
import Spinner from "../../../shared/loading-spinner/Spinner";
import { connect } from "react-redux";
import { addNotification } from "../../../redux/notification/notification.actions";
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 FollowingCompanies = (props) => {
// states
const [companies, setCompanies] = useState([]);
const [loading, setLoading] = useState(true);
// redux destructuring
const { addNotification } = props;
// 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/following-companies?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 handleUnfollow = async (link_unfollow, indexToFilter) => {
setLoading(true);
await axios.post(link_unfollow).then((response) => {
const resData = response.data;
if (resData.success) {
const newCompanies = companies.filter(
(_company, index) => index !== indexToFilter
);
setCompanies(newCompanies);
} else {
if (resError.constructor.name !== "Object") {
addNotification({
style: "danger",
msg: resData.data,
});
}
}
});
setLoading(false);
};
return (
<React.Fragment>
<section className="companies-info">
<div className="container">
<div className="company-title">
<div className="section_admin_title_buttons">
<div style={{ float: "left" }}>
<h1 className="title">Empresas que sigo</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",
}}
>
{companies.length ? (
companies.map(
({ image, name, link_view, link_unfollow }, index) => (
<Company
image={image}
name={name}
link_view={link_view}
link_unfollow={link_unfollow}
onUnfollow={handleUnfollow}
index={index}
key={index}
/>
)
)
) : (
<div style={{ margin: "auto", textAlign: "center" }}>
Ningún registro coincidio con su consulta
</div>
)}
{loading && (
<StyledSpinnerContainer>
<Spinner />
</StyledSpinnerContainer>
)}
</div>
{/* <!--product-feed-tab end--> */}
</div>
</div>
</section>
</React.Fragment>
);
};
// const mapStateToProps = (state) => ({
// })
const mapDispatchToProps = {
addNotification: (notification) => addNotification(notification),
};
export default connect(null, mapDispatchToProps)(FollowingCompanies);