import React, { useState, useEffect } from "react"; import Layout from "../core/Layout"; import { isAuthenticated } from "../auth"; /* eslint-disable no-unused-vars */ import { Link } from "react-router-dom"; import { listOrders, getStatusValues, updateOrderStatus } from "./apiAdmin"; import moment from "moment"; const Orders = () => { const [orders, setOrders] = useState([]); const [statusValues, setStatusValues] = useState([]); const { user, token } = isAuthenticated(); const loadOrders = () => { listOrders(user._id, token).then(data => { if (data.error) { console.log(data.error); } else { setOrders(data); } }); }; const loadStatusValues = () => { getStatusValues(user._id, token).then(data => { if (data.error) { console.log(data.error); } else { setStatusValues(data); } }); }; useEffect(() => { loadOrders(); loadStatusValues(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const showOrdersLength = () => { if (orders.length > 0) { return (

Total Place orders: {orders.length}

); } else { return

No Place orders

; } }; const showInput = (key, value) => (
{key}
); const handleStatusChange = (e, orderId) => { updateOrderStatus(user._id, token, orderId, e.target.value).then( data => { if (data.error) { console.log("Status update failed"); } else { loadOrders(); } } ); }; const showStatus = o => (

Status: {o.status}

); return (
{showOrdersLength()} {orders.map((o, oIndex) => { return (

Order ID: {o._id}

  • {showStatus(o)}
  • Transaction ID: {o.transaction_id}
  • Amount: ${o.amount}
  • Ordered by: {o.user.name}
  • Ordered on:{" "} {moment(o.createdAt).fromNow()}
  • Delivery address: {o.address}

Total products in the order:{" "} {o.products.length}

{o.products.map((p, pIndex) => (
{showInput("Product name", p.name)} {showInput("Product price", p.price)} {showInput("Product total", p.count)} {showInput("Product Id", p._id)}
))}
); })}
); }; export default Orders;