import { useState } from "react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { DashboardLayout } from "@/components/layout/DashboardLayout"; import { useAuth } from "@/hooks/useAuth"; import { Link } from "wouter"; import { apiRequest } from "@/lib/queryClient"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Skeleton } from "@/components/ui/skeleton"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { Search, PlusCircle, Edit, Trash2, MoreHorizontal, Eye, FileText } from "lucide-react"; import { Badge } from "@/components/ui/badge"; import { BlogPostListItem } from "@/lib/types"; import { format } from "date-fns"; import { useToast } from "@/hooks/use-toast"; export default function BlogManagement() { const { canManageContent } = useAuth(); const [searchQuery, setSearchQuery] = useState(""); const [postToDelete, setPostToDelete] = useState(null); const queryClient = useQueryClient(); const { toast } = useToast(); // Fetch blog posts const { data: posts, isLoading } = useQuery({ queryKey: ['/api/blog'], }); // Delete mutation const deletePostMutation = useMutation({ mutationFn: async (postId: number) => { return apiRequest(`/api/blog/${postId}`, { method: 'DELETE' }); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['/api/blog'] }); toast({ title: "Post deleted", description: "The blog post has been successfully deleted.", }); setPostToDelete(null); }, onError: (error) => { toast({ title: "Error", description: "Failed to delete the blog post. Please try again.", variant: "destructive", }); console.error("Delete error:", error); } }); // Filter posts by search query const filteredPosts = posts?.filter(post => post.title.toLowerCase().includes(searchQuery.toLowerCase()) || post.excerpt?.toLowerCase().includes(searchQuery.toLowerCase()) ); return (
setSearchQuery(e.target.value)} />
{/* Blog Posts Table */}
Title Status Author Date Actions {isLoading ? ( Array.from({length: 5}).map((_, i) => ( )) ) : filteredPosts && filteredPosts.length > 0 ? ( filteredPosts.map((post) => ( {post.title} {post.publishedAt ? ( Published ) : ( Draft )} {post.author?.displayName || 'Anonymous'} {post.publishedAt ? format(new Date(post.publishedAt), 'MMM d, yyyy') : format(new Date(post.createdAt), 'MMM d, yyyy')} View Edit {canManageContent && ( <> setPostToDelete(post.id)} > Delete )} )) ) : (

No blog posts found

{searchQuery && (

Try adjusting your search query

)}
)}
{/* Delete Confirmation Dialog */} !open && setPostToDelete(null)}> Are you sure? This action cannot be undone. This will permanently delete the blog post and remove it from the platform. Cancel { if (postToDelete) { deletePostMutation.mutate(postToDelete); } }} className="bg-red-600 hover:bg-red-700 focus:ring-red-600" > Delete
); }