-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpage.tsx
More file actions
172 lines (158 loc) · 5.7 KB
/
page.tsx
File metadata and controls
172 lines (158 loc) · 5.7 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
'use client';
import Link from 'next/link';
import { useEffect, useState } from 'react';
import Balancer from 'react-wrap-balancer';
import useSWR from 'swr';
import Chip from '@/components/chip';
import api from '@/utils/api-actions';
import CoursesTable from './courses-table';
const branches = ['CE', 'CS', 'EC', 'EE', 'IT', 'ME', 'PI'] as const;
const semesters = ['1', '2', '3', '4', '5', '6', '7', '8'] as const;
export default function CoursesPage() {
const {
data: courses,
isLoading,
error,
}: { data: Course[] | undefined; isLoading: boolean; error: any } = useSWR(
`/courses`,
api.GET
);
if (error) console.error(error);
const [branch, setBranch] = useState<Branch>();
const [semester, setSemester] = useState<Semester>();
const [filteredCourses, setFilteredCourses] = useState<Array<Course>>([]);
useEffect(() => {
if (!courses) return;
setFilteredCourses(
courses
.filter((course) => {
if (!branch && !semester) {
return true;
} else if (branch && !semester) {
return course.specifics.some(
(specific) => specific.branch == branch
);
} else if (!branch && semester) {
return course.specifics.some(
(specific) => specific.semester == semester
);
} else {
return course.specifics.some(
(specific) =>
specific.branch == branch && specific.semester == semester
);
}
})
.sort(({ code: codePrev }, { code: codeNext }) =>
Number(codePrev > codeNext)
)
);
}, [branch, semester, isLoading]);
return (
<>
<table className="w-full">
<tbody>
<tr className="flex flex-row justify-between gap-4 text-palette-800 dark:text-palette-100">
<td className="border-none px-0">
<fieldset className="p-2 sm:p-3 md:p-4 border-2 rounded border-palette-400">
<legend className="px-2">Choose your branch</legend>
<Balancer>
<section className="flex flex-row flex-wrap gap-2 sm:gap-3 md:gap-4 justify-center">
{branches.map((value: Branch, index) => {
const isSelected = value === branch;
return (
<Chip
key={index}
label={value}
onClick={() => {
if (isSelected) {
setBranch(undefined);
} else {
setBranch(value);
}
}}
isSelected={isSelected}
/>
);
})}
</section>
</Balancer>
</fieldset>
</td>
<td className="border-none px-0">
<fieldset className="p-2 sm:p-3 md:p-4 border-2 rounded border-palette-400">
<legend className="px-2">Choose your semester</legend>
<Balancer>
<section className="flex flex-row flex-wrap gap-2 sm:gap-3 md:gap-4 justify-center">
{semesters.map((value: Semester, index) => {
const isSelected = value === semester;
return (
<Chip
key={index}
label={value}
onClick={() => {
if (isSelected) {
setSemester(undefined);
} else {
setSemester(value);
}
}}
isSelected={isSelected}
/>
);
})}
</section>
</Balancer>
</fieldset>
</td>
</tr>
</tbody>
</table>
<br />
<br />
{!isLoading && !(branch && semester) && filteredCourses.length > 0 ? (
<>
{branch || semester ? <></> : <h2 className='text-palette-800 dark:text-palette-100'>All courses</h2>}
{branch ? <h2 className='text-palette-800 dark:text-palette-100'>All courses in the {branch} branch</h2> : <></>}
{semester ? <h2 className='text-palette-800 dark:text-palette-100'>All courses in semester {semester}</h2> : <></>}
<ul>
{filteredCourses.map((course) => {
return (
<Link href={`/courses/${course.code}`} key={course.code}>
<li className="list-disclosure-closed ml-4 text-palette-800 dark:text-palette-100">
<h4>
{course.code}:{' '}
<span className="font-normal">{course.title}</span>
</h4>
</li>
</Link>
);
})}
</ul>
</>
) : (
<></>
)}
{!isLoading && branch && semester ? (
filteredCourses.length ? (
<CoursesTable
branch={branch}
courses={filteredCourses}
semester={semester}
/>
) : (
<p>
<Balancer>
No courses were found for the given filters. Don't worry, we will
add them soon! If you want a particular course or a set of courses
added sooner, please contact us at any our handles mentioned at
the bottom of this page!
</Balancer>
</p>
)
) : (
<></>
)}
</>
);
}