Skip to content

Commit

Permalink
fix: default to no sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
rohan-b-84 committed Sep 9, 2024
1 parent e906bf2 commit 7345972
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions frontend/src/components/SearchResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ const examMap = (exam: string) => {
return exam.slice(0, 3).toUpperCase();
};

type SortBy = 'course_name' | 'year';
type SortBy = 'course_name' | 'year' | undefined;
type SortOrder = 'ascending' | 'descending';

const SearchResults: Component<Props> = (props) => {
const [displayedResults, setDisplayedResults] = createSignal<ISearchResult[]>(props.results);
const [filterByYear, setFilterByYear] = createSignal<number | null>(null);
const [sortBy, setSortBy] = createSignal<SortBy>("year");
const [sortBy, setSortBy] = createSignal<SortBy>(undefined);
const [sortOrder, setSortOrder] = createSignal<SortOrder>("descending");
const [availableYears, setAvailableYears] = createSignal<number[]>([]);

Expand All @@ -39,6 +39,11 @@ const SearchResults: Component<Props> = (props) => {
let filtered_results = props.results.slice();
if (filterByYear() !== null) filtered_results = filtered_results.filter((result) => result.year === filterByYear());

if (!sortBy()) {
setDisplayedResults(filtered_results)
return
}

const sorted_results = filtered_results.sort((a, b) => {
// Fall back to course name sorting when results are filtered by year.
const fallback_sorting = sortBy() === 'year' && filterByYear() !== null;
Expand All @@ -54,6 +59,8 @@ const SearchResults: Component<Props> = (props) => {
return first.year - second.year;
case "course_name":
return first.course_name.localeCompare(second.course_name);
default:
return 0;
}
});

Expand Down Expand Up @@ -127,7 +134,7 @@ const SearchResults: Component<Props> = (props) => {
<td style={{display: 'flex', "align-items": 'center'}}>
<p>
{result.course_name}&nbsp;
{result.exam !== "" && <span class="result-card-tag">{examMap(result.exam)}</span>}
{result.exam && <span class="result-card-tag">{examMap(result.exam)}</span>}
</p>
<div class="result-card-btns">
<a
Expand Down

0 comments on commit 7345972

Please sign in to comment.