import React,{useState, createContext} from 'react';
export const ProductsContext = createContext();
export const ProductsProvider = props => {
const [categories, setCategories] = useState({ categories: {} });
const [products, setProducts] = useState({ products: {} });
const [loading, setLoading] = useState(true);
useEffect(() => {
(async () => {
const [categoriesResult, productsResult] = await Promise.all([fetch('http://localhost:3000/api/categories'), fetch('http://localhost:3000/api/products')]);
setCategories(categoriesResult);
setProducts(productsResult);
setLoading(false);
})();
}, []);
return (
<ProductsContext.Provider value={ { products, categories, loading } }>
{props.children}
</ProductsContext.Provider>
);
}