Skip to content

Commit ebeb1a6

Browse files
committed
made topics user-specific rather than general
1 parent 3d5b4a5 commit ebeb1a6

3 files changed

Lines changed: 50 additions & 10 deletions

File tree

client/src/components/task_form.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,28 @@ export function TaskForm({ userId, onTaskCreated }: TaskFormProps) {
4343
const [availableTopics, setAvailableTopics] = useState<Topic[]>([]);
4444

4545
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));
46+
async function fetchTopics() {
47+
try {
48+
const token = localStorage.getItem("access");
49+
if (!token) return;
50+
51+
const res = await fetch("http://localhost:8000/api/planner/topic/", {
52+
headers: {
53+
Authorization: `Bearer ${token}`,
54+
},
55+
});
56+
57+
if (!res.ok) {
58+
throw new Error("Failed to load topics");
59+
}
60+
61+
const data = await res.json();
62+
setAvailableTopics(data);
63+
} catch (err) {
64+
console.error("Failed to load topics", err);
65+
}
66+
}
67+
fetchTopics();
5068
}, []);
5169

5270
const handleSubmit = async (e: React.FormEvent) => {
@@ -67,7 +85,7 @@ export function TaskForm({ userId, onTaskCreated }: TaskFormProps) {
6785
.filter((t) => t.type === "new")
6886
.map((t) => ({ name: t.name, color_hex: t.color_hex }));
6987

70-
const validTimes = times.filter(t => t.start_time && t.end_time);
88+
const validTimes = times.filter((t) => t.start_time && t.end_time);
7189

7290
const response = await fetch("http://localhost:8000/api/planner/tasks/", {
7391
method: "POST",

client/src/pages/tasks.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,29 @@ export default function TasksPage() {
8585
}, [router]);
8686

8787
useEffect(() => {
88-
fetch("http://localhost:8000/api/planner/topic/")
89-
.then((res) => res.json())
90-
.then((data) => setAvailableTopics(data))
91-
.catch((err) => console.error("Failed to load topics", err));
88+
async function fetchTopics() {
89+
try {
90+
const token = localStorage.getItem("access");
91+
if (!token) return;
92+
93+
const res = await fetch("http://localhost:8000/api/planner/topic/", {
94+
headers: {
95+
Authorization: `Bearer ${token}`,
96+
},
97+
});
98+
99+
if (!res.ok) {
100+
throw new Error("Failed to load topics");
101+
}
102+
103+
const data = await res.json();
104+
setAvailableTopics(data);
105+
} catch (err) {
106+
console.error("Failed to load topics", err);
107+
}
108+
}
109+
110+
fetchTopics();
92111
}, []);
93112

94113
if (loading) {

server/task_planner/views.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ def get(self, request):
5555
})
5656

5757
class TopicList(APIView):
58+
authentication_classes = [JWTAuthentication]
59+
permission_classes = [IsAuthenticated]
60+
5861
def get(self, request):
59-
topics = Topic.objects.all()
62+
topics = Topic.objects.filter(user=request.user)
6063
serializer = TopicReadSerializer(topics, many=True)
6164
return Response(serializer.data)
6265

0 commit comments

Comments
 (0)