-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
52 additions
and
34 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,58 @@ | ||
import { Fragment, useContext } from "react"; | ||
import classes from "./Cart.module.css"; | ||
import CartContext from "../../store/cart-context"; | ||
import CartItem from "../../store/CartItem"; | ||
import { Fragment, useContext } from 'react'; | ||
|
||
const CartList = (props) => { | ||
import CartItem from './CartItem'; | ||
import classes from './Cart.module.css'; | ||
import CartContext from '../../store/cart-context'; | ||
|
||
const Cart = (props) => { | ||
const cartCtx = useContext(CartContext); | ||
|
||
const totalAmount = `$${cartCtx.totalAmount.toFixed(2)}`; | ||
const check = cartCtx.items.length > 0; | ||
const hasItems = cartCtx.items.length > 0; | ||
|
||
const orderhandler = () => { | ||
console.log("Ordering..."); | ||
const cartItemRemoveHandler = (id) => { | ||
cartCtx.removeItem(id); | ||
}; | ||
|
||
const cartItemAddHandler =(id) => {}; | ||
const cartItemAddHandler = (item) => { | ||
cartCtx.addItem({...item, amount: 1}); | ||
}; | ||
|
||
const cartItemRemoveHandler = (item) => {}; | ||
const orderingHandler = () =>{ | ||
console.log('Ordering...') | ||
}; | ||
|
||
const CartItems = ( | ||
<ul className={classes["cart-items"]}> | ||
|
||
const cartItems = ( | ||
<ul className={classes['cart-items']}> | ||
{cartCtx.items.map((item) => ( | ||
<CartItem | ||
key={item.id} | ||
name={item.name} | ||
amount={item.amount} | ||
price={item.price} | ||
onRemove={cartItemRemoveHandler.bind(null, item.id)} | ||
onAdd={cartItemAddHandler(null,item)} | ||
onAdd={cartItemAddHandler.bind(null, item)} | ||
/> | ||
))} | ||
</ul> | ||
); | ||
|
||
return ( | ||
<Fragment> | ||
{CartItems} | ||
{console.log(props)} | ||
{!check && <label>Cart empty</label>} | ||
{cartItems} | ||
<div className={classes.total}> | ||
<span>Total Amount</span> | ||
<span>{totalAmount}</span> | ||
</div> | ||
|
||
<div className={classes.actions}> | ||
<button className={classes["button--alt"]} onClick={props.onClose}> | ||
<button className={classes['button--alt']} onClick={props.onClose}> | ||
Close | ||
</button> | ||
{check && ( | ||
<button className={classes.button} onClick={orderhandler}> | ||
Order | ||
</button> | ||
)} | ||
{hasItems && <button className={classes.button} onClick={orderingHandler} >Order</button>} | ||
</div> | ||
</Fragment> | ||
); | ||
}; | ||
|
||
export default CartList; | ||
export default Cart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters