import { useNavigate } from "react-router-dom"; import { productApi } from "../../apis/productApi"; import { startLoading, stopLoading } from "../../features/loading/loadingSlice"; import { useAppDispatch } from "../../hooks/useRedux"; import { OrderHistory } from "../../services/product/type"; import { useMutation } from "@tanstack/react-query"; import React, { useState, useEffect } from "react"; import { convertOrderStatusToColor, convertOrderStatusToText, formatCurrency, formatNumber, } from "../../logic/loigicUtils"; import { useTranslation } from "react-i18next"; const OrderHistoryView = () => { const [searchOrder, setSearchOrder] = useState(""); const lastDay = new Date(new Date().setDate(new Date().getDate() - 30)) .toISOString() .split("T")[0]; const today = new Date().toISOString().split("T")[0]; // status is one of: -1, 1, 2 const [status, setStatus] = useState("-1"); const [fromDate, setFromDate] = useState(lastDay); const [toDate, setToDate] = useState(today); const [orders, setOrders] = useState([]); const { t } = useTranslation(); const dispatch = useAppDispatch(); const navigate = useNavigate(); useEffect(() => { getOrderMutation.mutate(); }, []); const getOrderMutation = useMutation({ mutationFn: async () => { dispatch(startLoading({})); const res = await productApi.getOrderHistory({ searchOrder: searchOrder, fromDate, toDate, status, }); return res; }, onSuccess: (data) => { dispatch(stopLoading()); console.log("Get order history response data:", data); if (data && data.errorCode === "0") { console.log("Get order history successful"); setOrders(data.data || []); } else { console.error("Get order history failed, no token received"); } }, onError: (error: any) => { dispatch(stopLoading()); console.error("Get order history error:", error.response.data); }, }); // Input style matching LoginView const inputClass = "w-full bg-slate-50 border-2 border-transparent focus:border-[#EE0434]/20 rounded-2xl py-3 px-5 focus:outline-none focus:bg-white transition-all text-slate-700 font-bold placeholder:text-slate-300 text-sm md:text-base h-[50px]"; // const filteredOrders = orders.filter((order) => { // const matchesSearch = order.id.includes(searchOrder); // const matchesStatus = status === "-1" || order.status === status; // return matchesSearch && matchesStatus; // }); return (
{/* Breadcrumb */}
{/* Filter Bar */}
{ setSearchOrder(e.target.value); getOrderMutation.mutate(); }} />
(e.target.type = "date")} onBlur={(e) => (e.target.type = "date")} className={inputClass} value={fromDate} onChange={(e) => { setFromDate(e.target.value); getOrderMutation.mutate(); }} />
(e.target.type = "date")} onBlur={(e) => (e.target.type = "date")} className={inputClass} value={toDate} onChange={(e) => { setToDate(e.target.value); getOrderMutation.mutate(); }} />
{/* Order List */}
{orders.length === 0 ? (

No orders found

) : ( orders.map((order) => (
navigate(`/order-history-detail/${order.id}`, { state: { id: order.id, orderHistory: order }, }) } className="bg-white rounded-[20px] p-6 shadow-sm border border-slate-100 hover:shadow-md transition-shadow flex flex-col md:flex-row justify-between items-start md:items-center gap-6" >
{convertOrderStatusToText(order.status)}

{order.orderCode}

{order.createdDate}

{/* {order.flags.map((flag, idx) => (
{flag}
))} */}

{formatCurrency(order.paymentMoney, order.curency)}

{formatCurrency(order.totalMoney, order.curency)}

)) )}
); }; export default OrderHistoryView;