Skip to content

Commit

Permalink
Se actualiza provider de hisotira en fetch de productos
Browse files Browse the repository at this point in the history
  • Loading branch information
hendaniel committed Sep 7, 2020
1 parent 19fb60c commit 2fb4e87
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
9 changes: 5 additions & 4 deletions src/components/Products/Product.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { useContext, useEffect } from "react";
import { UserContext } from "../../providers/index";
import { UserContext, HistoryContext } from "../../providers/index";
import { coin, buy } from "../../assets/index";
import { SuccessModal, FailureModal, CoinsModal } from "../Modals/index";
import { useModal } from "../../hooks/index";
import { redeemProduct } from "../../services/productsService";

import "./products.scss";

const Product = ({
Expand All @@ -15,9 +14,10 @@ const Product = ({
cost,
img: { url },
},
onFetch,
setProductResponse,
}) => {
const { user, setUserResponse } = useContext(UserContext);
const { setHistoryResponse } = useContext(HistoryContext);

useEffect(() => {
if (!user) return;
Expand All @@ -38,7 +38,8 @@ const Product = ({
.then((res) => {
toggleSuccessModal();
setUserResponse(res);
onFetch(res);
setHistoryResponse(res);
setProductResponse(res);
})
.catch((err) => {
toggleErrorModal();
Expand Down
6 changes: 5 additions & 1 deletion src/components/Products/ProductsList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ const ProductsList = () => {
<div className="cards">
{currentData().map((item, key) => {
return (
<Product item={item} key={key} onFetch={setProductResponse} />
<Product
item={item}
key={key}
setProductResponse={setProductResponse}
/>
);
})}
</div>
Expand Down
12 changes: 6 additions & 6 deletions src/components/ProductsHistory/ProductsHistory.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import "./history.scss";
const ITEMS_PER_PAGE = 16;

const ProductsHistory = () => {
const { products } = useContext(HistoryContext);
const { history } = useContext(HistoryContext);

const [filteredProducts, setProducts] = useState([]);
const [filteredProducts, setHistory] = useState([]);

useEffect(() => {
if (!products) return;
setProducts(products);
}, [products]);
if (!history) return;
setHistory(history);
}, [history]);

const { next, prev, currentData, currentAmount, jump } = usePagination(
filteredProducts,
ITEMS_PER_PAGE
);

const handleFilter = (event) => {
setProducts(products.filter(priceFilter(event.target.value)));
setHistory(history.filter(priceFilter(event.target.value)));
jump(1);
};

Expand Down
11 changes: 5 additions & 6 deletions src/providers/HistoryProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import { getHistory } from "../services/userService";
export const HistoryContext = React.createContext();

const HistoryProvider = ({ children }) => {
const [products, setProducts] = useState({});
const [history, setHistory] = useState({});

const [productResponse, setProductResponse] = useState([]);
const [historyResponse, setHistoryResponse] = useState([]);

useEffect(() => {
getHistory().then((products) => {
setProducts(products);
setHistory(products);
});
}, [productResponse]);

}, [historyResponse]);
return (
<HistoryContext.Provider value={{ products, setProductResponse }}>
<HistoryContext.Provider value={{ history, setHistoryResponse }}>
{children}
</HistoryContext.Provider>
);
Expand Down

0 comments on commit 2fb4e87

Please sign in to comment.