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
269 changes: 147 additions & 122 deletions package-lock.json

Large diffs are not rendered by default.

27 changes: 17 additions & 10 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import React, { useState } from 'react';
import { Route } from 'react-router-dom';
import data from './data';

import ProductContext from './contexts/ProductContext';
import CartContext from './contexts/CartContext';

// Components
import Navigation from './components/Navigation';
import Products from './components/Products';
Expand All @@ -12,21 +15,25 @@ 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} />
<div className="App" >
<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
8 changes: 5 additions & 3 deletions src/components/Navigation.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React from 'react';
import React, { useContext } from 'react';
import { NavLink } from 'react-router-dom';
import CartContext from '../contexts/CartContext';

const Navigation = props => {
const Navigation = (props) => {
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
6 changes: 4 additions & 2 deletions src/components/Product.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react';
import React, {useContext} from 'react';
import ProductContext from '../contexts/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 +11,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
9 changes: 6 additions & 3 deletions src/components/Products.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import React from 'react';
import ProductContext from '../contexts/ProductContext';
import { useContext } from 'react';

// Components
import Product from './Product';

const Products = props => {
const Products = () => {
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
13 changes: 7 additions & 6 deletions src/components/ShoppingCart.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import React from 'react';

import React, { useContext} from 'react';
import CartContext from '../contexts/CartContext';
// Components
import Item from './ShoppingCartItem';

const ShoppingCart = props => {
const ShoppingCart = () => {
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 => (
<Item key={item.id} {...item} />
{cart.map((item, index) => (
<Item key={index} {...item}/>
))}

<div className="shopping-cart__checkout">
Expand Down
4 changes: 3 additions & 1 deletion src/components/ShoppingCartItem.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import { useState } from 'react';

const Item = props => {
return (

return (
<div className="shopping-cart_item">
<img src={props.image} alt={`${props.title} book`} />

Expand Down
4 changes: 4 additions & 0 deletions src/contexts/CartContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createContext } from "react";
const CartContext = createContext();

export default CartContext;
5 changes: 5 additions & 0 deletions src/contexts/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;