1- import { useState } from "react" ;
1+ import { useEffect , useState } from "react" ;
22
33import { TimeInput } from "@/components/time_input" ;
4+ import { TopicInput } from "@/components/topic_input" ;
45
56interface Time {
67 id : number ;
@@ -16,6 +17,10 @@ interface Topic {
1617 color_hex : number ;
1718}
1819
20+ type TopicSelection =
21+ | { type : "existing" ; id : number }
22+ | { type : "new" ; name : string ; color_hex : number } ;
23+
1924interface Item {
2025 id : number ;
2126 name : string ;
@@ -34,12 +39,26 @@ export function TaskForm({ userId, onTaskCreated }: TaskFormProps) {
3439 const [ taskName , setTaskName ] = useState ( "" ) ;
3540 const [ description , setDescription ] = useState ( "" ) ;
3641 const [ times , setTimes ] = useState < Time [ ] > ( [ ] ) ;
37- //const [existingTopicIds, setExistingTopicIds] = useState<number[]>([]);
38- //const [newTopics, setNewTopics] = useState<{ name: string; color_hex: string }[]>([]);
42+ const [ topics , setTopics ] = useState < TopicSelection [ ] > ( [ ] ) ;
43+ const [ availableTopics , setAvailableTopics ] = useState < Topic [ ] > ( [ ] ) ;
44+
45+ useEffect ( ( ) => {
46+ fetch ( "http://localhost:8000/api/planner/topic/" )
47+ . then ( ( res ) => res . json ( ) )
48+ . then ( ( data ) => setAvailableTopics ( data ) )
49+ . catch ( ( err ) => console . error ( "Failed to load topics:" , err ) ) ;
50+ } , [ ] ) ;
3951
4052 const handleSubmit = async ( e : React . FormEvent ) => {
4153 e . preventDefault ( ) ;
4254 try {
55+ const existing_topic_ids = topics
56+ . filter ( ( t ) => t . type === "existing" )
57+ . map ( ( t ) => t . id ) ;
58+
59+ const new_topics = topics
60+ . filter ( ( t ) => t . type === "new" )
61+ . map ( ( t ) => ( { name : t . name , color_hex : t . color_hex } ) ) ;
4362 const response = await fetch ( "http://localhost:8000/api/planner/tasks/" , {
4463 method : "POST" ,
4564 headers : {
@@ -51,8 +70,8 @@ export function TaskForm({ userId, onTaskCreated }: TaskFormProps) {
5170 completed : false ,
5271 user_id : userId ,
5372 times : times ,
54- // existing_topic_ids: existingTopicIds ,
55- // new_topics: newTopics ,
73+ existing_topic_ids : existing_topic_ids ,
74+ new_topics : new_topics ,
5675 } ) ,
5776 } ) ;
5877
@@ -64,6 +83,8 @@ export function TaskForm({ userId, onTaskCreated }: TaskFormProps) {
6483 onTaskCreated ( newTask ) ;
6584 setTaskName ( "" ) ;
6685 setDescription ( "" ) ;
86+ setTimes ( [ ] ) ;
87+ setTopics ( [ ] ) ;
6788 } catch ( error ) {
6889 console . error ( "Error creating task:" , error ) ;
6990 }
@@ -89,6 +110,12 @@ export function TaskForm({ userId, onTaskCreated }: TaskFormProps) {
89110 className = "h-32 w-full resize-none rounded border-2 bg-zinc-700 p-2 text-zinc-200"
90111 />
91112 < TimeInput times = { times } setTimes = { setTimes } />
113+ < TopicInput
114+ availableTopics = { availableTopics }
115+ topics = { topics }
116+ setTopics = { setTopics }
117+ />
118+
92119 < button
93120 type = "submit"
94121 className = "rounded border-2 border-zinc-200 bg-blue-600 px-4 py-2 text-zinc-200 hover:bg-blue-700"
0 commit comments