11import { Button , Container , Navbar , Modal } from 'react-bootstrap' ;
2- import { useState } from 'react' ;
2+ import { useState , useContext } from 'react' ;
3+ import { CartContext } from '../CartContext' ;
34
45function 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 </ >
0 commit comments