diff --git a/src/pages/PropDrillingExercise.js b/src/pages/PropDrillingExercise.js index 39a197a..51db389 100644 --- a/src/pages/PropDrillingExercise.js +++ b/src/pages/PropDrillingExercise.js @@ -1,11 +1,9 @@ import React, { useState } from "react"; -import { Container, Button, Typography, Box, Grid } from "@mui/material" +import { Container, Button, Typography, Box, Grid } from "@mui/material"; import productOne from "../images/product1.gif"; import productTwo from "../images/product2.gif"; import ReactJson from "react-json-view"; -import WrapperBox from "../components/WrapperBox" - - +import WrapperBox from "../components/WrapperBox"; const RootComponent = (props) => { // eslint-disable-next-line @@ -28,21 +26,48 @@ const RootComponent = (props) => { // Write a function called addProductToCart() that takes a product object as an argument // Example newProduct = { id: "p1", title: "Product 1", price: 1999 } // The function will add one new product into the cart - + const addProductToCart = (newProduct) => { + const newProductList = cart.products.map((cartProduct) => { + if (cartProduct.id === newProduct.id) { + cartProduct.qty += 1; + cartProduct.price += newProduct.price; + } + return cartProduct; + }); + const newTotalPrice = cart.totalPrice + newProduct.price; + setCart({ products: newProductList, totalPrice: newTotalPrice }); + }; // Step 2 // Write a function called removeProductFromCart() that takes a product object as an argument // Example removedProduct = { id: "p1", title: "Product 1", price: 1999 } // The function will remove one product from the cart. The min value of quantity is 0 - + const removeProductFromCart = (removeProduct) => { + let newTotalPrice = cart.totalPrice; + const newProductList = cart.products.map((cartProduct) => { + if (cartProduct.id === removeProduct.id && cartProduct.qty > 0) { + cartProduct.qty -= 1; + cartProduct.price -= removeProduct.price; + newTotalPrice -= removeProduct.price; + } + return cartProduct; + }); + setCart({ products: newProductList, totalPrice: newTotalPrice }); + }; // Step 3 // Pass the functions to the product components to handle the click event of the Add/Remove buttons return ( - + RootComponent {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} @@ -57,7 +82,11 @@ const RootComponent = (props) => { - + @@ -70,18 +99,31 @@ const RootComponent = (props) => { const ProductPage = (props) => { return ( - - + Product Page {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} - + - + @@ -91,9 +133,15 @@ const ProductPage = (props) => { const CartPage = (props) => { return ( - + Cart Page {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} @@ -104,7 +152,9 @@ const CartPage = (props) => { - Total Price: 💵 {props.cart.totalPrice} + + Total Price: 💵 {props.cart.totalPrice} + @@ -114,22 +164,38 @@ const CartPage = (props) => { const ProductOne = (props) => { return ( - + {props.product.title} {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} - + Product One - 💵 {props.product.price} + + 💵 {props.product.price} + - +
- -
@@ -140,25 +206,42 @@ const ProductOne = (props) => { }; const ProductTwo = (props) => { - return ( - + {props.product.title} {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} Product Two - 💵 {props.product.price} + + 💵 {props.product.price} + - +
- -
@@ -171,14 +254,24 @@ const ProductTwo = (props) => { const CartProductOne = (props) => { return ( - + CartProduct 1 {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} - - Quantity: {props.product.qty} - Price: 💵 {props.product.price} + + + Quantity: {props.product.qty} + + + Price: 💵 {props.product.price} + ); @@ -187,14 +280,24 @@ const CartProductOne = (props) => { const CartProductTwo = (props) => { return ( - + CartProduct 2 {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} - + - Quantity: {props.product.qty} - Price: 💵 {props.product.price} + + Quantity: {props.product.qty} + + + Price: 💵 {props.product.price} + ); @@ -204,7 +307,9 @@ const PropDrillingExercise = () => { return (
- How to add products to the cart? + + How to add products to the cart? +
diff --git a/src/pages/ReduxExercise.js b/src/pages/ReduxExercise.js index a25d9c6..f761b29 100644 --- a/src/pages/ReduxExercise.js +++ b/src/pages/ReduxExercise.js @@ -1,17 +1,24 @@ import React from "react"; -import { Container, Button, Typography, Box, Grid } from "@mui/material" +import { Container, Button, Typography, Box, Grid } from "@mui/material"; import productOne from "../images/product1.gif"; import productTwo from "../images/product2.gif"; import ReactJson from "react-json-view"; -import { useDispatch } from "react-redux"; +import { useDispatch, useSelector } from "react-redux"; import WrapperBox from "../components/WrapperBox"; +import { addToCart, removeCart } from "../service/cart/slice"; const RootComponent = (props) => { return ( - + RootComponent {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} @@ -29,9 +36,15 @@ const RootComponent = (props) => { const ProductPage = (props) => { return ( - + Product Page {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} @@ -49,13 +62,19 @@ const ProductPage = (props) => { const CartPage = (props) => { // Step 6 // Replace the line below to get data of the second product from state.cart.totalPrice - const totalPrice = "..."; + const totalPrice = useSelector((state) => state.cart.totalPrice); return ( - + Cart Page {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} @@ -66,7 +85,9 @@ const CartPage = (props) => { - Total Price: 💵 {totalPrice} + + Total Price: 💵 {totalPrice} + @@ -77,7 +98,7 @@ const ProductOne = (props) => { // Step 4 // Replace the line below to get data of the first product from state.product // You should see the price is updated - const product = { id: "...", title: "...", price: "..." }; + const product = useSelector((state) => state.product[0]); // Step 7 // Define: const dispatch = useDispatch(); @@ -87,6 +108,13 @@ const ProductOne = (props) => { // eslint-disable-next-line const dispatch = useDispatch(); + const addProduct = () => { + dispatch(addToCart(product)); + }; + const removeProduct = () => { + dispatch(removeCart(product)); + }; + // Step 8 // Create a function to handle click event of the button Remove // In the function, dispatch cartActions.removeProduct(product) to trigger the action remove product from the cart @@ -94,28 +122,44 @@ const ProductOne = (props) => { return ( - + {product.title} {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} - + Product One - 💵 {product.price} + + 💵 {product.price} + - +
- -
-
+
); }; @@ -123,29 +167,53 @@ const ProductTwo = (props) => { // Step 5 // Replace the line below to get data of the second product from state.product // You should see the price is updated - const product = { id: "...", title: "...", price: "..." }; + const product = useSelector((state) => state.product[1]); // Step 9 // Repeat step 7 and 8 for this component + const dispatch = useDispatch(); + + const addProduct = () => { + dispatch(addToCart(product)); + }; + const removeProduct = () => { + dispatch(removeCart(product)); + }; return ( - + {product.title} {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} Product Two - 💵 {product.price} + + 💵 {product.price} + - +
- -
@@ -159,19 +227,30 @@ const CartProductOne = (props) => { // Step 2 // Replace the line below to get data of the first product from state.cart.products // Change the price of products in `service/cart/slice.js` to see the effect - const product = { price: "...", qty: "..." }; + const product = useSelector((state) => state.cart.products[0]); + console.log("product", product); return ( - + CartProduct 1 {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} - - Quantity: {product.qty} - Price: 💵 {product.price} + + + Quantity: {product.qty} + + + Price: 💵 {product.price} + @@ -182,18 +261,28 @@ const CartProductTwo = (props) => { // Step 3 // Replace the line below to get data of the second product from state.cart.products // Change the price of products in `service/cart/slice.js` to see the effect - const product = { price: "...", qty: "..." }; + const product = useSelector((state) => state.cart.products[1]); return ( - + CartProduct 2 {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} - + - Quantity: {product.qty} - Price: 💵 {product.price} + + Quantity: {product.qty} + + + Price: 💵 {product.price} + ); @@ -204,15 +293,21 @@ const Store = (props) => { // use useSelector() to get the data of products and cart in the store // pass {cart, product} to the src attribute of the component + const products = useSelector((state) => state.product); + const cart = useSelector((state) => state.cart); return ( - + Store { return ( - + RootComponent {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} @@ -30,9 +36,15 @@ const RootComponent = (props) => { const ProductPage = (props) => { return ( - + Product Page {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} @@ -54,9 +66,15 @@ const CartPage = (props) => { return ( - + Cart Page {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} @@ -67,7 +85,9 @@ const CartPage = (props) => { - Total Price: 💵 {totalPrice} + + Total Price: 💵 {totalPrice} + @@ -80,7 +100,6 @@ const ProductOne = (props) => { // You should see the price is updated const product = useSelector((state) => state.product[0]); - // Step 7 // Define: const dispatch = useDispatch(); // Create a function to handle click event of the button Add @@ -104,22 +123,31 @@ const ProductOne = (props) => { return ( - + {product.title} {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} - + Product One - 💵 {product.price} + + 💵 {product.price} + - +
-
+
); }; @@ -156,22 +184,31 @@ const ProductTwo = (props) => { }; return ( - + {product.title} {`({`} - {Object.keys(props).join(", ")} + + {Object.keys(props).join(", ")} + {`})`} Product Two - 💵 {product.price} + + 💵 {product.price} + - +