import { useEffect, useState } from 'react' import { Button, Card, Col, Container, Form, 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 [title, setTitle] = useState('') const [body, setBody] = useState('') const [saving, setSaving] = useState(false) const { t } = useTranslation() 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) } catch (err) { setError(err.message) } finally { setSaving(false) } } return ( {loading &&

{t('forum.loading')}

} {error &&

{error}

} {forum && ( <>

{forum.type === 'forum' ? t('forum.type_forum') : t('forum.type_category')}

{forum.name}

{forum.description || t('forum.no_description')}

{t('forum.children')}

{children.length === 0 && (

{t('forum.empty_children')}

)} {children.map((child) => ( {child.name} {child.description || t('forum.no_description')} {t('forum.open')} ))} {forum.type === 'forum' && ( <>

{t('forum.threads')}

{threads.length === 0 && (

{t('forum.empty_threads')}

)} {threads.map((thread) => ( {thread.title} {thread.body.length > 160 ? `${thread.body.slice(0, 160)}...` : thread.body} {t('thread.view')} ))} )}

{t('forum.start_thread')}

{forum.type !== 'forum' && (

{t('forum.only_forums')}

)} {forum.type === 'forum' && !token && (

{t('forum.login_hint')}

)}
{t('form.title')} setTitle(event.target.value)} disabled={!token || saving || forum.type !== 'forum'} required /> {t('form.body')} setBody(event.target.value)} disabled={!token || saving || forum.type !== 'forum'} required />
)}
) }