import { useEffect, useState } from 'react'
import { Button, Badge, Card, Col, Container, Form, Modal, Row } from 'react-bootstrap'
import { Link, useParams } from 'react-router-dom'
import { createThread, getForum, listForumsByParent, listThreadsByForum } from '../api/client'
import { useAuth } from '../context/AuthContext'
import { useTranslation } from 'react-i18next'
export default function ForumView() {
const { id } = useParams()
const { token } = useAuth()
const [forum, setForum] = useState(null)
const [children, setChildren] = useState([])
const [threads, setThreads] = useState([])
const [error, setError] = useState('')
const [loading, setLoading] = useState(true)
const [showModal, setShowModal] = useState(false)
const [title, setTitle] = useState('')
const [body, setBody] = useState('')
const [saving, setSaving] = useState(false)
const { t } = useTranslation()
const renderChildRows = (nodes) =>
nodes.map((node) => (
{node.name}
{node.description || t('forum.no_description')}
—
—
{t('thread.no_replies')}
))
useEffect(() => {
let active = true
const loadData = async () => {
setLoading(true)
setError('')
try {
const forumData = await getForum(id)
if (!active) return
setForum(forumData)
const childData = await listForumsByParent(id)
if (!active) return
setChildren(childData)
if (forumData.type === 'forum') {
const threadData = await listThreadsByForum(id)
if (!active) return
setThreads(threadData)
} else {
setThreads([])
}
} catch (err) {
if (active) setError(err.message)
} finally {
if (active) setLoading(false)
}
}
loadData()
return () => {
active = false
}
}, [id])
const handleSubmit = async (event) => {
event.preventDefault()
setSaving(true)
setError('')
try {
await createThread({ title, body, forumId: id })
setTitle('')
setBody('')
const updated = await listThreadsByForum(id)
setThreads(updated)
setShowModal(false)
} catch (err) {
setError(err.message)
} finally {
setSaving(false)
}
}
return (
{loading && {t('forum.loading')}
}
{error && {error}
}
{forum && (
<>
{forum.type !== 'forum' && (
{children.length > 0 ? (
renderChildRows(children)
) : (
{t('forum.empty_children')}
)}
)}
{forum.type === 'forum' && (
<>
{threads.length} {t('forum.threads').toLowerCase()}
{!token && {t('forum.login_hint')}
}
{t('forum.threads')}
{t('thread.replies')}
{t('thread.views')}
{t('thread.last_post')}
{threads.length === 0 && (
{t('forum.empty_threads')}
)}
{threads.map((thread) => (
{thread.title}
{t('thread.by')}
{thread.user_name || t('thread.anonymous')}
{thread.created_at && (
{thread.created_at.slice(0, 10)}
)}
0
—
{t('thread.by')}{' '}
{thread.user_name || t('thread.anonymous')}
{thread.created_at && (
{thread.created_at.slice(0, 10)}
)}
))}
>
)}
>
)}
{forum?.type === 'forum' && (
setShowModal(false)} centered size="lg">
{t('forum.start_thread')}
{!token && {t('forum.login_hint')}
}
{t('form.title')}
setTitle(event.target.value)}
disabled={!token || saving}
required
/>
{t('form.body')}
setBody(event.target.value)}
disabled={!token || saving}
required
/>
)}
)
}