Skip to content

Commit 210b196

Browse files
authored
Sort by rating (#129)
* 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 * git stash fight * searchbar escape char bug fix * added srot by rating --------- Co-authored-by: Kacper Lisik <lisik@kth.se>
1 parent 3822682 commit 210b196

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

my-app/src/model.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const model = {
1919
locations: [],
2020
// indexes: 0 -> overall rating; 1 -> difficulty; 2->teacher rating
2121
avgRatings: [],
22+
// model.avgRatings["IK1203"][0]
2223
/* courses the user selected as their favourite */
2324
favourites: [],
2425
searchHistory:[],

my-app/src/presenters/ListViewPresenter.jsx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import CoursePagePopup from '../views/Components/CoursePagePopup.jsx';
66
import PrerequisitePresenter from './PrerequisitePresenter.jsx';
77
import {ReviewPresenter} from "./ReviewPresenter.jsx"
88
import {syncScrollPositionToFirebase} from "../../firebase.js"
9+
import { model } from '../model.js';
910

1011
const ListViewPresenter = observer(({ model }) => {
1112
const scrollContainerRef = useRef(null);
@@ -91,6 +92,52 @@ const ListViewPresenter = observer(({ model }) => {
9192
case 'credits':
9293
return sortedCourses.sort((a, b) =>
9394
direction * (parseFloat(a.credits) - parseFloat(b.credits)));
95+
96+
97+
// indexes: 0 -> overall rating; 1 -> difficulty; 2->teacher rating
98+
case 'avg_rating':
99+
return sortedCourses.sort((a, b) => {
100+
let aScore = 0;
101+
let bScore = 0;
102+
103+
if (model.avgRatings[a.code] != null && model.avgRatings[a.code][0] != null) {
104+
aScore = model.avgRatings[a.code][0] + 10; // +10 so courses with no reviews are shown before those with bad reviews (0/5 stars)
105+
}
106+
107+
if (model.avgRatings[b.code] != null && model.avgRatings[b.code][0] != null) {
108+
bScore = model.avgRatings[b.code][0] + 10;
109+
}
110+
return direction * (aScore - bScore);
111+
});
112+
case 'diff_rating':
113+
return sortedCourses.sort((a, b) => {
114+
let aScore = 0;
115+
let bScore = 0;
116+
117+
if (model.avgRatings[a.code] != null && model.avgRatings[a.code][1] != null) {
118+
aScore = model.avgRatings[a.code][1] + 10; // +10 so courses with no reviews are shown before those with bad reviews (0/5 stars)
119+
}
120+
121+
if (model.avgRatings[b.code] != null && model.avgRatings[b.code][1] != null) {
122+
bScore = model.avgRatings[b.code][1] + 10;
123+
}
124+
125+
return direction * (aScore - bScore);
126+
});
127+
case 'teacher_rating':
128+
return sortedCourses.sort((a, b) => {
129+
let aScore = 0;
130+
let bScore = 0;
131+
132+
if (model.avgRatings[a.code] != null && model.avgRatings[a.code][2] != null) {
133+
aScore = model.avgRatings[a.code][2] + 10; // +10 so courses with no reviews are shown before those with bad reviews (0/5 stars)
134+
}
135+
136+
if (model.avgRatings[b.code] != null && model.avgRatings[b.code][2] != null) {
137+
bScore = model.avgRatings[b.code][2] + 10;
138+
}
139+
return direction * (aScore - bScore);
140+
});
94141
case 'relevance':
95142
return direction === -1 ? sortedCourses : sortedCourses.reverse();
96143
default:
@@ -183,4 +230,4 @@ const ListViewPresenter = observer(({ model }) => {
183230
/>;
184231
});
185232

186-
export { ListViewPresenter };
233+
export { ListViewPresenter };

my-app/src/views/ListView.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ function ListView(props) {
134134
<option value="relevance">Sort by Relevance</option>
135135
<option value="name">Sort by Name</option>
136136
<option value="credits">Sort by Credits</option>
137+
<option value="avg_rating">Sort by Overall Rating</option>
138+
<option value="diff_rating">Sort by Difficulty Rating</option>
139+
<option value="teacher_rating">Sort by Professor Rating</option>
137140
</select>
138141

139142
<button

0 commit comments

Comments
 (0)