Goal: I should transfer all the added product items
from the product cart
to the payment page
after the user will click the checkout button
.
Product items inside the product cart:
I should transfer the product items here in the Payment page (after checkout button is clicked):
Problem: I am not sure how to transfer the product items
(including its prices, total prices, and increment/decrement buttons) to the payment page
, even by using props and replicating mapping functionality of the Basket.jsx
(where the cart functionality is found).
I know I shouldn't be replicating the functionality, especially in terms with mapping since there would be one and only one parent component for this.
Source code for Basket.jsx
:
import React from "react";
import {
BrowserRouter as Router,
Routes,
Route,
useNavigate
} from "react-router-dom";
export default function Basket(props) {
const navigate = useNavigate();
const navigateToPaymentPage = () => {
navigate("/paymentpage");
};
const { cartItems, onAdd, onRemove } = props;
const itemsPrice = cartItems.reduce((a, c) => a + c.price * c.qty, 0);
const totalPrice = itemsPrice;
// const totalPrice = itemsPrice + discountItemPrice ---- for discount items soon
return (
<aside className="block col-1">
<h2>Cart Items</h2>
{/* Display message when cartItemsLength is 0 */}
<div>{cartItems.length === 0 && <div>Cart is Empty</div>} </div>
{/* Renders the added item to the basket of the shopping cart through mapping cartItems */}
{cartItems.map((item) => (
<div key={item.id} className="row">
<div className="col-2">
{item.name} -- ${item.price.toFixed(2)}
</div>
{/* Increment and Decrement Buttons */}
<div className="col-2">
<button onClick={() => onRemove(item)} className="remove">
-
</button>
<button onClick={() => onAdd(item)} className="add">
+
</button>
Qty: {item.qty}
</div>
<div className="col-2 text-right">
${(item.price * item.qty).toFixed(2)}
</div>
</div>
))}
{cartItems.length !== 0 && (
<>
<hr></hr>
<div className="row">
<div className="col-2">
<strong>Total Price</strong>
</div>
<div className="col-1 text-right">
<strong>${totalPrice.toFixed(2)}</strong>
</div>
</div>
<hr />
<div className="row">
<button onClick={navigateToPaymentPage}>Checkout</button>
</div>
</>
)}
</aside>
);
}
Source code for PaymentPage.jsx
:
import React from "react";
import {
BrowserRouter as Router,
Routes,
Route,
useNavigate
} from "react-router-dom";
export default function PaymentPage(props) {
//I replicated the functionality here:
const { cartItems, onAdd, onRemove } = props;
const itemsPrice = cartItems.reduce((a, c) => a + c.price * c.qty, 0);
const totalPrice = itemsPrice;
const navigate = useNavigate();
const navigateToHomeOrderPage = () => {
navigate("/");
};
return (
<aside className="block col-1">
<button
sx={{ width: 10 }}
style={{ maxWidth: "60px" }}
onClick={navigateToHomeOrderPage}
>
Go back
</button>
<h2>PAYMENT PAGE</h2>
{/* Display message when cartItemsLength is 0 */}
<div>{cartItems.length === 0 && <div>Cart is Empty</div>} </div>
{/* Renders the added item to the basket of the shopping cart through mapping cartItems */}
{cartItems.map((item) => (
<div key={item.id} className="row">
<div className="col-2">
{item.name} -- ${item.price.toFixed(2)}
</div>
{/* Increment and Decrement Buttons */}
<div className="col-2">
<button onClick={() => onRemove(item)} className="remove">
-
</button>
<button onClick={() => onAdd(item)} className="add">
+
</button>
Qty: {item.qty}
</div>
<div className="col-2 text-right">
${(item.price * item.qty).toFixed(2)}
</div>
</div>
))}
{cartItems.length !== 0 && (
<>
<hr></hr>
<div className="row">
<div className="col-2">
<strong>Total Price</strong>
</div>
<div className="col-1 text-right">
<strong>${totalPrice.toFixed(2)}</strong>
</div>
</div>
<hr />
</>
)}
</aside>
);
}
Full source codes (functioning App):
Code Basis: https://www.youtube.com/watch?v=AmIdY1Eb8tY&t=2209s
Your responses would indeed help and guide me a lot since I am very much confused on how to pass these data from one page component to another. Thus, it would be great to hear guides and responses from all of you. Thank you very much!