8a3ba8ff40
- lib/forum.ts: getTopic/getPosts/countPosts aceptan includeDeleted (mods ven lo borrado); Post.deleted y TopicFull.deleted. - API: PUT /api/forum/post restaura post (mod-only); moderate action 'restore' restaura tema (usa includeDeleted al leer el tema). - UI: posts borrados atenuados con badge y botón Restaurar (PostActions); TopicModBar muestra banner + Restaurar tema cuando el tema está borrado. - i18n es/en: Forum.restore, restoreTopic, deletedMark. Verificado: build OK, PUT post y moderate restore 401 sin sesión, /forum 200. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
134 lines
3.8 KiB
TypeScript
134 lines
3.8 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import { useTranslations } from 'next-intl'
|
|
import { useRouter } from '@/i18n/navigation'
|
|
import { RichTextArea } from './RichTextArea'
|
|
|
|
export function PostActions({
|
|
postId,
|
|
initialText,
|
|
deleted = false,
|
|
}: {
|
|
postId: number
|
|
initialText: string
|
|
deleted?: boolean
|
|
}) {
|
|
const t = useTranslations('Forum')
|
|
const router = useRouter()
|
|
const [editing, setEditing] = useState(false)
|
|
const [text, setText] = useState(initialText)
|
|
const [busy, setBusy] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
async function save() {
|
|
if (busy || !text.trim()) return
|
|
setBusy(true)
|
|
setError(null)
|
|
try {
|
|
const res = await fetch('/api/forum/post', {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify({ postId, text }),
|
|
})
|
|
const data: { success?: boolean; error?: string } = await res.json()
|
|
if (data.success) {
|
|
setEditing(false)
|
|
router.refresh()
|
|
} else {
|
|
setError(t(data.error === 'tooShort' ? 'tooShort' : data.error === 'topicLocked' ? 'topicLocked' : 'genericError'))
|
|
}
|
|
} catch {
|
|
setError(t('genericError'))
|
|
} finally {
|
|
setBusy(false)
|
|
}
|
|
}
|
|
|
|
async function restore() {
|
|
if (busy) return
|
|
setBusy(true)
|
|
setError(null)
|
|
try {
|
|
const res = await fetch('/api/forum/post', {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify({ postId }),
|
|
})
|
|
if ((await res.json()).success) router.refresh()
|
|
else setError(t('genericError'))
|
|
} catch {
|
|
setError(t('genericError'))
|
|
} finally {
|
|
setBusy(false)
|
|
}
|
|
}
|
|
|
|
async function remove() {
|
|
if (busy || !confirm(t('confirmDeletePost'))) return
|
|
setBusy(true)
|
|
setError(null)
|
|
try {
|
|
const res = await fetch('/api/forum/post', {
|
|
method: 'DELETE',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify({ postId }),
|
|
})
|
|
const data: { success?: boolean } = await res.json()
|
|
if (data.success) router.refresh()
|
|
else setError(t('genericError'))
|
|
} catch {
|
|
setError(t('genericError'))
|
|
} finally {
|
|
setBusy(false)
|
|
}
|
|
}
|
|
|
|
if (editing) {
|
|
return (
|
|
<div className="mt-3 space-y-2">
|
|
<RichTextArea value={text} onChange={setText} rows={4} />
|
|
<div className="flex gap-2">
|
|
<button type="button" onClick={save} disabled={busy} className="nw-btn text-sm disabled:opacity-60">
|
|
{busy ? t('sending') : t('save')}
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => { setEditing(false); setText(initialText); setError(null) }}
|
|
className="nw-btn-ghost text-sm"
|
|
>
|
|
{t('cancel')}
|
|
</button>
|
|
</div>
|
|
{error && <p className="text-sm text-red-400">{error}</p>}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
if (deleted) {
|
|
return (
|
|
<div className="mt-2 flex items-center gap-3 text-xs">
|
|
<button type="button" onClick={restore} disabled={busy} className="text-green-400 hover:text-green-300 disabled:opacity-60">
|
|
{t('restore')}
|
|
</button>
|
|
{error && <span className="text-red-400">{error}</span>}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<div className="mt-2 flex items-center gap-3 text-xs">
|
|
<button type="button" onClick={() => setEditing(true)} className="text-nw-muted hover:text-nw-gold-light">
|
|
{t('edit')}
|
|
</button>
|
|
<button type="button" onClick={remove} disabled={busy} className="text-nw-muted hover:text-red-400 disabled:opacity-60">
|
|
{t('delete')}
|
|
</button>
|
|
{error && <span className="text-red-400">{error}</span>}
|
|
</div>
|
|
)
|
|
}
|