import React, { useState, useEffect } from 'react'; import { Link } from 'react-router-dom'; import { useAuth } from '../contexts/AuthContext'; import { forumAPI } from '../services/api'; import { ForumPost } from '../types'; import CategoryBadge from '../components/CategoryBadge'; import PostStatusBadge from '../components/PostStatusBadge'; const MyPosts: React.FC = () => { const { user } = useAuth(); const [posts, setPosts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [actionLoading, setActionLoading] = useState(null); useEffect(() => { if (user) { fetchMyPosts(); } }, [user]); const fetchMyPosts = async () => { try { setLoading(true); const response = await forumAPI.getMyPosts(); setPosts(response.data); } catch (err: any) { setError(err.response?.data?.error || 'Failed to fetch your posts'); } finally { setLoading(false); } }; const handleStatusChange = async (postId: string, newStatus: string) => { try { setActionLoading(postId); await forumAPI.updatePostStatus(postId, newStatus); await fetchMyPosts(); // Refresh list } catch (err: any) { alert(err.response?.data?.error || 'Failed to update status'); } finally { setActionLoading(null); } }; const handleDelete = async (postId: string, postTitle: string) => { if (!window.confirm(`Are you sure you want to delete "${postTitle}"? This action cannot be undone.`)) { return; } try { setActionLoading(postId); await forumAPI.deletePost(postId); await fetchMyPosts(); // Refresh list } catch (err: any) { alert(err.response?.data?.error || 'Failed to delete post'); setActionLoading(null); } }; const formatDate = (dateString: string) => { const date = new Date(dateString); const now = new Date(); const diffMs = now.getTime() - date.getTime(); const diffHours = Math.floor(diffMs / (1000 * 60 * 60)); const diffDays = Math.floor(diffHours / 24); if (diffHours < 1) { return 'Just now'; } else if (diffHours < 24) { return `${diffHours}h ago`; } else if (diffDays < 7) { return `${diffDays}d ago`; } else { return date.toLocaleDateString(); } }; if (!user) { return (
You must be logged in to view your posts.
Back to Forum
); } if (loading) { return (
Loading...
); } return (

My Posts

Manage your forum posts and discussions

Create Post
{error && (
{error}
)} {posts.length === 0 ? (

No posts yet

Start a conversation by creating your first post!

Create Your First Post
) : ( <>

You have {posts.length} post{posts.length !== 1 ? 's' : ''}

{posts.map((post) => (
{post.isPinned && ( )}
{post.title}
{post.tags && post.tags.length > 0 && (
{post.tags.slice(0, 3).map((tag) => ( #{tag.tagName} ))} {post.tags.length > 3 && ( +{post.tags.length - 3} )}
)}
{post.commentCount || 0} comments {post.viewCount || 0} views {formatDate(post.updatedAt)}
View {post.status === 'open' && ( )} {post.status !== 'closed' && ( )} {post.status === 'closed' && ( )}
))}
)}
Back to Forum
); }; export default MyPosts;