Skip to content

Commit

Permalink
Automatic course filtering when pressing enter on course page
Browse files Browse the repository at this point in the history
  • Loading branch information
SebiWrn committed Sep 20, 2024
1 parent 2a84e4c commit 6b37b1d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion web/template/search-global.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
selectedSemesterIndex !== -1 ? searcher.search(semesters[selectedSemesterIndex].Year, semesters[selectedSemesterIndex].TeachingTerm) : searcher.search(semesters[0].Year, semesters[0].TeachingTerm);"
@keyup="
if ($event.key == 'Enter') {
window.location.href = '/search?q=' + encodeURIComponent(document.getElementById('search-courses').value);
window.location.href = '/search?q=' + encodeURIComponent(document.getElementById('search-courses').value) + '&course=' + (home.isInCourse() ? home.generateCourseFromParam() : '');
return;
}
searcher.searchInput = document.getElementById('search-courses').value;
Expand Down
46 changes: 45 additions & 1 deletion web/template/search-page.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,51 @@
<main id="content" class="flex grow h-full overflow-y-scroll">
<section class="w-full">
<div id="search" class="h-fit w-full flex items-center flex-col" x-data="{searcher: home.filteredSearch(), filterSemesters: [], filterCourses: [], query: home.getSearchQueryFromParam(), courseFilterQuery: '', courseQuery: home.getCourseFromParam(), semesterQuery: home.getSemestersFromParam(), courses: [], oldSemesterFilter: [], shownCourses: []}"
x-init="home.initSearchBarArrowKeysListener()">
x-init="
home.initSearchBarArrowKeysListener();
if(courseQuery != '') {

let year = home.getYearFromCourse(courseQuery);
let term = home.getTermFromCourse(courseQuery);
let slug = home.getSlugFromCourse(courseQuery);




let timeout = setInterval(async function() {
if (semesters.length > 0) {
clearInterval(timeout);
console.log(year + ' ' + term);
console.log(semesters);

for (let i = 0; i < semesters.length; i++) {
console.log(semesters[i].Year + ' ' + semesters[i].TeachingTerm);
if (semesters[i].Year == year && semesters[i].TeachingTerm == term) {
filterSemesters.push(i);
console.log('found semester');
break;
}
}

courses = await home.getCoursesOfSemesters(semesters, filterSemesters);
oldSemesterFilter = filterSemesters;
shownCourses = courses;

for (let i = 0; i < courses.length; i++) {
if (courses[i].Slug == slug) {
filterCourses.push(i);
console.log('found course');
break;
}
}

searcher.searchWithDataFromPage(semesters, filterSemesters, courses, filterCourses)
}
}, 100);


}
">
<!--Search Bar-->
<div class="flex items-center rounded-full bg-gray-100 text-sm py-1 h-12 dark:bg-gray-800" style="width: 50%;">
<i class="fa-solid fa-search pl-3 text-xs text-7 text-black dark:text-gray-300"></i>
Expand Down
24 changes: 23 additions & 1 deletion web/ts/search.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Semester} from "./api/semesters";
import {Course, CoursesAPI} from "./api/courses";
import {Alpine} from "alpinejs";

export function coursesSearch() {
return {
Expand Down Expand Up @@ -174,7 +175,7 @@ export function getSearchQueryFromParam() {
export function getCourseFromParam() {
let url = new URL(document.location.href);
let params = new URLSearchParams(url.search);
return params.get("slug");
return params.get("course");
}

export function getSemestersFromParam() {
Expand All @@ -183,6 +184,27 @@ export function getSemestersFromParam() {
return params.get("semester");
}

export function generateCourseFromParam() {
let url = new URL(document.location.href);
let params = new URLSearchParams(url.search);
let slug = params.get("slug");
let year = params.get("year");
let term = params.get("term");
return slug + year + term;
}

export function getYearFromCourse(course: string) {
return parseInt(course.substring(course.length - 5, course.length - 1));
}

export function getTermFromCourse(course: string) {
return course.substring(course.length - 1, course.length);
}

export function getSlugFromCourse(course: string) {
return course.substring(0, course.length - 5);
}

export async function getCoursesOfSemesters(semesters: Semester[], filterSemesters: number[]) : Promise<Course[]>{
let courses : Course[] = [];
for(let i = 0; i < filterSemesters.length; i++) {
Expand Down

0 comments on commit 6b37b1d

Please sign in to comment.