"use client"; import { addedPopularTours, deleteTours, getAllTours, } from "@/pages/tours/lib/api"; import formatPrice from "@/shared/lib/formatPrice"; import { Button } from "@/shared/ui/button"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "@/shared/ui/dialog"; import { Switch } from "@/shared/ui/switch"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/shared/ui/table"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { AlertTriangle, ChevronLeft, ChevronRight, Edit, Loader2, Plane, PlusCircle, Trash2, X, } from "lucide-react"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import { useNavigate } from "react-router-dom"; import { toast } from "sonner"; type Role = | "superuser" | "admin" | "moderator" | "tour_admin" | "buxgalter" | "operator" | "user"; const Tours = ({ user }: { user: Role }) => { const { t } = useTranslation(); const [page, setPage] = useState(1); const [deleteId, setDeleteId] = useState(null); const [showPopularDialog, setShowPopularDialog] = useState(false); const navigate = useNavigate(); const queryClient = useQueryClient(); const { data, isLoading, isError, refetch } = useQuery({ queryKey: ["all_tours", page], queryFn: () => getAllTours({ page: page, page_size: 10 }), }); const { data: popularTour } = useQuery({ queryKey: ["popular_tours"], queryFn: () => getAllTours({ page: 1, page_size: 10, featured_tickets: true }), }); const { mutate, isPending } = useMutation({ mutationFn: (id: number) => deleteTours({ id }), onSuccess: () => { queryClient.refetchQueries({ queryKey: ["all_tours"] }); setDeleteId(null); }, onError: () => { toast.error(t("Xatolik yuz berdi"), { richColors: true, position: "top-center", }); }, }); const { mutate: popular } = useMutation({ mutationFn: ({ id, value }: { id: number; value: number }) => addedPopularTours({ id, value }), onSuccess: () => { queryClient.refetchQueries({ queryKey: ["all_tours"] }); queryClient.refetchQueries({ queryKey: ["popular_tours"] }); }, onError: () => { if (popularTour?.data.data.results.length === 5) { setShowPopularDialog(true); } else { toast.error(t("Xatolik yuz berdi"), { richColors: true, position: "top-center", }); } }, }); const confirmDelete = (id: number) => { mutate(id); }; const removeFromPopular = (id: number) => { popular({ id, value: 0 }); setShowPopularDialog(false); }; if (isLoading) { return (

{t("Ma'lumotlar yuklanmoqda...")}

); } if (isError) { return (

{t("Ma'lumotlarni yuklashda xatolik yuz berdi.")}

); } return (

{t("Turlar ro'yxati")}

# {t("Manzil")} {t("Davomiyligi")} {t("Mehmonxona")} {t("Narxi")} {user && user === "moderator" && ( {t("Popular")} )} {t("Операции")} {data?.data.data.results.map((tour, idx) => ( {(page - 1) * 6 + idx + 1}
{tour.destination}
{tour.duration_days} {t("kun")}
{tour.hotel_name} {tour.hotel_rating} {t("yulduzli mehmonxona")}
{formatPrice(tour.price, true)} {user && user === "moderator" && ( popular({ id: tour.id, value: tour.featured_tickets ? 0 : 1, }) } /> )}
))}
{/* Delete Tour Dialog */} setDeleteId(null)}> {t("Turni o'chirishni tasdiqlang")}

{t( "Haqiqatan ham bu turni o'chirib tashlamoqchimisiz? Bu amalni ortga qaytarib bo'lmaydi.", )}

{/* Popular Tours Dialog */} {t("Popular turlar (5/5)")}

{t( "Popular turlar ro'yxati to'lgan. Yangi tur qo'shish uchun biror turni o'chiring.", )}

{popularTour?.data.data.results.map((tour) => (

{tour.destination}

{tour.duration_days} kun • {tour.hotel_name}

{formatPrice(tour.price, true)}
))}
{[...Array(data?.data.data.total_pages)].map((_, i) => ( ))}
); }; export default Tours;