-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_form.tsx
More file actions
132 lines (119 loc) · 3.76 KB
/
Copy pathtask_form.tsx
File metadata and controls
132 lines (119 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { useEffect, useState } from "react";
import { TimeInput } from "@/components/time_input";
import { TopicInput } from "@/components/topic_input";
interface Time {
id: number;
day: number;
start_time: string;
end_time: string;
repeating: boolean;
}
interface Topic {
id: number;
name: string;
color_hex: number;
}
type TopicSelection =
| { type: "existing"; id: number }
| { type: "new"; name: string; color_hex: number };
interface Item {
id: number;
name: string;
completed: boolean;
description: string;
times: Time[];
topics: Topic[];
}
interface TaskFormProps {
userId: number;
onTaskCreated: (task: Item) => void;
}
export function TaskForm({ userId, onTaskCreated }: TaskFormProps) {
const [taskName, setTaskName] = useState("");
const [description, setDescription] = useState("");
const [times, setTimes] = useState<Time[]>([]);
const [topics, setTopics] = useState<TopicSelection[]>([]);
const [availableTopics, setAvailableTopics] = useState<Topic[]>([]);
useEffect(() => {
fetch("http://localhost:8000/api/planner/topic/")
.then((res) => res.json())
.then((data) => setAvailableTopics(data))
.catch((err) => console.error("Failed to load topics:", err));
}, []);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
try {
const existing_topic_ids = topics
.filter((t) => t.type === "existing")
.map((t) => t.id);
const new_topics = topics
.filter((t) => t.type === "new")
.map((t) => ({ name: t.name, color_hex: t.color_hex }));
const response = await fetch("http://localhost:8000/api/planner/tasks/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name: taskName,
description: description,
completed: false,
user_id: userId,
times: times,
existing_topic_ids: existing_topic_ids,
new_topics: new_topics,
}),
});
if (!response.ok) {
throw new Error("Failed to create task");
}
const newTask = await response.json();
onTaskCreated(newTask);
setTaskName("");
setDescription("");
setTimes([]);
setTopics([]);
} catch (error) {
console.error("Error creating task:", error);
}
};
return (
<form
onSubmit={handleSubmit}
className="task-form flex h-full w-full flex-col items-center justify-between gap-2 rounded-lg bg-slate-400 p-4 text-slate-200"
>
<input
className="title-input w-full rounded-lg bg-slate-400 px-3 py-1 text-3xl font-bold text-slate-100 brightness-90 placeholder:text-slate-300 hover:brightness-110"
type="text"
value={taskName}
onChange={(e) => setTaskName(e.target.value)}
placeholder="Task name"
required
/>
<div className="description-wrapper flex w-full flex-col items-center">
<h1 className="description-input-title text-xl hover:brightness-110">
Description
</h1>
<textarea
className="description-input w-full rounded-lg bg-slate-400 p-2 brightness-90 placeholder:text-slate-300 hover:brightness-110"
value={description}
onChange={(e) => setDescription(e.target.value)}
rows={7}
placeholder="Add description here..."
/>
</div>
<TopicInput
availableTopics={availableTopics}
topics={topics}
setTopics={setTopics}
/>
<TimeInput times={times} setTimes={setTimes} />
<button
className="w-fit rounded-lg border border-slate-300 bg-slate-400 px-3 py-1 text-xl brightness-110 hover:brightness-125"
type="submit"
>
Save
</button>
</form>
);
}