Skip to content

Commit f17b80b

Browse files
authored
Merge pull request #14 from codersforcauses/fixing_focus
fixed focus page to work with backend
2 parents 85bd681 + bb3f10d commit f17b80b

5 files changed

Lines changed: 118 additions & 79 deletions

File tree

client/src/components/ui/focus/taskDisplay.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ interface Time {
1616
export default function TaskDisplay({
1717
task,
1818
time,
19+
current
1920
}: {
2021
task: Task;
2122
time: Time;
23+
current: Boolean;
2224
}) {
2325
return (
24-
<div className="px-auto m-3 flex flex-auto flex-col rounded-3xl bg-indigo-400 p-4 shadow-md shadow-black/40">
26+
<div className={current ? "px-auto m-3 flex flex-auto flex-col rounded-3xl bg-indigo-500 p-4 shadow-md shadow-black/40 outline-3 outline outline-slate-50" : "px-auto m-3 flex flex-auto flex-col rounded-3xl bg-indigo-400 p-4 shadow-md shadow-black/40"}>
2527
<div className="flex flex-auto flex-row gap-4 px-3">
2628
<div>{task.name}:</div>
2729
<div>

client/src/components/ui/focus/timeDisplay.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ interface TimeDisplayProps {
66
statusSignal: (data: string) => void;
77
time: string;
88
current: boolean;
9+
day: number;
910
}
1011

1112
export default function TimeDisplay({
1213
statusSignal,
1314
time,
1415
current,
16+
day,
1517
}: TimeDisplayProps) {
1618
const [timeRemaining, setTimeRemaining] = useState(0);
1719

@@ -20,13 +22,15 @@ export default function TimeDisplay({
2022
const [Hours, Mins, Sec] = time.split(":").map(Number);
2123

2224
const d = new Date();
23-
const time_serial = Hours * 60 * 60 + Mins * 60 + Sec;
25+
const time_serial = Hours * 60 * 60 + Mins * 60 + Sec;
26+
var day_diff = ((day - d.getDay() + 8) % 8) * 24 * 60 * 60
27+
2428
const cur_time =
2529
d.getHours() * 60 * 60 + d.getMinutes() * 60 + d.getSeconds();
2630

2731
let remaining_time = 0;
2832

29-
remaining_time = time_serial - cur_time;
33+
remaining_time = time_serial - cur_time + day_diff;
3034
if (remaining_time <= 1) {
3135
clearInterval(getTimeRemaining);
3236
}
@@ -35,7 +39,7 @@ export default function TimeDisplay({
3539
}, 1000);
3640

3741
return () => clearInterval(getTimeRemaining);
38-
}, [time, timeRemaining]);
42+
}, [time, timeRemaining, day]);
3943

4044
useEffect(() => {
4145
if (timeRemaining <= 1) {

client/src/components/ui/focus/upcomingTasks.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,12 @@ interface Time {
1717
task: number;
1818
}
1919

20-
interface Tasks {
21-
tasks: Task[];
22-
times: Time[];
23-
}
24-
2520
function serializeTime(time: string) {
2621
const [Hours, Mins, Sec] = time.split(":").map(Number);
2722
return Hours * 60 * 60 + Mins * 60 + Sec;
2823
}
2924

30-
export default function UpcomingTasks({ tasks, times }: Tasks) {
25+
export default function UpcomingTasks({ tasks, times, refresh }: { tasks: Task[], times:Time[], refresh : Boolean}) {
3126
const [upcomingTimes, setUpcomingTimes] = useState<Time[]>([]);
3227

3328
useEffect(() => {
@@ -53,17 +48,20 @@ export default function UpcomingTasks({ tasks, times }: Tasks) {
5348
}
5449

5550
sortTimes();
56-
}, [times, tasks]);
51+
}, [times, tasks, refresh]);
5752

5853
return (
5954
<div className="rounded-2xl bg-slate-900 p-2 py-3 shadow-xl shadow-black/40">
6055
<div className="scrollbar h-[285px] w-96 overflow-y-auto overflow-x-hidden">
61-
{upcomingTimes.map((time: Time) => (
56+
{upcomingTimes.map((time: Time, index) => (
57+
<>
6258
<TaskDisplay
6359
task={tasks.filter((task) => task.id === time.task)[0]}
6460
time={time}
6561
key={time.id}
62+
current={index===0}
6663
/>
64+
</>
6765
))}
6866
</div>
6967
</div>

client/src/components/ui/timeDisplay.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface TimeDisplayProps {
1111
export default function TimeDisplay({
1212
statusSignal,
1313
time,
14-
current,
14+
current
1515
}: TimeDisplayProps) {
1616
const [timeRemaining, setTimeRemaining] = useState(0);
1717

client/src/pages/focus.tsx

Lines changed: 101 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, { useEffect, useState } from "react";
33
import CurrentTaskTitle from "@/components/ui/focus/currentTaskTitle";
44
import TimeDisplay from "@/components/ui/focus/timeDisplay";
55
import UpcomingTasks from "@/components/ui/focus/upcomingTasks";
6-
import Refresh from "@/components/ui/refresh";
6+
import { useRouter } from "next/router";
77

88
interface 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+
1723
interface Task {
1824
id: number;
1925
name: string;
26+
completed: boolean;
2027
description: string;
28+
times: Time[];
29+
topics: Topic[];
2130
}
2231

2332
function serializeTime(time: string) {
@@ -26,94 +35,118 @@ function serializeTime(time: string) {
2635
}
2736

2837
const 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

Comments
 (0)