add cart item component and use cart context
This commit is contained in:
		
							parent
							
								
									cae3ccc644
								
							
						
					
					
						commit
						cee3570ef0
					
				@ -1,11 +1,37 @@
 | 
				
			|||||||
 | 
					import { useContext } from "react";
 | 
				
			||||||
import classes from "./Cart.module.css";
 | 
					import classes from "./Cart.module.css";
 | 
				
			||||||
import Modal from "../UI/Modal";
 | 
					import Modal from "../UI/Modal";
 | 
				
			||||||
 | 
					import CartContext from "../../store/cart-context";
 | 
				
			||||||
 | 
					import CartItem from "./CartItem";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Cart = (props) => {
 | 
					const Cart = (props) => {
 | 
				
			||||||
 | 
					  const cartCtx = useContext(CartContext);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const totalAmount = `$${cartCtx.totalAmount.toFixed(2)}`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const hasItems = cartCtx.items.length > 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const cartItemAddHandler = (item) => {
 | 
				
			||||||
 | 
					    cartCtx.addItem(item);
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const cartItemRemoveHandler = (id) => {
 | 
				
			||||||
 | 
					    cartCtx.removeItem(id);
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const cartItems = (
 | 
					  const cartItems = (
 | 
				
			||||||
    <ul className={classes["cart-items"]}>
 | 
					    <ul className={classes["cart-items"]}>
 | 
				
			||||||
      {[{ id: "c1", name: "sushi", amount: 2, price: 12.99 }].map((item) => (
 | 
					      {cartCtx.items.map((item) => (
 | 
				
			||||||
        <li>{item.name}</li>
 | 
					        <CartItem
 | 
				
			||||||
 | 
					          key={item.id}
 | 
				
			||||||
 | 
					          name={item.name}
 | 
				
			||||||
 | 
					          amount={item.amount}
 | 
				
			||||||
 | 
					          price={item.price}
 | 
				
			||||||
 | 
					          onRemove={cartItemRemoveHandler.bind(null, item.id)}
 | 
				
			||||||
 | 
					          onAdd={cartItemAddHandler.bind(null, item)}
 | 
				
			||||||
 | 
					        >
 | 
				
			||||||
 | 
					          {item.name}
 | 
				
			||||||
 | 
					        </CartItem>
 | 
				
			||||||
      ))}
 | 
					      ))}
 | 
				
			||||||
    </ul>
 | 
					    </ul>
 | 
				
			||||||
  );
 | 
					  );
 | 
				
			||||||
@ -15,13 +41,13 @@ const Cart = (props) => {
 | 
				
			|||||||
      {cartItems}
 | 
					      {cartItems}
 | 
				
			||||||
      <div className={classes.total}>
 | 
					      <div className={classes.total}>
 | 
				
			||||||
        <span>Total Amount</span>
 | 
					        <span>Total Amount</span>
 | 
				
			||||||
        <span>35.63</span>
 | 
					        <span>{totalAmount}</span>
 | 
				
			||||||
      </div>
 | 
					      </div>
 | 
				
			||||||
      <div className={classes.actions}>
 | 
					      <div className={classes.actions}>
 | 
				
			||||||
        <button className={classes["button--alt"]} onClick={props.onHideCart}>
 | 
					        <button className={classes["button--alt"]} onClick={props.onHideCart}>
 | 
				
			||||||
          Close
 | 
					          Close
 | 
				
			||||||
        </button>
 | 
					        </button>
 | 
				
			||||||
        <button className={classes.button}>Order</button>
 | 
					        {hasItems && <button className={classes.button}>Order</button>}
 | 
				
			||||||
      </div>
 | 
					      </div>
 | 
				
			||||||
    </Modal>
 | 
					    </Modal>
 | 
				
			||||||
  );
 | 
					  );
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										23
									
								
								src/components/Cart/CartItem.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/components/Cart/CartItem.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,23 @@
 | 
				
			|||||||
 | 
					import classes from './CartItem.module.css';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const CartItem = (props) => {
 | 
				
			||||||
 | 
					  const price = `$${props.price.toFixed(2)}`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return (
 | 
				
			||||||
 | 
					    <li className={classes['cart-item']}>
 | 
				
			||||||
 | 
					      <div>
 | 
				
			||||||
 | 
					        <h2>{props.name}</h2>
 | 
				
			||||||
 | 
					        <div className={classes.summary}>
 | 
				
			||||||
 | 
					          <span className={classes.price}>{price}</span>
 | 
				
			||||||
 | 
					          <span className={classes.amount}>x {props.amount}</span>
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					      </div>
 | 
				
			||||||
 | 
					      <div className={classes.actions}>
 | 
				
			||||||
 | 
					        <button onClick={props.onRemove}>−</button>
 | 
				
			||||||
 | 
					        <button onClick={props.onAdd}>+</button>
 | 
				
			||||||
 | 
					      </div>
 | 
				
			||||||
 | 
					    </li>
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default CartItem;
 | 
				
			||||||
							
								
								
									
										65
									
								
								src/components/Cart/CartItem.module.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								src/components/Cart/CartItem.module.css
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,65 @@
 | 
				
			|||||||
 | 
					.cart-item {
 | 
				
			||||||
 | 
					  display: flex;
 | 
				
			||||||
 | 
					  justify-content: space-between;
 | 
				
			||||||
 | 
					  align-items: center;
 | 
				
			||||||
 | 
					  border-bottom: 2px solid #8a2b06;
 | 
				
			||||||
 | 
					  padding: 1rem 0;
 | 
				
			||||||
 | 
					  margin: 1rem 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.cart-item h2 {
 | 
				
			||||||
 | 
					  margin: 0 0 0.5rem 0;
 | 
				
			||||||
 | 
					  color: #363636;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.summary {
 | 
				
			||||||
 | 
					  width: 10rem;
 | 
				
			||||||
 | 
					  display: flex;
 | 
				
			||||||
 | 
					  justify-content: space-between;
 | 
				
			||||||
 | 
					  align-items: center;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.price {
 | 
				
			||||||
 | 
					  font-weight: bold;
 | 
				
			||||||
 | 
					  color: #8a2b06;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.amount {
 | 
				
			||||||
 | 
					  font-weight: bold;
 | 
				
			||||||
 | 
					  border: 1px solid #ccc;
 | 
				
			||||||
 | 
					  padding: 0.25rem 0.75rem;
 | 
				
			||||||
 | 
					  border-radius: 6px;
 | 
				
			||||||
 | 
					  color: #363636;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.actions {
 | 
				
			||||||
 | 
					  display: flex;
 | 
				
			||||||
 | 
					  flex-direction: column;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@media (min-width: 768px) {
 | 
				
			||||||
 | 
					  .actions {
 | 
				
			||||||
 | 
					    flex-direction: row;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.cart-item button {
 | 
				
			||||||
 | 
					  font: inherit;
 | 
				
			||||||
 | 
					  font-weight: bold;
 | 
				
			||||||
 | 
					  font-size: 1.25rem;
 | 
				
			||||||
 | 
					  color: #8a2b06;
 | 
				
			||||||
 | 
					  border: 1px solid #8a2b06;
 | 
				
			||||||
 | 
					  width: 3rem;
 | 
				
			||||||
 | 
					  text-align: center;
 | 
				
			||||||
 | 
					  border-radius: 6px;
 | 
				
			||||||
 | 
					  background-color: transparent;
 | 
				
			||||||
 | 
					  cursor: pointer;
 | 
				
			||||||
 | 
					  margin-left: 1rem;
 | 
				
			||||||
 | 
					  margin: 0.25rem;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.cart-item button:hover,
 | 
				
			||||||
 | 
					.cart-item button:active {
 | 
				
			||||||
 | 
					  background-color: #8a2b06;
 | 
				
			||||||
 | 
					  color: white;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user