import { useEffect, useState } from 'react' import { Button, Card, Col, Container, Form, Row } from 'react-bootstrap' import { Link, useParams } from 'react-router-dom' import { createThread, getCategory, listThreadsByCategory } from '../api/client' import { useAuth } from '../context/AuthContext' export default function CategoryView() { const { id } = useParams() const { token } = useAuth() const [category, setCategory] = useState(null) 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) useEffect(() => { setLoading(true) Promise.all([getCategory(id), listThreadsByCategory(id)]) .then(([categoryData, threadData]) => { setCategory(categoryData) setThreads(threadData) }) .catch((err) => setError(err.message)) .finally(() => setLoading(false)) }, [id]) const handleSubmit = async (event) => { event.preventDefault() setSaving(true) setError('') try { await createThread({ title, body, categoryId: id }) setTitle('') setBody('') const updated = await listThreadsByCategory(id) setThreads(updated) } catch (err) { setError(err.message) } finally { setSaving(false) } } return ( {loading &&

Loading category...

} {error &&

{error}

} {category && ( <>

Category

{category.name}

{category.description || 'No description added yet.'}

Threads

{threads.length === 0 && (

No threads here yet. Start one below.

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

Start a thread

{!token && (

Log in to create a new thread.

)}
Title setTitle(event.target.value)} disabled={!token || saving} required /> Body setBody(event.target.value)} disabled={!token || saving} required />
)}
) }