import { useState } from 'react'; import { useLiveBlogs } from '@/queries/live-blogs'; import { useArticles, useDeleteArticle, useArchiveArticle, usePublishArticle } from '@/queries/articles'; import { useDeleteLiveBlog, useArchiveLiveBlog, usePublishLiveBlog } from '@/queries/live-blogs'; import { Link } from '@tanstack/react-router'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { format } from 'date-fns'; import { mk } from 'date-fns/locale'; export function AdminDashboardComponent() { // State for confirmation dialog and filters const [showConfirmDialog, setShowConfirmDialog] = useState(false); const [dialogType, setDialogType] = useState<'delete' | 'archive'>('delete'); const [itemToDelete, setItemToDelete] = useState<{ type: 'article' | 'liveBlog'; id: string; title: string; } | null>(null); const [isProcessing, setIsProcessing] = useState(false); const [showArchived, setShowArchived] = useState(false); const { data: liveBlogsData, isLoading: loadingLiveBlogs } = useLiveBlogs({ limit: 50, status: showArchived ? 'archived' : 'draft,live,ended' }); const { data: articlesData, isLoading: loadingArticles } = useArticles({ limit: 50, status: showArchived ? 'archived' : 'draft,published' }); const deleteArticleMutation = useDeleteArticle(); const deleteLiveBlogMutation = useDeleteLiveBlog(); const archiveArticleMutation = useArchiveArticle(); const archiveLiveBlogMutation = useArchiveLiveBlog(); const publishArticleMutation = usePublishArticle(); const publishLiveBlogMutation = usePublishLiveBlog(); const liveBlogs = liveBlogsData?.data || []; const articles = articlesData?.data || []; // No need to filter items - API already filters based on showArchived state const filteredLiveBlogs = liveBlogs; const filteredArticles = articles; const handleDeleteClick = (type: 'article' | 'liveBlog', id: string, title: string) => { setItemToDelete({ type, id, title }); setDialogType('delete'); setShowConfirmDialog(true); }; const handleArchiveClick = (type: 'article' | 'liveBlog', id: string, title: string) => { setItemToDelete({ type, id, title }); setDialogType('archive'); setShowConfirmDialog(true); }; const handlePublishClick = async (type: 'article' | 'liveBlog', id: string) => { setIsProcessing(true); try { if (type === 'article') { await publishArticleMutation.mutateAsync({ id, status: 'published' }); } else { await publishLiveBlogMutation.mutateAsync({ id, status: 'draft' }); } } catch (error) { console.error('Failed to publish:', error); } finally { setIsProcessing(false); } }; const handleConfirmAction = async () => { if (!itemToDelete) return; setIsProcessing(true); try { if (dialogType === 'delete') { if (itemToDelete.type === 'article') { await deleteArticleMutation.mutateAsync(itemToDelete.id); } else { await deleteLiveBlogMutation.mutateAsync(itemToDelete.id); } } else { // archive if (itemToDelete.type === 'article') { await archiveArticleMutation.mutateAsync(itemToDelete.id); } else { await archiveLiveBlogMutation.mutateAsync(itemToDelete.id); } } setShowConfirmDialog(false); setItemToDelete(null); } catch (error) { console.error(`Failed to ${dialogType}:`, error); } finally { setIsProcessing(false); } }; const handleCancelAction = () => { setShowConfirmDialog(false); setItemToDelete(null); }; const getStatusColor = (status: string) => { switch (status) { case 'published': case 'live': return 'bg-green-100 text-green-800 border-green-200'; case 'draft': return 'bg-yellow-100 text-yellow-800 border-yellow-200'; case 'archived': case 'ended': return 'bg-gray-100 text-gray-800 border-gray-200'; default: return 'bg-blue-100 text-blue-800 border-blue-200'; } }; const getStatusText = (status: string) => { switch (status) { case 'published': return 'Објавено'; case 'draft': return 'Нацрт'; case 'archived': return 'Архивирано'; case 'live': return 'Во живо'; case 'ended': return 'Завршено'; default: return status; } }; return (
Управување со сите написи и live блогови
Вчитување...
{showArchived ? 'Нема архивирани live блогови' : 'Нема live блогови'}
{!showArchived && ( )}Вчитување...
{showArchived ? 'Нема архивирани написи' : 'Нема написи'}
{!showArchived && ( )}{article.excerpt}
)}Активни live блогови
Објавени написи
Закачени live блогови
Вкупно прегледи
{dialogType === 'delete' ? `Дали сте сигурни дека сакате да го избришете "${itemToDelete.title}"?` : `Дали сте сигурни дека сакате да го архивирате "${itemToDelete.title}"?`}