Skip to content

Commit fe8e33b

Browse files
committed
showing cart data in modal
1 parent c01e035 commit fe8e33b

5 files changed

Lines changed: 66 additions & 15 deletions

File tree

src/App.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@ import { BrowserRouter, Routes, Route} from 'react-router-dom';
66
import Store from './pages/Store';
77
import Success from './pages/Success';
88
import Cancel from './pages/Cancel';
9+
import CartProvider from './CartContext';
910

1011
function App() {
1112
return (
12-
<Container>
13-
<NavbarComponent></NavbarComponent>
14-
<BrowserRouter>
15-
<Routes>
16-
<Route index element={<Store />} /> {/*here index property represents the home route or '/' */}
17-
<Route path="success" element={<Success />} />
18-
<Route path="cancel" element={<Cancel />} />
19-
</Routes>
20-
</BrowserRouter>
21-
</Container>
13+
<CartProvider>
14+
<Container>
15+
<NavbarComponent></NavbarComponent>
16+
<BrowserRouter>
17+
<Routes>
18+
<Route index element={<Store />} /> {/*here index property represents the home route or '/' */}
19+
<Route path="success" element={<Success />} />
20+
<Route path="cancel" element={<Cancel />} />
21+
</Routes>
22+
</BrowserRouter>
23+
</Container>
24+
</CartProvider>
2225
);
2326
}
2427

src/CartContext.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function CartProvider({children}) {
1818
// { id: 1, quantity: 2 }
1919

2020
function getProductQuantity(id) {
21-
cartProducts.find(product => product.id === id)?.quantity
21+
const quantity = cartProducts.find(product => product.id === id)?.quantity
2222

2323
if(quantity === undefined) {
2424
return 0;

src/components/CartProduct.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Button } from "react-bootstrap";
2+
import { CartContext } from "../CartContext";
3+
import { useContext } from "react";
4+
import { getProductData } from "../productsStore";
5+
6+
function CartProduct(props) {}
7+
8+
9+
export default CartProduct;

src/components/Navbar.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import {Button, Container, Navbar, Modal} from 'react-bootstrap';
2-
import { useState } from 'react';
2+
import { useState, useContext } from 'react';
3+
import { CartContext } from '../CartContext';
34

45
function NavbarComponent() {
6+
const cart = useContext(CartContext);
7+
58
const [ show, setShow ] = useState(false);
69
const handleClose = () => setShow(false);
710
const handleShow = () => setShow(true);
11+
12+
const productCount = cart.items.reduce((sum, product) => sum + product.quantity, 0);
813

914
return (
1015
<>
1116
<Navbar expand="sm"> {/*expand determines where your window collapses */}
1217
<Navbar.Brand href='/'>Ecommerce Store</Navbar.Brand>
1318
<Navbar.Toggle /> {/*This determines the things to collaps on small screen size */}
1419
<Navbar.Collapse className='justify-content-end'>
15-
<Button onClick={handleShow}>Cart 0 Items</Button>
20+
<Button onClick={handleShow}>Cart ({productCount} Items)</Button>
1621
</Navbar.Collapse>
1722
</Navbar>
1823

@@ -21,7 +26,22 @@ function NavbarComponent() {
2126
<Modal.Title>Shopping Cart</Modal.Title>
2227
</Modal.Header>
2328
<Modal.Body>
24-
<h1>This is the Modal Body</h1>
29+
{productCount > 0 ?
30+
<>
31+
<p>Items in your cart: </p>
32+
{cart.items.map((currentProduct, idx) => (
33+
<h1>{currentProduct.id}</h1>
34+
))}
35+
36+
<h1>Total Cost: {cart.getTotalCost().toFixed(2)}</h1>
37+
38+
<Button variant="success">
39+
Purchase items!
40+
</Button>
41+
</>
42+
:
43+
<h1>There are no items in your cart!</h1>
44+
}
2545
</Modal.Body>
2646
</Modal>
2747
</>

src/components/ProductCard.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
import { Card, Button, Form, Row, Col } from 'react-bootstrap';
2+
import { CartContext } from '../CartContext';
3+
import { useContext } from 'react';
24

35
function ProductCard(props) { //props.product is the product we are selling
46
const product = props.product;
7+
const cart = useContext(CartContext);
8+
const productQuantity = cart.getProductQuantity(product.id);
9+
console.log(cart.items);
510

611
return (
712
<Card>
813
<Card.Body>
914
<Card.Title>{product.title}</Card.Title>
1015
<Card.Text>{product.price}</Card.Text>
11-
<Button variant="primary">Add To Cart </Button>
16+
{ productQuantity > 0 ?
17+
<>
18+
<Form as={Row}>
19+
<Form.Label column="true" sm="6">In Cart: {productQuantity}</Form.Label>
20+
<Col sm="6">
21+
<Button sm="6" onClick={() => cart.addOneToCart(product.id)} className='mx-2'>+</Button>
22+
<Button sm="6" onClick={() => cart.removeOneFromCart(product.id)} className='mx-2'>-</Button>
23+
</Col>
24+
</Form>
25+
<Button variant='danger' onClick={() => cart.deleteFromCart(product.id)} className='my-2'>Remove from cart</Button>
26+
</>
27+
:
28+
<Button variant="primary" onClick={() => cart.addOneToCart(product.id)}>Add To Cart </Button>
29+
30+
}
1231
</Card.Body>
1332
</Card>
1433
)

0 commit comments

Comments
 (0)