-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
92c6924
commit 1749050
Showing
15 changed files
with
380 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
import axios from "axios"; | ||
import React, { useEffect, useState } from "react"; | ||
import { Card, Col, Image, ListGroup, Row } from "react-bootstrap"; | ||
import { PayPalButton } from 'react-paypal-button-v2'; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import { Link } from "react-router-dom"; | ||
import { ORDER_PAY_RESET } from '../../../constants/orderConstants'; | ||
import { getOrderDetails, payOrder } from "../../../redux/actions/orderActions"; | ||
import Loader from "../Loader"; | ||
import Message from "../Message"; | ||
|
||
const OrderScreen = ({ match }) => { | ||
|
||
const orderId = match.params.id | ||
const dispatch = useDispatch(); | ||
|
||
const [sdkReady, setSdkReady] = useState(false) | ||
|
||
const orderDetails = useSelector((state) => state.orderDetails); | ||
const { order, loading, error } = orderDetails; | ||
|
||
const orderPay = useSelector((state) => state.orderPay); | ||
const { loading:loadingPay, success:successPay } = orderPay; | ||
|
||
|
||
|
||
if (!loading) { | ||
const addDecimals = (num) => { | ||
return (Math.round(num * 100) / 100).toFixed(2); | ||
|
||
}; | ||
order.itemsPrice = addDecimals( | ||
order?.orderedItems?.reduce((acc, item) => acc + item.price * item.qty, 0) | ||
); | ||
} | ||
|
||
|
||
|
||
useEffect(() => { | ||
const addPayPalScript = async (req, res) => { | ||
const { data: clientId } = await axios.get('/api/config/paypal') | ||
console.log(clientId); | ||
|
||
const script = document.createElement('script') | ||
script.type = 'text/javascript' | ||
script.src = `https://www.paypal.com/sdk/js?client-id=${clientId}` | ||
script.async = true | ||
script.onload = () => { | ||
setSdkReady(true) | ||
} | ||
document.body.appendChild(script) | ||
|
||
} | ||
|
||
if (!order || successPay) { | ||
dispatch({type:ORDER_PAY_RESET}) | ||
dispatch(getOrderDetails(orderId)) | ||
} else if (!order.isPaid) { | ||
if (!window.paypal) { | ||
addPayPalScript() | ||
}else setSdkReady(true) | ||
} | ||
|
||
}, [dispatch,order,orderId,successPay]) | ||
|
||
const successPaymentHandler = (paymentResult) => { | ||
console.log(paymentResult); | ||
dispatch(payOrder(orderId,paymentResult)) | ||
} | ||
|
||
|
||
return loading ? <Loader /> : error ? <Message variant='danger'>{error}</Message> : | ||
<> | ||
<h1>Order {order._id}</h1> | ||
<Row> | ||
<Col md={8}> | ||
<ListGroup variant="flush"> | ||
<ListGroup.Item> | ||
<h2>Shipping</h2> | ||
<p><strong>Name: </strong> {order.user.name}</p> | ||
<p><strong>Email: </strong> {order.user.email}</p> | ||
<p> | ||
<strong>Address: </strong> | ||
{order.shippingAddress.address}, {order.shippingAddress.city}{" "} | ||
{order.shippingAddress.thana}, {order.shippingAddress.houseNumber} | ||
</p> | ||
{order.isDelivered ? <Message variant='success'>Delivery Done</Message> : <Message variant='danger'>Not Delivered</Message>} | ||
|
||
|
||
</ListGroup.Item> | ||
|
||
<ListGroup.Item> | ||
<h2>Payment Method</h2> | ||
<p> | ||
<strong>Method: </strong> | ||
{order.paymentMethod} | ||
</p> | ||
|
||
{order.isPaid ? <Message variant='success'>Payment Done</Message> : <Message variant='danger'>Not Paid</Message> } | ||
|
||
</ListGroup.Item> | ||
<ListGroup.Item> | ||
<h2>Order Items</h2> | ||
{order?.orderedItems?.length === 0 ? ( | ||
<Message>Order is empty</Message> | ||
) : ( | ||
<ListGroup variant="flush"> | ||
{order?.orderedItems?.map((item, index) => ( | ||
<ListGroup.Item key={index}> | ||
<Row> | ||
<Col md={1}> | ||
<Image | ||
src={item.image} | ||
alt={item.name} | ||
fluid | ||
rounded | ||
/> | ||
</Col> | ||
<Col> | ||
<Link to={`/product/${item.product}`}> | ||
{item.name} | ||
</Link> | ||
</Col> | ||
<Col md={4}> | ||
{item.qty} x ৳{item.price} = ৳{item.qty * item.price} | ||
</Col> | ||
</Row> | ||
</ListGroup.Item> | ||
))} | ||
</ListGroup> | ||
)} | ||
</ListGroup.Item> | ||
</ListGroup> | ||
</Col> | ||
<Col md={4}> | ||
<Card> | ||
<ListGroup variant="flush"> | ||
<ListGroup.Item> | ||
<h3>Order Summary</h3> | ||
</ListGroup.Item> | ||
<ListGroup.Item> | ||
<Row> | ||
<Col>Items</Col> | ||
<Col>৳{order.itemsPrice}</Col> | ||
</Row> | ||
</ListGroup.Item> | ||
<ListGroup.Item> | ||
<Row> | ||
<Col>Delivery Charge</Col> | ||
<Col>৳{order.shippingPrice}</Col> | ||
</Row> | ||
</ListGroup.Item> | ||
|
||
<ListGroup.Item> | ||
<Row> | ||
<Col>Total</Col> | ||
<Col>৳{order.totalPrice}</Col> | ||
</Row> | ||
</ListGroup.Item> | ||
|
||
{!order.isPaid && ( | ||
<ListGroup.Item> | ||
{loadingPay && <Loader />} | ||
{!sdkReady ? <Loader /> : ( | ||
<PayPalButton | ||
amount={order.totalPrice} | ||
onSuccess={successPaymentHandler} | ||
/> | ||
)} | ||
</ListGroup.Item> | ||
|
||
)} | ||
|
||
</ListGroup> | ||
</Card> | ||
</Col> | ||
</Row> | ||
</> | ||
|
||
}; | ||
|
||
export default OrderScreen; |
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
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
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
Oops, something went wrong.