Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -12,21 +16,26 @@ function App() {
const [cart, setCart] = useState([]);

const addItem = item => {
// add the given item to the cart
setCart([...cart, item]);

};

return (
<div className="App">
<Navigation cart={cart} />
<CartContext.Provider value={cart}>
<Navigation />

{/* Routes */}
<Route exact path="/">
<Products products={products} addItem={addItem} />
</Route>
{/* Routes */}
<ProductContext.Provider value={{ products, addItem }}>
<Route exact path="/">
<Products />
</Route>
</ProductContext.Provider>

<Route path="/cart">
<ShoppingCart cart={cart} />
</Route>
<Route path="/cart">
<ShoppingCart />
</Route>
</CartContext.Provider>
</div>
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/components/Context/CartContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createContext } from "react";

const CartContext = createContext();

export default CartContext;
5 changes: 5 additions & 0 deletions src/components/Context/ProductContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createContext } from "react";

const ProductContext = createContext();

export default ProductContext;
9 changes: 6 additions & 3 deletions src/components/Navigation.js
Original file line number Diff line number Diff line change
@@ -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 (
<div className="navigation">
<NavLink to="/">Products</NavLink>
<NavLink to="/cart">
Cart <span>{props.cart.length}</span>
Cart <span>{cart.length}</span>
</NavLink>
</div>
);
Expand Down
7 changes: 5 additions & 2 deletions src/components/Product.js
Original file line number Diff line number Diff line change
@@ -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 (
<div className="product">
<img src={props.product.image} alt={`${props.product.title} book`} />
Expand All @@ -9,7 +12,7 @@ const Product = props => {

<p className="price">${props.product.price}</p>

<button onClick={() => props.addItem(props.product)}>
<button onClick={() => addItem(props.product)}>
Add to cart
</button>
</div>
Expand Down
8 changes: 5 additions & 3 deletions src/components/Products.js
Original file line number Diff line number Diff line change
@@ -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 (
<div className="products-container">
{props.products.map(product => (
{products.map(product => (
<Product
key={product.id}
product={product}
addItem={props.addItem}
/>
))}
</div>
Expand Down
9 changes: 6 additions & 3 deletions src/components/ShoppingCart.js
Original file line number Diff line number Diff line change
@@ -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 (
<div className="shopping-cart">
{props.cart.map(item => (
{cart.map(item => (
<Item key={item.id} {...item} />
))}

Expand Down