'use client' import { useState } from 'react' import { useTranslations } from 'next-intl' import { useRouter } from '@/i18n/navigation' import type { AdminCategory, AdminForum } from '@/lib/admin-forum' type Group = { category: AdminCategory; forums: AdminForum[] } export function AdminForumManager({ initial }: { initial: Group[] }) { const t = useTranslations('Admin') const router = useRouter() const [busy, setBusy] = useState(false) const [catForm, setCatForm] = useState({ name: '', nameEn: '', order: '0' }) const [forumForm, setForumForm] = useState>({}) const [editCat, setEditCat] = useState<{ id: number; name: string; nameEn: string; order: string } | null>(null) const [editForum, setEditForum] = useState<{ id: number; name: string; nameEn: string; description: string; descriptionEn: string; order: string; visibility: number } | null>(null) function ff(catId: number) { return forumForm[catId] ?? { name: '', nameEn: '', description: '', descriptionEn: '', order: '0' } } function setFF(catId: number, patch: Partial<{ name: string; nameEn: string; description: string; descriptionEn: string; order: string }>) { setForumForm((s) => ({ ...s, [catId]: { ...ff(catId), ...patch } })) } async function call(url: string, method: string, body?: unknown): Promise { setBusy(true) try { const res = await fetch(url, { method, headers: body ? { 'Content-Type': 'application/json' } : undefined, credentials: 'same-origin', body: body ? JSON.stringify(body) : undefined, }) const d = await res.json() if (d.success) { router.refresh() return true } if (d.error === 'categoryNotEmpty') alert(t('categoryNotEmpty')) return false } finally { setBusy(false) } } return (
{/* Alta de categoría */}
{ e.preventDefault() if (catForm.name.trim()) call('/api/admin/forum/category', 'POST', { name: catForm.name, nameEn: catForm.nameEn, order: Number(catForm.order) }).then( (ok) => ok && setCatForm({ name: '', nameEn: '', order: '0' }), ) }} className="admin-form info-box-light separate2" > {t('newCategory')} setCatForm({ ...catForm, name: e.target.value })} placeholder={t('categoryName')} /> setCatForm({ ...catForm, nameEn: e.target.value })} placeholder={t('categoryNameEn')} /> {t('order')} setCatForm({ ...catForm, order: e.target.value })} />

{t('forumEnHint')}


{initial.length === 0 &&

{t('noCategories')}

} {initial.map(({ category, forums }) => (
{editCat?.id === category.id ? (
setEditCat({ ...editCat, name: e.target.value })} placeholder={t('categoryName')} /> setEditCat({ ...editCat, nameEn: e.target.value })} placeholder={t('categoryNameEn')} /> setEditCat({ ...editCat, order: e.target.value })} /> {' '}
) : (

{category.name} {category.name_en && · EN: {category.name_en}}{' '} #{category.order}{' '} {' '}

)} {/* Foros de la categoría */} {forums.length === 0 && ( )} {forums.map((f) => editForum?.id === f.id ? ( ) : ( ), )}
{t('noForums')}
setEditForum({ ...editForum, name: e.target.value })} placeholder={t('forumName')} maxLength={45} /> setEditForum({ ...editForum, nameEn: e.target.value })} placeholder={t('forumNameEn')} maxLength={45} /> setEditForum({ ...editForum, description: e.target.value })} placeholder={t('forumDescription')} /> setEditForum({ ...editForum, descriptionEn: e.target.value })} placeholder={t('forumDescriptionEn')} /> {' '}
{f.name}{' '} {f.name_en && · EN: {f.name_en}}{' '} {f.visibility === 0 && ({t('hidden')})} {f.description &&

{f.description}

}
{' '} {' '}
{/* Alta de foro en esta categoría */}
{ e.preventDefault() const v = ff(category.id) if (v.name.trim()) call('/api/admin/forum', 'POST', { categoryId: category.id, name: v.name, nameEn: v.nameEn, description: v.description, descriptionEn: v.descriptionEn, order: Number(v.order), }).then((ok) => ok && setFF(category.id, { name: '', nameEn: '', description: '', descriptionEn: '', order: '0' })) }} className="admin-form separate" > setFF(category.id, { name: e.target.value })} placeholder={t('forumName')} maxLength={45} /> setFF(category.id, { nameEn: e.target.value })} placeholder={t('forumNameEn')} maxLength={45} /> setFF(category.id, { description: e.target.value })} placeholder={t('forumDescription')} /> setFF(category.id, { descriptionEn: e.target.value })} placeholder={t('forumDescriptionEn')} />
))}
) }