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
18,089 changes: 18,041 additions & 48 deletions package-lock.json

Large diffs are not rendered by default.

23 changes: 15 additions & 8 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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';
Expand All @@ -13,20 +15,25 @@ function App() {

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>

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

<Route path="/cart">
<ShoppingCart />
</Route>
</CartContext.Provider>
</div>
);
}
Expand Down
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 '../contexts/CartContext';

const Navigation = props => {

const Navigation = () => {
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
5 changes: 4 additions & 1 deletion src/components/Product.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React from 'react';
import React from 'react';


const Product = props => {


return (
<div className="product">
<img src={props.product.image} alt={`${props.product.title} book`} />
Expand Down
12 changes: 7 additions & 5 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 '../contexts/ProductContext';
// Components
import Product from './Product';

const Products = props => {
const Products = () => {
const {products, addItem} = useContext(ProductContext)

return (
<div className="products-container">
{props.products.map(product => (
{products.map(product => (
<Product
key={product.id}
product={product}
addItem={props.addItem}
addItem={addItem}
/>
))}
</div>
Expand Down
13 changes: 8 additions & 5 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 '../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 => (
{cart.map(item => (
<Item key={item.id} {...item} />
))}

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

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

export const ProductContext = createContext();