import { user_api } from "@/features/users/lib/api"; import type { UserListData, UserListRes } from "@/features/users/lib/data"; import { userStore } from "@/shared/hooks/user"; import { Button } from "@/shared/ui/button"; import { Checkbox } from "@/shared/ui/checkbox"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/shared/ui/table"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import type { AxiosError, AxiosResponse } from "axios"; import clsx from "clsx"; import { Edit, Loader2, Trash } from "lucide-react"; import { useState, type Dispatch, type SetStateAction } from "react"; import { toast } from "sonner"; interface Props { data: AxiosResponse | undefined; isLoading: boolean; isError: boolean; setDialogOpen: Dispatch>; setEditingUser: Dispatch>; setUserList: Dispatch>; handleDelete: (user: UserListData) => void; currentPage: number; userList: number[] | null; sendMessage: boolean; } const UserTable = ({ data, isLoading, isError, setDialogOpen, handleDelete, userList, setEditingUser, setUserList, sendMessage, currentPage, }: Props) => { const queryClient = useQueryClient(); const { user: getme } = userStore(); const [pendingUserId, setPendingUserId] = useState(null); // TableHeader checkbox holati const allSelected = data?.data.data.results.length ? userList?.length === data.data.data.results.length : false; const { mutate: active } = useMutation({ mutationFn: (id: number) => user_api.active(id), onMutate: (id) => setPendingUserId(id), onSuccess: () => { queryClient.refetchQueries({ queryKey: ["user_list"] }); toast.success(`Foydalanuvchi aktivlashdi`); setPendingUserId(null); }, onError: (err: AxiosError) => { const errMessage = err.response?.data as { message: string }; setPendingUserId(null); toast.error(errMessage?.message || "Xatolik yuz berdi", { richColors: true, position: "top-center", }); }, }); const handleActivate = (userId: number) => active(userId); // TableHeader checkbox toggle const handleSelectAll = () => { if (!data) return; if (allSelected) { setUserList([]); setUserList([]); } else { const allIds = data.data.data.results.map((u) => u.id); setUserList(allIds); setUserList(allIds); } }; // Individual checkbox toggle const handleSelect = (id: number) => { let updated: number[] = []; if (userList) { if (userList?.includes(id)) { updated = userList.filter((i) => i !== id); } else { updated = [...userList, id]; } setUserList(updated); setUserList(updated.length ? updated : []); } }; return (
{isLoading && (
)} {isError && (
Ma'lumotlarni olishda xatolik yuz berdi.
)} {!isLoading && !isError && ( {sendMessage && ( )} ID Ismi Familiyasi Hududi Holati Harakatlar {data && data.data.data.results.length > 0 ? ( data.data.data.results.map((user, index) => ( {sendMessage && ( handleSelect(user.id)} /> )} {index + 1 + (currentPage - 1) * 20} {user.first_name} {user.last_name} {user.region.name} {getme?.is_superuser && ( )} )) ) : ( Foydalanuvchilar topilmadi. )}
)}
); }; export default UserTable;