Admin del foro: gestionar traducción EN de categorías y foros
Los formularios de crear categoría/foro aceptan nombre/descripción en inglés, y se añade EDICIÓN en línea de categorías (name, name_en, order) y foros (name, name_en, description, description_en). lib/admin-forum: createCategory/createForum con EN + updateCategory/updateForum; rutas PUT en category/[id] y forum/[id]. Claves Admin nuevas (nombre/descripción EN). Verificado: listado con EN + update round-trip; PUT protegido (403 sin admin). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -11,13 +11,15 @@ 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<Record<number, { name: string; description: string; order: string }>>({})
|
||||
const [catForm, setCatForm] = useState({ name: '', nameEn: '', order: '0' })
|
||||
const [forumForm, setForumForm] = useState<Record<number, { name: string; nameEn: string; description: string; descriptionEn: string; order: string }>>({})
|
||||
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: '', description: '', order: '0' }
|
||||
return forumForm[catId] ?? { name: '', nameEn: '', description: '', descriptionEn: '', order: '0' }
|
||||
}
|
||||
function setFF(catId: number, patch: Partial<{ name: string; description: string; order: string }>) {
|
||||
function setFF(catId: number, patch: Partial<{ name: string; nameEn: string; description: string; descriptionEn: string; order: string }>) {
|
||||
setForumForm((s) => ({ ...s, [catId]: { ...ff(catId), ...patch } }))
|
||||
}
|
||||
|
||||
@@ -48,14 +50,19 @@ export function AdminForumManager({ initial }: { initial: Group[] }) {
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
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' }))
|
||||
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"
|
||||
>
|
||||
<span className="second-brown">{t('newCategory')}</span>
|
||||
<input value={catForm.name} onChange={(e) => setCatForm({ ...catForm, name: e.target.value })} placeholder={t('categoryName')} />
|
||||
<input value={catForm.nameEn} onChange={(e) => setCatForm({ ...catForm, nameEn: e.target.value })} placeholder={t('categoryNameEn')} />
|
||||
<span className="second-brown">{t('order')}</span>
|
||||
<input type="number" value={catForm.order} onChange={(e) => setCatForm({ ...catForm, order: e.target.value })} />
|
||||
<p className="third-brown small-font">{t('forumEnHint')}</p>
|
||||
<button type="submit" disabled={busy}>{t('create')}</button>
|
||||
</form>
|
||||
<br />
|
||||
@@ -64,18 +71,46 @@ export function AdminForumManager({ initial }: { initial: Group[] }) {
|
||||
|
||||
{initial.map(({ category, forums }) => (
|
||||
<div key={category.id} className="inline-div" style={{ display: 'block' }}>
|
||||
<div className="title-content-h3">
|
||||
<h3 className="big-font">
|
||||
{category.name} <span className="third-brown small-font">#{category.order}</span>{' '}
|
||||
{editCat?.id === category.id ? (
|
||||
<div className="admin-form separate2">
|
||||
<input value={editCat.name} onChange={(e) => setEditCat({ ...editCat, name: e.target.value })} placeholder={t('categoryName')} />
|
||||
<input value={editCat.nameEn} onChange={(e) => setEditCat({ ...editCat, nameEn: e.target.value })} placeholder={t('categoryNameEn')} />
|
||||
<input type="number" value={editCat.order} onChange={(e) => setEditCat({ ...editCat, order: e.target.value })} />
|
||||
<button
|
||||
onClick={() => confirm(t('confirmDelete')) && call(`/api/admin/forum/category/${category.id}`, 'DELETE')}
|
||||
onClick={() =>
|
||||
editCat.name.trim() &&
|
||||
call(`/api/admin/forum/category/${category.id}`, 'PUT', { name: editCat.name, nameEn: editCat.nameEn, order: Number(editCat.order) }).then((ok) => ok && setEditCat(null))
|
||||
}
|
||||
disabled={busy}
|
||||
className="nw-tool-btn red-info2"
|
||||
className="nw-tool-btn"
|
||||
>
|
||||
{t('delete')}
|
||||
</button>
|
||||
</h3>
|
||||
</div>
|
||||
{t('save')}
|
||||
</button>{' '}
|
||||
<button onClick={() => setEditCat(null)} className="nw-tool-btn">{t('cancel')}</button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="title-content-h3">
|
||||
<h3 className="big-font">
|
||||
{category.name}
|
||||
{category.name_en && <span className="third-brown small-font"> · EN: {category.name_en}</span>}{' '}
|
||||
<span className="third-brown small-font">#{category.order}</span>{' '}
|
||||
<button
|
||||
onClick={() => setEditCat({ id: category.id, name: category.name, nameEn: category.name_en, order: String(category.order) })}
|
||||
disabled={busy}
|
||||
className="nw-tool-btn"
|
||||
>
|
||||
{t('edit')}
|
||||
</button>{' '}
|
||||
<button
|
||||
onClick={() => confirm(t('confirmDelete')) && call(`/api/admin/forum/category/${category.id}`, 'DELETE')}
|
||||
disabled={busy}
|
||||
className="nw-tool-btn red-info2"
|
||||
>
|
||||
{t('delete')}
|
||||
</button>
|
||||
</h3>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Foros de la categoría */}
|
||||
<table className="max-center-table">
|
||||
@@ -85,31 +120,68 @@ export function AdminForumManager({ initial }: { initial: Group[] }) {
|
||||
<td className="second-brown centered">{t('noForums')}</td>
|
||||
</tr>
|
||||
)}
|
||||
{forums.map((f) => (
|
||||
<tr key={f.id} className="team-center-table-tr">
|
||||
<td className="lefted separate">
|
||||
<span className="yellow-info">{f.name}</span>{' '}
|
||||
{f.visibility === 0 && <span className="red-info2 small-font">({t('hidden')})</span>}
|
||||
{f.description && <p className="third-brown small-font">{f.description}</p>}
|
||||
</td>
|
||||
<td className="real-info-box no-wrap-td">
|
||||
<button
|
||||
onClick={() => call(`/api/admin/forum/${f.id}`, 'PATCH', { visibility: f.visibility === 0 })}
|
||||
disabled={busy}
|
||||
className="nw-tool-btn"
|
||||
>
|
||||
{f.visibility === 0 ? t('show') : t('hide')}
|
||||
</button>{' '}
|
||||
<button
|
||||
onClick={() => confirm(t('confirmDelete')) && call(`/api/admin/forum/${f.id}`, 'DELETE')}
|
||||
disabled={busy}
|
||||
className="nw-tool-btn red-info2"
|
||||
>
|
||||
{t('delete')}
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
{forums.map((f) =>
|
||||
editForum?.id === f.id ? (
|
||||
<tr key={f.id} className="team-center-table-tr">
|
||||
<td className="lefted separate" colSpan={2}>
|
||||
<input value={editForum.name} onChange={(e) => setEditForum({ ...editForum, name: e.target.value })} placeholder={t('forumName')} maxLength={45} />
|
||||
<input value={editForum.nameEn} onChange={(e) => setEditForum({ ...editForum, nameEn: e.target.value })} placeholder={t('forumNameEn')} maxLength={45} />
|
||||
<input value={editForum.description} onChange={(e) => setEditForum({ ...editForum, description: e.target.value })} placeholder={t('forumDescription')} />
|
||||
<input value={editForum.descriptionEn} onChange={(e) => setEditForum({ ...editForum, descriptionEn: e.target.value })} placeholder={t('forumDescriptionEn')} />
|
||||
<button
|
||||
onClick={() =>
|
||||
editForum.name.trim() &&
|
||||
call(`/api/admin/forum/${f.id}`, 'PUT', {
|
||||
name: editForum.name,
|
||||
nameEn: editForum.nameEn,
|
||||
description: editForum.description,
|
||||
descriptionEn: editForum.descriptionEn,
|
||||
order: Number(editForum.order),
|
||||
visibility: editForum.visibility !== 0,
|
||||
}).then((ok) => ok && setEditForum(null))
|
||||
}
|
||||
disabled={busy}
|
||||
className="nw-tool-btn"
|
||||
>
|
||||
{t('save')}
|
||||
</button>{' '}
|
||||
<button onClick={() => setEditForum(null)} className="nw-tool-btn">{t('cancel')}</button>
|
||||
</td>
|
||||
</tr>
|
||||
) : (
|
||||
<tr key={f.id} className="team-center-table-tr">
|
||||
<td className="lefted separate">
|
||||
<span className="yellow-info">{f.name}</span>{' '}
|
||||
{f.name_en && <span className="third-brown small-font">· EN: {f.name_en}</span>}{' '}
|
||||
{f.visibility === 0 && <span className="red-info2 small-font">({t('hidden')})</span>}
|
||||
{f.description && <p className="third-brown small-font">{f.description}</p>}
|
||||
</td>
|
||||
<td className="real-info-box no-wrap-td">
|
||||
<button
|
||||
onClick={() => setEditForum({ id: f.id, name: f.name, nameEn: f.name_en, description: f.description, descriptionEn: f.description_en, order: String(f.order), visibility: f.visibility })}
|
||||
disabled={busy}
|
||||
className="nw-tool-btn"
|
||||
>
|
||||
{t('edit')}
|
||||
</button>{' '}
|
||||
<button
|
||||
onClick={() => call(`/api/admin/forum/${f.id}`, 'PATCH', { visibility: f.visibility === 0 })}
|
||||
disabled={busy}
|
||||
className="nw-tool-btn"
|
||||
>
|
||||
{f.visibility === 0 ? t('show') : t('hide')}
|
||||
</button>{' '}
|
||||
<button
|
||||
onClick={() => confirm(t('confirmDelete')) && call(`/api/admin/forum/${f.id}`, 'DELETE')}
|
||||
disabled={busy}
|
||||
className="nw-tool-btn red-info2"
|
||||
>
|
||||
{t('delete')}
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
),
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@@ -119,14 +191,21 @@ export function AdminForumManager({ initial }: { initial: Group[] }) {
|
||||
e.preventDefault()
|
||||
const v = ff(category.id)
|
||||
if (v.name.trim())
|
||||
call('/api/admin/forum', 'POST', { categoryId: category.id, name: v.name, description: v.description, order: Number(v.order) }).then(
|
||||
(ok) => ok && setFF(category.id, { name: '', description: '', order: '0' }),
|
||||
)
|
||||
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"
|
||||
>
|
||||
<input value={ff(category.id).name} onChange={(e) => setFF(category.id, { name: e.target.value })} placeholder={t('forumName')} maxLength={45} />
|
||||
<input value={ff(category.id).nameEn} onChange={(e) => setFF(category.id, { nameEn: e.target.value })} placeholder={t('forumNameEn')} maxLength={45} />
|
||||
<input value={ff(category.id).description} onChange={(e) => setFF(category.id, { description: e.target.value })} placeholder={t('forumDescription')} />
|
||||
<input value={ff(category.id).descriptionEn} onChange={(e) => setFF(category.id, { descriptionEn: e.target.value })} placeholder={t('forumDescriptionEn')} />
|
||||
<button type="submit" disabled={busy}>{t('addForum')}</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user