"use client"; import { deleteNews, getAllNews, updateNews } from "@/pages/news/lib/api"; import { Badge } from "@/shared/ui/badge"; import { Button } from "@/shared/ui/button"; import { Card } from "@/shared/ui/card"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "@/shared/ui/dialog"; import { Switch } from "@/shared/ui/switch"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import clsx from "clsx"; import { ChevronLeft, ChevronRight, Edit, Eye, EyeOff, FolderOpen, Loader2, PlusCircle, Trash2, } from "lucide-react"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import { useNavigate } from "react-router-dom"; import { toast } from "sonner"; const News = () => { const [currentPage, setCurrentPage] = useState(1); const { t } = useTranslation(); const [deleteId, setDeleteId] = useState(null); const queryClient = useQueryClient(); const navigate = useNavigate(); const { data: allNews, isLoading, isError, } = useQuery({ queryKey: ["all_news", currentPage], queryFn: () => getAllNews({ page: currentPage, page_size: 10 }), }); const { mutate: deleteMutate, isPending } = useMutation({ mutationFn: (id: number) => deleteNews(id), onSuccess: () => { setDeleteId(null); queryClient.refetchQueries({ queryKey: ["all_news"] }); toast.success(t("Yangilik o'chirildi"), { richColors: true, position: "top-center", }); }, onError: () => { toast.error(t("Xatolik yuz berdi"), { richColors: true, position: "top-center", }); }, }); const { mutate: togglePublicMutate } = useMutation({ mutationFn: ({ id, body }: { id: number; body: FormData }) => updateNews({ body, id }), onSuccess: () => { queryClient.refetchQueries({ queryKey: ["all_news"] }); toast.success(t("Status o'zgartirildi"), { richColors: true, position: "top-center", }); }, onError: () => { toast.error(t("Xatolik yuz berdi"), { richColors: true, position: "top-center", }); }, }); const confirmDelete = () => { if (deleteId) { deleteMutate(deleteId); } }; const handleTogglePublic = (id: number, currentStatus: boolean) => { const formData = new FormData(); console.log(currentStatus); formData.append("is_public", String(currentStatus)); togglePublicMutate({ id, body: formData, }); }; if (isLoading) { return (

{t("Yuklanmoqda...")}

); } if (isError) { return (
); } return (
{/* Header */}

{t("Yangiliklar")}

{t("Jami")} {allNews?.data.data.total_items}{" "} {t("ta yangilik mavjud")}

{/* News Grid */}
{allNews?.data.data.total_items === 0 ? (

{t("Hozircha yangilik yo'q")}

{t("Birinchi yangilikni qo'shishni boshlang")}

) : ( allNews?.data.data.results.map((item) => ( {/* Image */}
{item.title} { e.currentTarget.src = "https://images.unsplash.com/photo-1507525428034-b723cf961d3e"; }} /> {/* Category Badge */} {item.category && ( {item.category.name} )}
{/* Content */}
{/* Title */}

{item.title}

{/* Short Text */}

{item.text}

{/* Slug */} {item.tag && item.tag.length > 0 && (
{item.tag.map((e, idx) => ( /{e.name} ))}
)} {/* Spacer to push content to bottom */}
{/* Public/Private Toggle */}
{item.is_public ? ( ) : ( )} {t("Оmmaviy")}
handleTogglePublic(item.id, !item.is_public) } className="data-[state=checked]:bg-green-600" />
{/* Actions - at the very bottom */}
)) )}
setDeleteId(null)}> {t("Yangilikni o'chirishni tasdiqlang")}

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

{/* Pagination */}
{[...Array(allNews?.data.data.total_pages)].map((_, i) => ( ))}
); }; export default News;