import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { format } from "date-fns"; import { Edit, Eye, FileText, MoreHorizontal, PlusCircle, Search, Trash2, } from "lucide-react"; import { useState } from "react"; import { Link } from "wouter"; import { DashboardLayout } from "@/components/layout/DashboardLayout"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Input } from "@/components/ui/input"; import { Skeleton } from "@/components/ui/skeleton"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { useAuth } from "@/hooks/use-auth"; import { useToast } from "@/hooks/use-toast"; import { apiRequest } from "@/lib/queryClient"; import type { BlogPostListItem } from "@/lib/types"; 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
); }