diff --git a/src/App.js b/src/App.js index e134ca03..ed684b59 100644 --- a/src/App.js +++ b/src/App.js @@ -2,6 +2,10 @@ import React, { useState } from 'react'; import { Route } from 'react-router-dom'; import data from './data'; + +import ProductContext from './components/Context/ProductContext'; +import CartContext from './components/Context/CartContext'; + // Components import Navigation from './components/Navigation'; import Products from './components/Products'; @@ -12,21 +16,26 @@ function App() { const [cart, setCart] = useState([]); const addItem = item => { - // add the given item to the cart + setCart([...cart, item]); + }; return (
- + + - {/* Routes */} - - - + {/* Routes */} + + + + + - - - + + + +
); } diff --git a/src/components/Context/CartContext.js b/src/components/Context/CartContext.js new file mode 100644 index 00000000..c1bfac69 --- /dev/null +++ b/src/components/Context/CartContext.js @@ -0,0 +1,5 @@ +import { createContext } from "react"; + +const CartContext = createContext(); + +export default CartContext; \ No newline at end of file diff --git a/src/components/Context/ProductContext.js b/src/components/Context/ProductContext.js new file mode 100644 index 00000000..611ccbdb --- /dev/null +++ b/src/components/Context/ProductContext.js @@ -0,0 +1,5 @@ +import { createContext } from "react"; + +const ProductContext = createContext(); + +export default ProductContext; \ No newline at end of file diff --git a/src/components/Navigation.js b/src/components/Navigation.js index b2ef658a..ea015e8a 100644 --- a/src/components/Navigation.js +++ b/src/components/Navigation.js @@ -1,12 +1,15 @@ -import React from 'react'; +import React, {useContext } from 'react'; import { NavLink } from 'react-router-dom'; +import CartContext from './Context/CartContext'; + const Navigation = props => { - return ( + const cart = useContext(CartContext); + return (
Products - Cart {props.cart.length} + Cart {cart.length}
); diff --git a/src/components/Product.js b/src/components/Product.js index 8c9b860b..f3698026 100644 --- a/src/components/Product.js +++ b/src/components/Product.js @@ -1,6 +1,9 @@ -import React from 'react'; +import React, { useContext } from 'react'; + +import ProductContext from './Context/ProductContext'; const Product = props => { + const {addItem} = useContext(ProductContext); return (
{`${props.product.title} @@ -9,7 +12,7 @@ const Product = props => {

${props.product.price}

-
diff --git a/src/components/Products.js b/src/components/Products.js index 3f56b80e..964b79c1 100644 --- a/src/components/Products.js +++ b/src/components/Products.js @@ -1,16 +1,18 @@ -import React from 'react'; +import React, { useContext } from 'react'; + +import ProductContext from './Context/ProductContext'; // Components import Product from './Product'; const Products = props => { + const { products } = useContext(ProductContext); return (
- {props.products.map(product => ( + {products.map(product => ( ))}
diff --git a/src/components/ShoppingCart.js b/src/components/ShoppingCart.js index a2d21939..492eae24 100644 --- a/src/components/ShoppingCart.js +++ b/src/components/ShoppingCart.js @@ -1,18 +1,21 @@ -import React from 'react'; +import React, {useContext } from 'react'; + +import CartContext from './Context/CartContext'; // Components import Item from './ShoppingCartItem'; const ShoppingCart = props => { + const cart = useContext(CartContext); const getCartTotal = () => { - return props.cart.reduce((acc, value) => { + return cart.reduce((acc, value) => { return acc + value.price; }, 0).toFixed(2); }; return (
- {props.cart.map(item => ( + {cart.map(item => ( ))}