@@ -5,6 +5,7 @@ import Timetable, {
55 resizeAndPositionTimetableElements ,
66} from "@/components/timetable" ;
77import { TimetableTaskProps } from "@/components/timetable_task" ;
8+ import { useRouter } from "next/router" ;
89
910/*
1011Database representation of Time.
@@ -59,6 +60,8 @@ Page containing a Timetable to visually represent the times that tasks have been
5960assigned.
6061*/
6162function Schedule ( ) {
63+
64+ const router = useRouter ( ) ;
6265 const [ timetableTasksProps , setTimetableTasksProps ] = useState <
6366 TimetableTaskProps [ ]
6467 > ( [ ] ) ;
@@ -82,13 +85,35 @@ function Schedule() {
8285 to be displayed. Sets the timetableTasksProps State variable once finished.
8386 */
8487 async function fetchTasks ( ) {
85- const API_URL = "http://localhost:8000/api/planner/tasks/" ;
88+
8689 try {
87- const response = await fetch ( API_URL ) ;
88- if ( ! response . ok ) {
89- throw new Error ( `Response status: ${ response . status } ` ) ;
90+ const token = localStorage . getItem ( "access" ) ;
91+ if ( ! token ) {
92+ router . push ( "/login" ) ; //redirect to login if unauthorised
93+ return ;
94+ }
95+ const auth = await fetch (
96+ "http://localhost:8000/api/planner/protected/" ,
97+ {
98+ headers : {
99+ Authorization : `Bearer ${ token } ` ,
100+ } ,
101+ } ,
102+ ) ;
103+ if ( ! auth . ok ) {
104+ router . push ( "/login" ) ; //redirect to login if unauthorised
105+ return ;
90106 }
91- const data = await response . json ( ) ;
107+ const tasksFetch = await fetch (
108+ `http://localhost:8000/api/planner/tasks/` ,
109+ {
110+ headers : {
111+ Authorization : `Bearer ${ token } ` ,
112+ } ,
113+ } ,
114+ ) ;
115+
116+ const data = await tasksFetch . json ( ) ;
92117 let timetable_task_props =
93118 await formatTaskDataToTimetableTaskProps ( data ) ;
94119 timetable_task_props =
@@ -269,12 +294,12 @@ function Schedule() {
269294 return ( ) => {
270295 removeEventListeners ( ) ;
271296 } ;
272- } , [ ] ) ;
297+ } , [ router ] ) ;
273298 /* This should be dependent on timetableTasksProps however doing so causes it
274299 to run in an infinite loop. */
275300
276301 return (
277- < div className = "content-container min-w-screen min-h-screen flex w-full flex-row bg-slate-950" >
302+ < div className = "content-container min-w-screen h-[calc(100vh-64px)] flex w-full flex-row bg-slate-950" >
278303 < div className = "timetable-container max-h-[90vh] w-full p-3" >
279304 < Timetable timetable_tasks_props = { timetableTasksProps } />
280305 </ div >
0 commit comments