import React, { useEffect, useState } from 'react' import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper, Typography, Skeleton, Box, Button, TablePagination, } from '@mui/material' import Axios from '../../axios' import { isAutheticated } from '../../auth' import { useNavigate } from 'react-router-dom' const Order = () => { const [orders, setOrders] = useState([]) const [loading, setLoading] = useState(true) const [page, setPage] = useState(0) const [rowsPerPage, setRowsPerPage] = useState(5) const [totalOrders, setTotalOrders] = useState(0) const token = isAutheticated() const navigate = useNavigate() const formatAMPM = (date) => { var hours = new Date(date).getHours() var minutes = new Date(date).getMinutes() var ampm = hours >= 12 ? 'PM' : 'AM' hours = hours % 12 hours = hours ? hours : 12 // the hour '0' should be '12' minutes = minutes < 10 ? '0' + minutes : minutes var strTime = hours + ':' + minutes + ' ' + ampm return strTime } useEffect(() => { // Fetch orders from the API with pagination const fetchOrders = async () => { try { const response = await Axios.get('/api/get-placed-order-pd', { headers: { 'Access-Control-Allow-Origin': '*', Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', }, params: { page: page + 1, // page number for API (usually starts from 1) limit: rowsPerPage, // number of rows per page }, }) setOrders(response.data.plcaedOrders) setTotalOrders(response.data.totalOrders) // Ensure the API returns the total count of orders } catch (error) { console.error('Error fetching orders:', error) } finally { setLoading(false) } } fetchOrders() }, [page, rowsPerPage]) const handleChangePage = (event, newPage) => { setPage(newPage) } const handleChangeRowsPerPage = (event) => { setRowsPerPage(parseInt(event.target.value, 10)) setPage(0) // Reset page to 0 when rows per page changes } return ( Order placed list Order ID Order Date Items Order Value Status Action {loading ? ( Array.from(new Array(rowsPerPage)).map((_, index) => ( )) ) : orders.length > 0 ? ( orders.map((order) => ( {order.uniqueId} {new Date(`${order?.createdAt}`).toDateString()} , {`${formatAMPM(order?.createdAt)}`} {order.orderItem.length} {order.grandTotal.toFixed(2)} {order.status} )) ) : ( Data not found )}
) } export default Order