Skip to content

Commit 0fc3662

Browse files
authored
Fixes and sorting (#120)
* fixed summation after long fight with git :! * sorting kinda works - some problems exist * fixed search + filter interaction * sorting is better now / Metallic Materials are the only issue * asc/desc have correct icon now * added favicon * fixed the website name in index.html * ArrowLeft closes the CoursePopup * popup variables moved to the model * popup variables moved to the model * bug fix * prefix matching is preffered in search function --------- Co-authored-by: Kacper Lisik <lisik@kth.se>
1 parent 6067843 commit 0fc3662

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

my-app/src/presenters/SearchbarPresenter.jsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,19 @@ const SearchbarPresenter = observer(({ model }) => {
2828
model.setCurrentSearch(model.filteredCourses);
2929
} else {
3030
const fuse = new Fuse(model.filteredCourses, fuseOptions);
31-
const results = fuse.search(query).map((r) => r.item);
32-
model.setCurrentSearch(results);
31+
const results = fuse.search(query);
32+
33+
const sortedResults = results.sort((a, b) => {
34+
const aStartsWith = a.item.code.toLowerCase().startsWith(query.toLowerCase());
35+
const bStartsWith = b.item.code.toLowerCase().startsWith(query.toLowerCase());
36+
37+
//sort by prefix match as a primary sorting
38+
if (aStartsWith && !bStartsWith) return -1;
39+
if (!aStartsWith && bStartsWith) return 1;
40+
return a.score - b.score; //Fuse.js score sorting otherwise
41+
});
42+
43+
model.setCurrentSearch(sortedResults.map(r => r.item));
3344
}
3445
}, 500), []);
3546

0 commit comments

Comments
 (0)