diff --git a/client/src/components/task_form.tsx b/client/src/components/task_form.tsx index 868be10..c35a88e 100644 --- a/client/src/components/task_form.tsx +++ b/client/src/components/task_form.tsx @@ -51,6 +51,13 @@ export function TaskForm({ userId, onTaskCreated }: TaskFormProps) { const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); + + const token = localStorage.getItem("access"); + if (!token) { + console.error("No access token"); + return; + } + try { const existing_topic_ids = topics .filter((t) => t.type === "existing") @@ -63,6 +70,7 @@ export function TaskForm({ userId, onTaskCreated }: TaskFormProps) { method: "POST", headers: { "Content-Type": "application/json", + Authorization: `Bearer ${token}`, }, body: JSON.stringify({ name: taskName, diff --git a/client/src/components/task_item.tsx b/client/src/components/task_item.tsx index 50d6079..538fcd0 100644 --- a/client/src/components/task_item.tsx +++ b/client/src/components/task_item.tsx @@ -82,6 +82,9 @@ export function TaskItem({ async function saveEdit() { setSaving(true); + + const token = localStorage.getItem("access"); + try { const existing_topic_ids = draftTopics .filter((t) => t.type === "existing") @@ -98,7 +101,10 @@ export function TaskItem({ `http://localhost:8000/api/planner/tasks/${item.id}/`, { method: "PUT", - headers: { "Content-Type": "application/json" }, + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, body: JSON.stringify({ name: draftName, description: draftDescription, diff --git a/client/src/components/timetable.tsx b/client/src/components/timetable.tsx index 27240c4..dd954ab 100644 --- a/client/src/components/timetable.tsx +++ b/client/src/components/timetable.tsx @@ -374,7 +374,7 @@ function Timetable({ timetable_tasks_props }: TimetableProps) { >
-
-
{task.name}:
-
- {time.start_time} - {time.end_time} -
+
+
{task.name}
+
+ {time.day === 1 + ? "Mon" + : time.day === 2 + ? "Tue" + : time.day === 3 + ? "Wed" + : time.day === 4 + ? "Thu" + : time.day === 5 + ? "Fri" + : time.day === 6 + ? "Sat" + : time.day === 7 + ? "Sun" + : ""}{" "} + - {time.start_time} - {time.end_time}
-
{task.description}
+
{task.description}
); } diff --git a/client/src/components/ui/focus/timer.tsx b/client/src/components/ui/focus/timer.tsx index 353ac7a..2c3bc3a 100644 --- a/client/src/components/ui/focus/timer.tsx +++ b/client/src/components/ui/focus/timer.tsx @@ -24,7 +24,7 @@ const Timer = (timeRemaining: number) => { }; return ( -
+
{formatTime(timeRemaining)}
); diff --git a/client/src/pages/focus.tsx b/client/src/pages/focus.tsx index 8a44e9a..9d755cd 100644 --- a/client/src/pages/focus.tsx +++ b/client/src/pages/focus.tsx @@ -152,7 +152,7 @@ const CountdownTimer = () => { } return ( -
+

focus

diff --git a/client/src/pages/login.tsx b/client/src/pages/login.tsx index cbe633c..ce7087a 100644 --- a/client/src/pages/login.tsx +++ b/client/src/pages/login.tsx @@ -73,10 +73,10 @@ export default function Login() {
-
+
-
+
-
- - -
+
+ + +
([]); @@ -82,13 +85,35 @@ function Schedule() { to be displayed. Sets the timetableTasksProps State variable once finished. */ async function fetchTasks() { - const API_URL = "http://localhost:8000/api/planner/tasks/"; + try { - const response = await fetch(API_URL); - if (!response.ok) { - throw new Error(`Response status: ${response.status}`); + const token = localStorage.getItem("access"); + if (!token) { + router.push("/login"); //redirect to login if unauthorised + return; + } + const auth = await fetch( + "http://localhost:8000/api/planner/protected/", + { + headers: { + Authorization: `Bearer ${token}`, + }, + }, + ); + if (!auth.ok) { + router.push("/login"); //redirect to login if unauthorised + return; } - const data = await response.json(); + const tasksFetch = await fetch( + `http://localhost:8000/api/planner/tasks/`, + { + headers: { + Authorization: `Bearer ${token}`, + }, + }, + ); + + const data = await tasksFetch.json(); let timetable_task_props = await formatTaskDataToTimetableTaskProps(data); timetable_task_props = @@ -269,12 +294,12 @@ function Schedule() { return () => { removeEventListeners(); }; - }, []); + }, [router]); /* This should be dependent on timetableTasksProps however doing so causes it to run in an infinite loop. */ return ( -
+
diff --git a/client/src/pages/tasks.tsx b/client/src/pages/tasks.tsx index 3debb62..f89c9f8 100644 --- a/client/src/pages/tasks.tsx +++ b/client/src/pages/tasks.tsx @@ -49,7 +49,8 @@ export default function TasksPage() { router.push("/login"); //redirect to login if unauthorised return; } - const auth = await fetch( "http://localhost:8000/api/planner/protected/", + const auth = await fetch( + "http://localhost:8000/api/planner/protected/", { headers: { Authorization: `Bearer ${token}`, @@ -102,6 +103,8 @@ export default function TasksPage() { const task = items.find((t) => t.id === id); if (!task) return; + const token = localStorage.getItem("access"); + try { const response = await fetch( `http://localhost:8000/api/planner/tasks/${id}/toggle_complete/`, @@ -109,6 +112,7 @@ export default function TasksPage() { method: "PATCH", headers: { "Content-Type": "application/json", + Authorization: `Bearer ${token}`, }, body: JSON.stringify({ completed: !task.completed }), }, @@ -135,10 +139,17 @@ export default function TasksPage() { async function handleTaskDeleted(id: number) { if (!window.confirm("Delete this task?")) return; + const token = localStorage.getItem("access"); + try { const response = await fetch( `http://localhost:8000/api/planner/tasks/${id}/`, - { method: "DELETE" }, + { + method: "DELETE", + headers: { + Authorization: `Bearer ${token}`, + }, + }, ); if (!response.ok) { @@ -152,7 +163,7 @@ export default function TasksPage() { } return ( -
+
@@ -163,10 +174,10 @@ export default function TasksPage() {