-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathScheduleDisplay.tsx
More file actions
343 lines (317 loc) · 14.2 KB
/
ScheduleDisplay.tsx
File metadata and controls
343 lines (317 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import React, { Component, useState, useEffect } from "react";
import classNames from "classnames";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { ReactFitty } from "react-fitty";
import { API_Day, API_Days } from "@scripts/apiTypes";
import { MeetTime, Section } from "@scripts/soc";
import { Schedule } from "@scripts/scheduleGenerator";
import { getSectionColor } from "@constants/frontend";
import { PERIOD_COUNTS } from "@constants/schedule";
import { GrPersonalComputer } from "react-icons/gr";
import { handleExportScheduleClick } from "@scripts/soc/calendar.ts";
interface Props {
schedule: Schedule;
}
interface States {}
// TODO: reconsider what to store
type MeetTimeInfo = {
meetTime: MeetTime;
courseColor: string;
courseNum: number;
sectionIsOnline: boolean;
};
// Pin/Save Feature (new)
const PinScheduleFeature = ({ schedule }) => {
const [pinnedSchedules, setPinnedSchedules] = useState([]);
// Load pinned schedules from localStorage when component mounts
useEffect(() => {
const savedSchedules = JSON.parse(localStorage.getItem('pinnedSchedules'));
if (savedSchedules) {
setPinnedSchedules(savedSchedules);
}
}, []);
const pinSchedule = (schedule) => {
const updatedPinned = [...pinnedSchedules, schedule];
setPinnedSchedules(updatedPinned);
localStorage.setItem('pinnedSchedules', JSON.stringify(updatedPinned));
};
const removePinnedSchedule = (index) => {
const updatedPinned = pinnedSchedules.filter((_, i) => i !== index);
setPinnedSchedules(updatedPinned);
localStorage.setItem('pinnedSchedules', JSON.stringify(updatedPinned));
};
return (
<div>
<button onClick={() => pinSchedule(schedule)}>📌 Pin</button>
{/* Display pinned schedules */}
<div className="pinned-schedules">
<h2>Pinned Schedules</h2>
{pinnedSchedules.length === 0 ? (
<p>No pinned schedules yet.</p>
) : (
pinnedSchedules.map((schedule, index) => (
<div key={index} className="pinned-schedule">
<h3>Pinned Schedule {index + 1}</h3>
<button onClick={() => removePinnedSchedule(index)}>Unpin</button>
</div>
))
)}
</div>
</div>
);
};
export default class ScheduleDisplay extends Component<Props, States> {
// TODO: redo this (it is *disgusting*); maybe there is a library that does the work
render() {
const schedule = this.props.schedule,
periodCounts = PERIOD_COUNTS[schedule.term];
// TODO: this is suspiciously similar to Meetings class
const blockSchedule: Record<API_Day, (MeetTimeInfo | null)[]> = {
[API_Day.Mon]: new Array(periodCounts.all).fill(null),
[API_Day.Tue]: new Array(periodCounts.all).fill(null),
[API_Day.Wed]: new Array(periodCounts.all).fill(null),
[API_Day.Thu]: new Array(periodCounts.all).fill(null),
[API_Day.Fri]: new Array(periodCounts.all).fill(null),
[API_Day.Sat]: new Array(periodCounts.all).fill(null),
};
schedule.forEach((section: Section, s: number) =>
API_Days.forEach((day) =>
section.meetings[day].forEach((mT) => {
const info: MeetTimeInfo = {
meetTime: mT,
courseColor: getSectionColor(s),
courseNum: s + 1,
sectionIsOnline: section.isOnline,
};
for (
let p = mT.periodBegin ?? periodCounts.all;
p <= mT.periodEnd ?? -1;
++p
)
blockSchedule[day][p - 1] = info;
}),
),
);
const divs = [];
for (let p = 0; p < periodCounts.all; ++p) {
for (const day of API_Days) {
// TODO: make this a checkbox or automatically change format to 6 days if schedule has a Saturday course
if (day == API_Day.Sat) continue;
//TODO: make this not absolutely horrible :)
const meetTimeInfo: MeetTimeInfo | null = blockSchedule[day][p];
if (meetTimeInfo == null) {
// No course
divs.push(
<div
className={classNames([
"border-solid",
"border-2",
"border-gray-300",
"rounded",
"whitespace-nowrap",
"text-center",
"h-6",
])}
></div>,
);
continue;
}
const mT = meetTimeInfo.meetTime,
color = meetTimeInfo.courseColor,
courseNum = meetTimeInfo.courseNum;
let location: React.JSX.Element = <i>TBD</i>;
if (mT.location) location = <>{mT.location}</>;
if (
mT.periodBegin != mT.periodEnd &&
(p == 0 ||
blockSchedule[day][p - 1] == null ||
blockSchedule[day][p - 1]!.meetTime != mT)
) {
// TODO: why do I have to do this garbage??
const spanMap: Map<number, string> = new Map<
number,
string
>([
[2, "row-span-2"],
[3, "row-span-3"],
[4, "row-span-4"],
[5, "row-span-5"],
[6, "row-span-6"],
]);
const span: string = spanMap.get(
Math.min(1 + (mT.periodEnd - mT.periodBegin), 6),
)!; // TODO: error handling for NaN
divs.push(
<div
className={classNames([
"border-solid",
"border-2",
"border-gray-400",
color,
"rounded",
"whitespace-nowrap",
"text-center",
span,
])}
>
<div className={"flex items-center h-full"}>
<ReactFitty
minSize={0}
maxSize={14}
className={"px-0.5"}
>
{location}
<sup>
<b>{courseNum}</b>
</sup>
</ReactFitty>
</div>
</div>,
);
} else if (
!(
p > 0 &&
mT != null &&
blockSchedule[day][p - 1] != null &&
blockSchedule[day][p - 1]!.meetTime == mT
)
)
divs.push(
<div
className={classNames([
"border-solid",
"border-2",
"border-gray-400",
color,
"rounded",
"whitespace-nowrap",
"text-center",
"h-6",
])}
>
<ReactFitty
minSize={0}
maxSize={14}
className={"px-0.5"}
>
{location}
<sup>
<b>{courseNum}</b>
</sup>
</ReactFitty>
</div>,
);
}
}
const onlineSections: Section[] = schedule.filter((s) => s.isOnline);
return (
<div className={"text-sm"}>
<button
onClick={() =>
handleExportScheduleClick(this.props.schedule)
}
className={
"bg-sky-500 hover:bg-sky-400 border border-blue-300 text-white text-sm rounded-lg p-1.5 mr-1 text-center mt-1.5 mb-1.5"
}
>
Export Schedule
</button>
{/* Added pin/save feature below */}
<PinScheduleFeature schedule={this.props.schedule} />
<div className={"min-w-full w-5/12 my-1"}>
<div className={"flex gap-1"}>
{schedule.map((sec: Section, s: number) => (
<div
className={classNames([
"border-solid",
"border-2",
"border-gray-400",
getSectionColor(s),
"rounded",
"text-center",
"grow",
])}
>
<b>({s + 1})</b> Sec. {sec.number} [
{sec.courseCode}]
</div>
))}
</div>
</div>
<div className={"min-w-full w-5/12 my-1 flex gap-1"}>
<div className={"inline-block h-max"}>
<div className={"grid grid-cols-1 gap-y-1"}>
{[...Array(periodCounts.all).keys()]
.map((p) => p + 1)
.map((p) => (
<div
className={
"border-solid border-2 border-gray-400 bg-gray-200 rounded text-center w-full h-6 px-0.5 min-w-full"
}
>
<b>
{MeetTime.formatPeriod(
p,
schedule.term,
)}
</b>
</div>
))}
{onlineSections.length > 0 && (
<div
className={
"border-solid border-2 border-gray-400 bg-gray-200 rounded text-center w-full h-6 px-0.5 min-w-full"
}
>
<div
className={
"flex items-center justify-center"
}
>
<GrPersonalComputer />️
</div>
</div>
)}
</div>
</div>
<div className={"inline-block grow"}>
<div className={"grid grid-cols-5 grid-rows-11 gap-1"}>
{divs}
{onlineSections.length > 0 && (
<div className={"col-span-5"}>
<div className={"min-w-full w-5/12 h-full"}>
<div className={"flex gap-1"}>
{onlineSections.map(
(sec: Section, ind: number) => (
<div
className={classNames([
"border-solid",
"border-2",
"border-gray-400",
getSectionColor(
ind,
),
"rounded",
"text-center",
"grow",
])}
>
{sec.displayName}
<sup>
<b>{1 + ind}</b>
</sup>
</div>
),
)}
</div>
</div>
</div>
)}
</div>
</div>
</div>
</div>
);
}
}