'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[] } type ForumFields = { name: string description: string icon: string colortitle: string type: string order: string } const EMPTY_FORUM: ForumFields = { name: '', description: '', icon: '', colortitle: '', type: '0', order: '0' } export function AdminForumManager({ initial }: { initial: Group[] }) { const t = useTranslations('Admin') const router = useRouter() const [busy, setBusy] = useState(false) const [catForm, setCatForm] = useState({ name: '', order: '0' }) const [forumForm, setForumForm] = useState>({}) const [editCat, setEditCat] = useState<{ id: number; name: string; order: string } | null>(null) const [editForum, setEditForum] = useState<(ForumFields & { id: number }) | null>(null) const ff = (catId: number) => forumForm[catId] ?? EMPTY_FORUM const setFF = (catId: number, patch: Partial) => 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')) else if (d.error === 'forumNotEmpty') alert(t('forumNotEmpty')) return false } finally { setBusy(false) } } const forumBody = (v: ForumFields, category: number) => ({ category, name: v.name, description: v.description, icon: v.icon, colortitle: v.colortitle, type: Number(v.type), order: Number(v.order), }) function ForumEditFields({ v, onChange, }: { v: ForumFields onChange: (patch: Partial) => void }) { return ( <> onChange({ name: e.target.value })} placeholder={t('forumName')} maxLength={45} /> onChange({ description: e.target.value })} placeholder={t('forumDescription')} /> onChange({ icon: e.target.value })} placeholder={t('forumIcon')} /> onChange({ colortitle: e.target.value })} placeholder={t('forumColor')} /> onChange({ order: e.target.value })} placeholder={t('order')} /> ) } return (
{/* Alta de categoría */}
{ e.preventDefault() if (catForm.name.trim()) call('/api/admin/forum/category', 'POST', { name: catForm.name, order: Number(catForm.order) }).then( (ok) => ok && setCatForm({ name: '', order: '0' }), ) }} className="admin-form info-box-light separate2" > {t('newCategory')} setCatForm({ ...catForm, name: e.target.value })} placeholder={t('categoryName')} maxLength={45} /> {t('order')} setCatForm({ ...catForm, order: e.target.value })} />

{initial.length === 0 &&

{t('noCategories')}

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

{category.name} #{category.order}{' '} {' '}

)} {forums.length === 0 && ( )} {forums.map((f) => editForum?.id === f.id ? ( ) : ( ), )}
{t('noForums')}
setEditForum({ ...editForum, ...patch })} /> {' '}
{f.name}{' '} · {t('forumType')}: {f.type} · #{f.order} {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', forumBody(v, category.id)).then( (ok) => ok && setFF(category.id, EMPTY_FORUM), ) }} className="admin-form separate" > setFF(category.id, patch)} />
))}
) }