diff --git a/src/App.jsx b/src/App.jsx index fdee086..687c4fe 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,10 +1,9 @@ -import React, { useState } from 'react'; - -import ProductList from './components/ProductList'; -import Cart from './components/Cart'; -import CartContext from './contexts/CartContext'; - +import React, { useState } from "react"; +import ProductList from "./components/ProductList"; +import Cart from "./components/Cart"; +import CartContext from "./contexts/CartContext"; +import styles from "./App.module.css"; // oldCart === newCart // { @@ -25,6 +24,7 @@ function App() { * } */ const [cart, setCart] = useState({}); + const [show, setShow] = useState(false); function increaseQuantity(product) { const newCart = { ...cart }; @@ -37,9 +37,9 @@ function App() { title: product.title, price: product.price, quantity: 0, - } + }; } - + newCart[product.id].quantity += 1; setCart(newCart); @@ -59,27 +59,35 @@ function App() { setCart(newCart); } - console.log('App rendered'); + console.log("App rendered"); return ( - + +
+ {show === false && ( + + )} + {show === true && ( + + )} +
+ {show && ( +
+ +
+ )}
-
- ) + ); } export default App; - // By default a component re renders: // 1. It's internal state changes // 2. It's props change // 3. If it subscribed to a context and value of the context changes // 4. Parent re renders -// - +// diff --git a/src/App.module.css b/src/App.module.css new file mode 100644 index 0000000..1f04c4d --- /dev/null +++ b/src/App.module.css @@ -0,0 +1,19 @@ +.carttoggle { + position: absolute; + top: 0; + right: 0; + z-index: 999; +} + +.sidenav { + height: 100%; + width: 250px; + position: fixed; + z-index: 1; + top: 0; + right: 0; + background-color: #e7c1c1; + overflow-x: hidden; + transition: 0.5s; + padding-top: 60px; +} diff --git a/src/components/Cart/Cart.jsx b/src/components/Cart/Cart.jsx index 6c706b2..be39563 100644 --- a/src/components/Cart/Cart.jsx +++ b/src/components/Cart/Cart.jsx @@ -1,8 +1,8 @@ -import React, { useContext } from 'react'; +import React, { useContext, useState } from "react"; -import CartContext from '../../contexts/CartContext'; +import CartContext from "../../contexts/CartContext"; -import styles from './Cart.module.css'; +import styles from "./Cart.module.css"; // { // '1': { id: '1' }, @@ -13,24 +13,27 @@ import styles from './Cart.module.css'; function Cart() { const { cart } = useContext(CartContext); const cartList = Object.values(cart); - - console.log('Cart rendered'); + const [cartTotal, setCartTotal] = useState(0); if (cartList.length === 0) { - return ( -
No items in the cart!
- ); + return
No items in the cart!
; } else { return ( -
    - {cartList.map(cartItem => ( -
  1. -
    {cartItem.title}
    -
    Quantity: {cartItem.quantity}
    -
  2. - ))} -
- ) +
+
    + {cartList.map((cartItem) => ( +
  1. +
    {cartItem.title}
    +
    + Rs:{cartItem.price} * {cartItem.quantity} = Rs: + {cartItem.price * cartItem.quantity} +
    +
  2. + ))} +
+
Cart Total:{cartTotal}
+
+ ); } } diff --git a/src/components/ProductCard/ProductCard.module.css b/src/components/ProductCard/ProductCard.module.css index 8d8d102..0aae022 100644 --- a/src/components/ProductCard/ProductCard.module.css +++ b/src/components/ProductCard/ProductCard.module.css @@ -1,5 +1,5 @@ .card { padding: 10px; border: 1px solid black; - margin: 10px; -} \ No newline at end of file + margin: 25px 10px 30px 10px; +}