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
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ In this project you'll take take an almost completed e-commerce store and refact
- [ ] CD into the project base directory `cd react-shopping-cart`.
- [ ] Download project dependencies by running `npm install`.
- [ ] Start up the app using `npm start`.
- [ ] Create a new branch: git checkout -b `<firstName-lastName>`.
- [ ] Implement the project on your newly created `<firstName-lastName>` branch, committing changes regularly.
- [ ] Push commits: git push origin `<firstName-lastName>`.

### Task 2: MVP

Expand Down Expand Up @@ -161,8 +158,3 @@ Do not attempt stretch problems until MVP has been reached and a final commit ha

- Create a `removeItem` function that allows you to remove an item from your cart with a click of a button. This `removeItem` function should be able to be consumed from your `ShoppingCartItem` component.
Remember each item has an `id` this will help out a lot while creating your removeItem function!

- Persist Cart Items using `localStorage`. (If you try this one, it will be a bit tricky to get our items to populate the shopping cart on a refresh. You'll have to think about where the data actually lives, and how you can get data there from localStorage when the app is being mounted after a refresh. Good luck!)

## Submission Format
* [ ] Submit a Pull-Request to merge `<firstName-lastName>` Branch into `main` (student's Repo). **Please don't merge your own pull request**
26 changes: 18 additions & 8 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { Profiler, useState } from 'react';
import { Route } from 'react-router-dom';
import data from './data';

Expand All @@ -7,26 +7,36 @@ import Navigation from './components/Navigation';
import Products from './components/Products';
import ShoppingCart from './components/ShoppingCart';

// Contexts
import { ProductContext } from './contexts/ProductContext';
import { CartContext } from './contexts/CartContext';

function App() {
const [products] = useState(data);
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 cart={cart} />

{/* Routes */}
<Route exact path="/">
<Products products={products} addItem={addItem} />
</Route>
<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
7 changes: 5 additions & 2 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 { 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 '../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 +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
10 changes: 6 additions & 4 deletions src/components/Products.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import React from 'react';
import React, { useContext } from 'react';
import { ProductContext } from '../contexts/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>
);
};

export default Products;
export default Products;
10 changes: 6 additions & 4 deletions src/components/ShoppingCart.js
Original file line number Diff line number Diff line change
@@ -1,18 +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 => (
{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();