import React, { useState, useEffect, useRef } from 'react' import axios from 'axios' import { useNavigate, useParams } from 'react-router-dom' import QRCode from 'react-qr-code' import ReactToPrint from 'react-to-print' import { isAutheticated } from 'src/auth' import PrintOrderDetails from './PrintOrderDetails.js' function ViewOrder() { const { status, id } = useParams() const navigate = useNavigate() const printOrderRef = useRef() const token = isAutheticated() const [orderData, setOrderData] = useState({}) const [loading, setLoading] = useState(true) const [orderStatus, setOrderStatus] = useState('') const [data, setData] = useState({ courier_name: '', tracking_id: '', }) useEffect(() => { function getOrderDetails() { setLoading(true) axios .get(`/api/order/${id}`, { headers: { 'Access-Control-Allow-Origin': '*', Authorization: `Bearer ${token}` }, }) .then((res) => { setLoading(false) setOrderData(res.data.data) }) .catch((err) => { setLoading(false) getBack() }) } getOrderDetails() }, []) const handleChange = (e) => { if (e.target.type === 'text') { setData((prev) => ({ ...prev, [e.target.id]: e.target.value })) } else { setOrderStatus(e.target.value) } } function handleSubmit() { if (orderStatus === '') return if (orderData.status === 'processing') { if (data.courier_name.trim() === '' || data.tracking_id.trim() === '') { swal({ title: 'Warning', text: 'Enter Courier Name And Tracking ID!', icon: 'error', button: 'Retry', dangerMode: true, }) return } } setLoading(true) axios .patch( `/api/order/${id}`, { ...data, status: orderStatus }, { headers: { 'Access-Control-Allow-Origin': '*', Authorization: `Bearer ${token}`, }, }, ) .then((res) => { swal({ title: 'Updated', text: 'Order updated!', icon: 'success', button: 'Return', }) setLoading(false) getBack() }) .catch((err) => { setLoading(false) swal({ title: 'Warning', text: 'Something went wrong!', icon: 'error', button: 'Retry', dangerMode: true, }) }) } function getBack() { navigate(`/orders/${status}`, { replace: true }) } return ( <> {' '}
Order Details
{(orderData?.status === 'new' || orderData?.status === 'processing') && ( ( )} content={() => printOrderRef.current} /> )} {orderData?.status !== 'cancelled' && orderData?.status !== 'returned' && orderData?.status !== 'delivered' && ( )}
{orderData?.items && orderData?.items?.length > 0 && orderData.items.map((e, i) => (
{e.product.name}
{e.product.name}

SKU: {e.product?.sku}

Variant: {e.variant.size}

Quantity: {e.quantity}

{' '}

Price: Rs.{e.price}

{e?.igst && (

IGST: {e.igst}%

)} {e?.cgst && (

CGST: {e.cgst}%

)} {e?.sgst && (

SGST: {e.sgst}%

)}

Total: Rs.{e.amount}


))}
{status !== 'cancelled' && status !== 'returned' && status !== 'delivered' && (
)}
{orderData?.order_id && (

Order ID QR Code:

)}
{orderData.status === 'processing' && ( <>
)}
{orderData?.address && ( <>
)} {orderData?.courier_name && (
)} {orderData?.tracking_id && (
)} {/*
*/}
{' '}
Order Placed On : {orderData?.status_timeline?.new ? new Date(orderData?.status_timeline?.new).toLocaleString('en-IN', { month: 'short', day: 'numeric', year: 'numeric', hour: '2-digit', minute: 'numeric', hour12: true, }) : new Date(orderData?.placed_on).toLocaleString('en-IN', { month: 'short', day: 'numeric', year: 'numeric', hour: '2-digit', minute: 'numeric', hour12: true, })}
Processing Started : {orderData?.status_timeline?.processing ? new Date(orderData?.status_timeline?.processing).toLocaleString( 'en-IN', { month: 'short', day: 'numeric', year: 'numeric', hour: '2-digit', minute: 'numeric', hour12: true, }, ) : '-'}
Dispatched On : {orderData?.status_timeline?.dispatched ? new Date(orderData?.status_timeline?.dispatched).toLocaleString( 'en-IN', { month: 'short', day: 'numeric', year: 'numeric', hour: '2-digit', minute: 'numeric', hour12: true, }, ) : '-'}
Delivered On : {orderData?.status_timeline?.delivered ? new Date(orderData?.status_timeline?.delivered).toLocaleString( 'en-IN', { month: 'short', day: 'numeric', year: 'numeric', hour: '2-digit', minute: 'numeric', hour12: true, }, ) : '-'}
Cancelled On : {orderData?.status_timeline?.cancelled ? new Date(orderData?.status_timeline?.cancelled).toLocaleString( 'en-IN', { month: 'short', day: 'numeric', year: 'numeric', hour: '2-digit', minute: 'numeric', hour12: true, }, ) : '-'}
Returned On : {orderData?.status_timeline?.returned ? new Date(orderData?.status_timeline?.returned).toLocaleString( 'en-IN', { month: 'short', day: 'numeric', year: 'numeric', hour: '2-digit', minute: 'numeric', hour12: true, }, ) : '-'}
) } export default ViewOrder