Barazo default frontend barazo.forum

fix(topics): fetch categories from API in new/edit topic pages (#180)

The topic creation and edit pages never passed categories to TopicForm,
causing it to fall back to a hardcoded "General Discussion" entry.
Now both pages fetch the full category list from the API on mount.

authored by

Guido X Jansen and committed by
GitHub
8ab8f7f6 ddb8f7e9

+19 -4
+12 -2
src/app/[handle]/[rkey]/edit/page.tsx
··· 10 10 import { useState, useEffect } from 'react' 11 11 import { useRouter } from 'next/navigation' 12 12 import Link from 'next/link' 13 - import type { CreateTopicInput, PublicSettings, Topic } from '@/lib/api/types' 14 - import { getTopicByAuthorAndRkey, updateTopic, getPublicSettings } from '@/lib/api/client' 13 + import type { CreateTopicInput, CategoryTreeNode, PublicSettings, Topic } from '@/lib/api/types' 14 + import { 15 + getCategories, 16 + getTopicByAuthorAndRkey, 17 + updateTopic, 18 + getPublicSettings, 19 + } from '@/lib/api/client' 15 20 import { getTopicUrl } from '@/lib/format' 16 21 import { useAuth } from '@/hooks/use-auth' 17 22 import { ForumLayout } from '@/components/layout/forum-layout' ··· 32 37 const [submitting, setSubmitting] = useState(false) 33 38 const [error, setError] = useState<string | null>(null) 34 39 const [publicSettings, setPublicSettings] = useState<PublicSettings | null>(null) 40 + const [categories, setCategories] = useState<CategoryTreeNode[]>([]) 35 41 36 42 useEffect(() => { 37 43 getPublicSettings() 38 44 .then((settings) => setPublicSettings(settings)) 45 + .catch(() => {}) 46 + getCategories() 47 + .then((res) => setCategories(res.categories)) 39 48 .catch(() => {}) 40 49 }, []) 41 50 ··· 170 179 onSubmit={handleSubmit} 171 180 submitting={submitting} 172 181 mode="edit" 182 + categories={categories.length > 0 ? categories : undefined} 173 183 initialValues={{ 174 184 title: topic.title, 175 185 content: topic.content,
+7 -2
src/app/new/page.tsx
··· 9 9 10 10 import { useState, useEffect } from 'react' 11 11 import { useRouter, useSearchParams } from 'next/navigation' 12 - import type { CreateTopicInput, PublicSettings } from '@/lib/api/types' 13 - import { ApiError, createTopic, getPublicSettings } from '@/lib/api/client' 12 + import type { CreateTopicInput, CategoryTreeNode, PublicSettings } from '@/lib/api/types' 13 + import { ApiError, createTopic, getCategories, getPublicSettings } from '@/lib/api/client' 14 14 import { getTopicUrl } from '@/lib/format' 15 15 import { ForumLayout } from '@/components/layout/forum-layout' 16 16 import { Breadcrumbs } from '@/components/breadcrumbs' ··· 28 28 const [error, setError] = useState<string | null>(null) 29 29 const [heldMessage, setHeldMessage] = useState<string | null>(null) 30 30 const [publicSettings, setPublicSettings] = useState<PublicSettings | null>(null) 31 + const [categories, setCategories] = useState<CategoryTreeNode[]>([]) 31 32 32 33 useEffect(() => { 33 34 getPublicSettings() 34 35 .then((settings) => setPublicSettings(settings)) 36 + .catch(() => {}) 37 + getCategories() 38 + .then((res) => setCategories(res.categories)) 35 39 .catch(() => {}) 36 40 }, []) 37 41 ··· 93 97 <TopicForm 94 98 onSubmit={handleSubmit} 95 99 submitting={submitting} 100 + categories={categories.length > 0 ? categories : undefined} 96 101 initialValues={{ category: initialCategory }} 97 102 /> 98 103 </div>