'use client'; import { useState, useEffect } from 'react'; import { useRouter, useParams } from 'next/navigation'; import Link from 'next/link'; import { boardApi, BoardPost } from '@/lib/api'; import { useAuthStore } from '@/lib/store'; import { useTranslate } from '@/lib/useTranslate'; export default function BoardPostPage() { const router = useRouter(); const params = useParams(); const postId = parseInt(params.id as string); const { user, isLoggedIn } = useAuthStore(); const { translate, language } = useTranslate(); const [post, setPost] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [deleting, setDeleting] = useState(false); useEffect(() => { const fetchPost = async () => { try { const data = await boardApi.getPost(postId); setPost(data); } catch (err: any) { setError(err.response?.data?.detail || 'Failed to load post'); } finally { setLoading(false); } }; fetchPost(); }, [postId]); const getCategoryName = (post: BoardPost) => { if (!post.category) return '-'; if (language === 'en' && post.category.name_en) return post.category.name_en; if (language === 'mn' && post.category.name_mn) return post.category.name_mn; if (language === 'ru' && post.category.name_ru) return post.category.name_ru; return post.category.name; }; const canEdit = isLoggedIn && post && (user?.id === post.author_id || user?.is_admin); const handleDelete = async () => { if (!confirm(translate('Are you sure you want to delete this post?'))) return; setDeleting(true); try { await boardApi.deletePost(postId); router.push('/board'); } catch (err: any) { alert(err.response?.data?.detail || 'Failed to delete post'); } finally { setDeleting(false); } }; if (loading) { return (
); } if (error || !post) { return (

{error || 'Post not found'}

{translate('Back to list')}
); } return (
{/* Back Button */}
{translate('Back to list')}
{/* Post */}
{/* Header */}
{/* Notice Badge */} {post.is_notice && ( Notice )} {/* Title */}

{post.title}

{/* Meta */}
{/* Category */} {getCategoryName(post)} {/* Author */} {post.author?.name || post.author?.email || 'Unknown'} {post.author?.is_admin && ( Admin )} {/* Date */} {new Date(post.created_at).toLocaleString()} {/* Views */} {post.view_count} {translate('views')}
{/* Content */}
{post.content}
{/* Actions */} {canEdit && (
{translate('Edit')}
)}
{/* Navigation Buttons */}
{translate('List')}
); }