import { useState } from 'react'; import { useLiveBlogs } from '@/queries/live-blogs'; import { useArticles, useDeleteArticle, useArchiveArticle, usePublishArticle, useUpdateArticle } from '@/queries/articles'; import { useDeleteLiveBlog, useArchiveLiveBlog, usePublishLiveBlog } from '@/queries/live-blogs'; import { Link } from '@tanstack/react-router'; import { Button } from '@/components/ui/button'; import { format } from 'date-fns'; import { mk } from 'date-fns/locale'; import { PushNotificationManager } from '@/components/admin/PushNotificationManager'; export function AdminDashboardComponent() { 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 updateArticleMutation = useUpdateArticle(); const liveBlogs = liveBlogsData?.data || []; const articles = articlesData?.data || []; 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 { 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 handleSetHero = async (articleId: string, isHero: boolean) => { setIsProcessing(true); try { await updateArticleMutation.mutateAsync({ id: articleId, dto: { isHero } }); } catch (error) { console.error('Failed to update hero status:', error); } finally { setIsProcessing(false); } }; const getStatusColor = (status: string) => { switch (status) { case 'published': case 'live': return 'bg-green-500 text-white border-2 border-foreground'; case 'draft': return 'bg-yellow-400 text-black border-2 border-foreground'; case 'archived': case 'ended': return 'bg-gray-400 text-white border-2 border-foreground'; default: return 'bg-blue-500 text-white border-2 border-foreground'; } }; 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 блогови
Активни live блогови
Објавени написи
Закачени live блогови
Вкупни прегледи
Вчитување...
{showArchived ? 'Нема архивирани live блогови' : 'Нема live блогови'}
Вчитување...
{showArchived ? 'Нема архивирани написи' : 'Нема написи'}
{article.excerpt}
)}Facebook Shares
Twitter Shares
WhatsApp Shares
Telegram Shares
Total Shares
{dialogType === 'delete' ? `Дали сте сигурни дека сакате да го избришете "${itemToDelete.title}"?` : `Дали сте сигурни дека сакате да го архивирате "${itemToDelete.title}"?`}