'use client'; import { useState, useEffect } from 'react'; import { useRouter, useParams } from 'next/navigation'; import Link from 'next/link'; import { boardApi, BoardCategory, BoardPost } from '@/lib/api'; import { useAuthStore } from '@/lib/store'; import { useTranslate } from '@/lib/useTranslate'; export default function BoardEditPage() { 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 [categories, setCategories] = useState([]); const [loading, setLoading] = useState(true); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); const [title, setTitle] = useState(''); const [content, setContent] = useState(''); const [categoryId, setCategoryId] = useState(''); const [isNotice, setIsNotice] = useState(false); const [isPinned, setIsPinned] = useState(false); useEffect(() => { if (!isLoggedIn) { router.push('/login?redirect=/board'); return; } const fetchData = async () => { try { const [postData, categoriesRes] = await Promise.all([ boardApi.getPost(postId), boardApi.getCategories(), ]); // Check permission if (postData.author_id !== user?.id && !user?.is_admin) { router.push('/board'); return; } setPost(postData); setCategories(categoriesRes.categories); setTitle(postData.title); setContent(postData.content); setCategoryId(postData.category_id); setIsNotice(postData.is_notice); setIsPinned(postData.is_pinned); } catch (err: any) { setError(err.response?.data?.detail || 'Failed to load post'); } finally { setLoading(false); } }; fetchData(); }, [isLoggedIn, postId, user, router]); const getCategoryName = (cat: BoardCategory) => { if (language === 'en' && cat.name_en) return cat.name_en; if (language === 'mn' && cat.name_mn) return cat.name_mn; if (language === 'ru' && cat.name_ru) return cat.name_ru; return cat.name; }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!title.trim()) { setError(translate('Please enter a title')); return; } if (!content.trim()) { setError(translate('Please enter content')); return; } if (!categoryId) { setError(translate('Please select a category')); return; } setSubmitting(true); setError(null); try { await boardApi.updatePost(postId, { title: title.trim(), content: content.trim(), category_id: categoryId as number, is_notice: user?.is_admin ? isNotice : undefined, is_pinned: user?.is_admin ? isPinned : undefined, }); router.push(`/board/${postId}`); } catch (err: any) { setError(err.response?.data?.detail || 'Failed to update post'); } finally { setSubmitting(false); } }; if (!isLoggedIn || loading) { return (
); } if (error && !post) { return (

{error}

{translate('Back to list')}
); } return (
{/* Back Button */}
{translate('Cancel')}
{/* Form */}

{translate('Edit Post')}

{error && (
{error}
)} {/* Category */}
{/* Title */}
setTitle(e.target.value)} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500" placeholder={translate('Enter title')} maxLength={200} required />
{/* Content */}