"use client"; import { order_api } from "@/features/order/lib/api"; import type { Order } from "@/features/order/lib/type"; import formatPhone from "@/shared/lib/formatPhone"; import formatPrice from "@/shared/lib/formatPrice"; import { Button } from "@/shared/ui/button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/shared/ui/select"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/shared/ui/table"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { Eye, Loader2, Trash } from "lucide-react"; import type { Dispatch, SetStateAction } from "react"; import { toast } from "sonner"; interface Props { orders: Order[] | []; isLoading: boolean; isFetching: boolean; isError: boolean; setDetailOpen: Dispatch>; handleDelete: (order: Order) => void; setOrderDetail: Dispatch>; } const deliveryTypeLabel: Record = { YANDEX_GO: "Yandex Go", DELIVERY_COURIES: "Kuryer orqali yetkazish", PICKUP: "O‘zi olib ketish", }; type OrderStatus = | "NEW" // "PROCESSING" | | "DONE"; // | "CANCELLED"; const orderStatusLabel: Record = { NEW: "Yangi", // PROCESSING: "Jarayonda", DONE: "Yakunlangan", // CANCELLED: "Bekor qilingan", }; const OrderTable = ({ orders, isLoading, isFetching, isError, setDetailOpen, handleDelete, setOrderDetail, }: Props) => { const queryClient = useQueryClient(); const { mutate } = useMutation({ mutationFn: ({ body, id, }: { body: { status: string }; id: number | string; }) => order_api.status_update({ body, id }), onSuccess: () => { queryClient.refetchQueries({ queryKey: ["order_list"] }); }, onError: () => { toast.error("Xatolik yuz berdi status o'zgarmadi", { richColors: true, position: "top-center", }); }, }); const handleStatusChange = async ( orderId: number | string, status: OrderStatus, ) => { mutate({ id: orderId, body: { status: status } }); }; if (isLoading || isFetching) { return (
); } if (isError) { return (
Maʼlumotlarni yuklashda xatolik yuz berdi
); } return (
# Order № Foydalanuvchi Kontakt Toʻlov turi Yetkazib berish Umumiy narx Izoh Holat Harakatlar {orders.map((order, index) => ( {index + 1} {order.order_number} {order.user.first_name} {order.user.last_name} ( {order.user.username}) {formatPhone(order.contact_number)} {order.payment_type === "CASH" ? "Naxt" : "Karta orqali"} {deliveryTypeLabel[order.delivery_type] ?? order.delivery_type} {formatPrice(order.total_price, true)} {order.comment || "-"} ))} {orders.length === 0 && ( Buyurtmalar topilmadi )}
); }; export default OrderTable;