@@ -3,7 +3,7 @@ import React, { useEffect, useState } from "react";
33import CurrentTaskTitle from "@/components/ui/focus/currentTaskTitle" ;
44import TimeDisplay from "@/components/ui/focus/timeDisplay" ;
55import UpcomingTasks from "@/components/ui/focus/upcomingTasks" ;
6- import Refresh from "@/components/ui/refresh " ;
6+ import { useRouter } from "next/router " ;
77
88interface Time {
99 id : number ;
@@ -14,10 +14,19 @@ interface Time {
1414 task : number ;
1515}
1616
17+ interface Topic {
18+ id : number ;
19+ name : string ;
20+ color_hex : number ;
21+ }
22+
1723interface Task {
1824 id : number ;
1925 name : string ;
26+ completed : boolean ;
2027 description : string ;
28+ times : Time [ ] ;
29+ topics : Topic [ ] ;
2130}
2231
2332function serializeTime ( time : string ) {
@@ -26,94 +35,118 @@ function serializeTime(time: string) {
2635}
2736
2837const CountdownTimer = ( ) => {
38+ const router = useRouter ( ) ;
2939 const [ times , setTimes ] = useState < Time [ ] > ( [ ] ) ;
3040 const [ tasks , setTasks ] = useState < Task [ ] > ( [ ] ) ;
3141 const [ currentTime , setCurrentTime ] = useState < Time | null > ( ) ;
3242 const [ currentTask , setCurrentTask ] = useState < Task | null > ( ) ;
3343 const [ nextTime , setNextTime ] = useState < Time | null > ( ) ;
34- const [ loading , setLoading ] = useState ( false ) ;
35-
36- async function refreshTimes ( ) {
37- setLoading ( true ) ;
38-
39- try {
40- const response = await fetch ( "http://localhost:8000/api/planner/time/" ) ;
41- if ( ! response . ok ) {
42- throw new Error ( `Response status: ${ response . status } ` ) ;
43- }
44-
45- const result = await response . json ( ) ;
46- setTimes ( result ) ;
47- getCurrentTask ( result ) ;
48- setLoading ( false ) ;
49- } catch ( error ) {
50- console . error ( error ) ;
51- }
52- }
44+ const [ refresh , setRefresh ] = useState ( false ) ;
5345
5446 useEffect ( ( ) => {
5547 async function fetchTask ( ) {
5648 try {
57- const response = await fetch (
58- "http://localhost:8000/api/planner/tasks/" ,
49+ const token = localStorage . getItem ( "access" ) ;
50+ if ( ! token ) {
51+ router . push ( "/login" ) ; //redirect to login if unauthorised
52+ return ;
53+ }
54+ const auth = await fetch (
55+ "http://localhost:8000/api/planner/protected/" ,
56+ {
57+ headers : {
58+ Authorization : `Bearer ${ token } ` ,
59+ } ,
60+ } ,
5961 ) ;
60- if ( ! response . ok ) {
61- throw new Error ( `Response status: ${ response . status } ` ) ;
62+ if ( ! auth . ok ) {
63+ router . push ( "/login" ) ; //redirect to login if unauthorised
64+ return ;
6265 }
63- const result = await response . json ( ) ;
66+ const tasksFetch = await fetch (
67+ `http://localhost:8000/api/planner/tasks/` ,
68+ {
69+ headers : {
70+ Authorization : `Bearer ${ token } ` ,
71+ } ,
72+ } ,
73+ ) ;
74+ if ( ! tasksFetch . ok ) {
75+ throw new Error ( `Response status: ${ tasksFetch . status } ` ) ;
76+ }
77+ const result = await tasksFetch . json ( ) ;
78+ console . log ( "tasks: " , result ) ;
6479 setTasks ( result ) ;
65- const cur_task = tasks . filter ( ( task ) => task . id === currentTime ?. task ) ;
66- setCurrentTask ( cur_task [ 0 ] ) ;
80+
6781 } catch ( error ) {
6882 console . error ( error ) ;
6983 }
7084 }
7185
7286 fetchTask ( ) ;
73- } , [ tasks , currentTime ] ) ;
87+ } , [ router ] ) ;
88+
89+ useEffect ( ( ) => {
90+ var task_times : Time [ ] = [ ] ;
91+ for ( let index = 0 ; index < tasks . length ; index ++ ) {
92+ const task = tasks [ index ] ;
93+ for ( let i = 0 ; i < task . times . length ; i ++ ) {
94+ task_times = task_times . concat ( task . times [ i ] )
95+ console . log ( "new time: " , task . times [ i ] ) ;
96+ }
97+ }
98+ setTimes ( task_times ) ;
99+ console . log ( "times: " , task_times ) ;
100+ getCurrentTask ( task_times ) ;
101+ } , [ tasks ] )
74102
75103 function getCurrentTask ( ts : Time [ ] ) {
76- console . log ( times ) ;
77- let upcomingTimes : Time [ ] ;
78-
79- const d = new Date ( ) ;
80- const cur_time =
81- d . getHours ( ) * 60 * 60 + d . getMinutes ( ) * 60 + d . getSeconds ( ) ;
82- const cur_d = d . getDay ( ) ;
83-
84- // filter out finished tasks
85- upcomingTimes = ts . filter ( ( time ) => {
86- return ! ( cur_d == time . day && serializeTime ( time . end_time ) <= cur_time ) ;
87- } ) ;
88-
89- // sort by start time
90- upcomingTimes = upcomingTimes . sort ( ( a , b ) => {
91- return (
92- serializeTime ( a . start_time ) +
93- ( ( a . day + 8 - cur_d ) % 8 ) * 24 * 60 * 60 -
94- ( serializeTime ( b . start_time ) + ( ( b . day + 8 - cur_d ) % 8 ) * 24 * 60 * 60 )
95- ) ;
96- } ) ;
97-
98- // logic for if there are no tasks currently or at all
99- if ( upcomingTimes . length > 0 ) {
100- if (
101- serializeTime ( upcomingTimes [ 0 ] . start_time ) < cur_time &&
102- upcomingTimes [ 0 ] . day == cur_d
103- ) {
104- setCurrentTime ( upcomingTimes [ 0 ] ) ;
104+ let upcomingTimes : Time [ ] ;
105+
106+ const d = new Date ( ) ;
107+ const cur_time =
108+ d . getHours ( ) * 60 * 60 + d . getMinutes ( ) * 60 + d . getSeconds ( ) ;
109+ const cur_d = d . getDay ( ) ;
110+
111+ // filter out finished tasks
112+ upcomingTimes = ts . filter ( ( time ) => {
113+ return ! ( cur_d == time . day && serializeTime ( time . end_time ) <= cur_time + 10 ) ;
114+ } ) ;
115+
116+ // sort by start time
117+ upcomingTimes = upcomingTimes . sort ( ( a , b ) => {
118+ return (
119+ serializeTime ( a . start_time ) +
120+ ( ( a . day + 8 - cur_d ) % 8 ) * 24 * 60 * 60 -
121+ ( serializeTime ( b . start_time ) + ( ( b . day + 8 - cur_d ) % 8 ) * 24 * 60 * 60 )
122+ ) ;
123+ } ) ;
124+
125+ // logic for if there are no tasks currently or at all
126+ if ( upcomingTimes . length > 0 ) {
127+ if (
128+ serializeTime ( upcomingTimes [ 0 ] . start_time ) < cur_time &&
129+ upcomingTimes [ 0 ] . day == cur_d
130+ ) {
131+ setCurrentTime ( upcomingTimes [ 0 ] ) ;
132+
133+ const cur_task = tasks . filter ( ( task ) => task . id === upcomingTimes [ 0 ] ?. task ) ;
134+ console . log ( tasks , cur_task )
135+ setCurrentTask ( cur_task [ 0 ] ) ;
136+ } else {
137+ setCurrentTime ( null ) ;
138+ setCurrentTask ( null ) ;
139+ setNextTime ( upcomingTimes [ 0 ] ) ;
140+ }
105141 } else {
106142 setCurrentTime ( null ) ;
107- setNextTime ( upcomingTimes [ 0 ] ) ;
143+ setNextTime ( null ) ;
108144 }
109- } else {
110- setCurrentTime ( null ) ;
111- setNextTime ( null ) ;
112145 }
113- }
114146
115147 function onTaskEnd ( status : string ) {
116148 if ( status == "finished" ) {
149+ setRefresh ( ! refresh ) ;
117150 getCurrentTask ( times ) ;
118151 }
119152 }
@@ -130,6 +163,7 @@ const CountdownTimer = () => {
130163 statusSignal = { onTaskEnd }
131164 time = { currentTime . end_time }
132165 current = { true }
166+ day = { currentTime . day }
133167 />
134168 ) : (
135169 < >
@@ -138,6 +172,7 @@ const CountdownTimer = () => {
138172 statusSignal = { onTaskEnd }
139173 time = { nextTime . start_time }
140174 current = { false }
175+ day = { nextTime . day }
141176 />
142177 ) : (
143178 < > </ >
@@ -154,20 +189,20 @@ const CountdownTimer = () => {
154189 </ div >
155190 </ div >
156191 < div >
157- { times . length > 1 ? (
192+ { tasks . length > 0 ? (
158193 < div className = "justify-top flex flex-col pl-8" >
159194 < div className = "p-4 font-inter text-2xl font-semibold" >
160- upcoming
195+ tasks
161196 </ div >
162- < UpcomingTasks tasks = { tasks } times = { times } />
197+ < UpcomingTasks tasks = { tasks } times = { times } refresh = { refresh } />
163198 </ div >
164199 ) : (
165200 < > </ >
166201 ) }
167202 </ div >
168203 </ div >
169204 < div className = "p-8" >
170- < Refresh loading = { loading } update = { refreshTimes } />
205+
171206 </ div >
172207 </ div >
173208 </ div >
0 commit comments