diff --git a/README.md b/README.md index 24835949..115a4a0a 100644 --- a/README.md +++ b/README.md @@ -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 ``. -- [ ] Implement the project on your newly created `` branch, committing changes regularly. -- [ ] Push commits: git push origin ``. ### Task 2: MVP @@ -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 `` Branch into `main` (student's Repo). **Please don't merge your own pull request** diff --git a/src/App.js b/src/App.js index e134ca03..8567331f 100644 --- a/src/App.js +++ b/src/App.js @@ -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'; @@ -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 (
- + + {/* Routes */} - - - + + + + + - - - + + + + +
); } diff --git a/src/components/Navigation.js b/src/components/Navigation.js index b2ef658a..5b6b9e75 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 '../contexts/CartContext'; const Navigation = props => { + 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..9adbaee9 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 '../contexts/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..51fc4f37 100644 --- a/src/components/Products.js +++ b/src/components/Products.js @@ -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 (
- {props.products.map(product => ( + {products.map(product => ( ))}
); }; -export default Products; +export default Products; \ No newline at end of file diff --git a/src/components/ShoppingCart.js b/src/components/ShoppingCart.js index a2d21939..0949b43f 100644 --- a/src/components/ShoppingCart.js +++ b/src/components/ShoppingCart.js @@ -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 (
- {props.cart.map(item => ( + {cart.map(item => ( ))} diff --git a/src/contexts/CartContext.js b/src/contexts/CartContext.js new file mode 100644 index 00000000..22d69585 --- /dev/null +++ b/src/contexts/CartContext.js @@ -0,0 +1,3 @@ +import { createContext } from "react"; + +export const CartContext = createContext(); diff --git a/src/contexts/ProductContext.js b/src/contexts/ProductContext.js new file mode 100644 index 00000000..38ef1154 --- /dev/null +++ b/src/contexts/ProductContext.js @@ -0,0 +1,3 @@ +import { createContext } from 'react'; + +export const ProductContext = createContext(); \ No newline at end of file