Skip to content

Commit c89f3f3

Browse files
authored
Search bar bug fix (#127)
* 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 --------- Co-authored-by: Kacper Lisik <lisik@kth.se>
1 parent fb2ec83 commit c89f3f3

4 files changed

Lines changed: 32 additions & 8 deletions

File tree

my-app/src/model.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { query } from "firebase/database";
12
import { addCourse, addReviewForCourse, getReviewsForCourse, uploadDepartmentsAndLocations } from "../firebase";
23

34

@@ -52,6 +53,7 @@ export const model = {
5253
},
5354
isPopupOpen: false,
5455
selectedCourse: null,
56+
searchQueryModel: "",
5557

5658
_coursesListeners: [], // internal list of listeners
5759

my-app/src/presenters/ListViewPresenter.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ const ListViewPresenter = observer(({ model }) => {
168168
setPopupOpen={(isOpen) => model.setPopupOpen(isOpen)}
169169
setSelectedCourse={(course) => model.setSelectedCourse(course)}
170170
popup={popup}
171-
171+
query={model.searchQueryModel}
172172
targetScroll={model.scrollPosition}
173173
setTargetScroll={setTargetScroll}
174174
scrollContainerRef={scrollContainerRef}

my-app/src/presenters/SearchbarPresenter.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const SearchbarPresenter = observer(({ model }) => {
1717
{ name: 'name', weight: 0.3 },
1818
{ name: 'description', weight: 0.1 },
1919
],
20-
threshold: 0.4, // adjust this for sensitivity
20+
threshold: 0.3141592653589793238, // adjust this for sensitivity
2121
ignoreLocation: true,
2222
minMatchCharLength: 2,
2323
};
@@ -41,6 +41,7 @@ const SearchbarPresenter = observer(({ model }) => {
4141
});
4242

4343
model.setCurrentSearch(sortedResults.map(r => r.item));
44+
model.searchQueryModel = query;
4445
}
4546
}, 500), []);
4647

my-app/src/views/ListView.jsx

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import 'ldrs/react/Quantum.css';
44
import InfiniteScroll from 'react-infinite-scroll-component';
55
import { useNavigate, Link } from 'react-router-dom';
66

7+
// Add this helper function at the top of your component
8+
const highlightText = (text, query) => {
9+
if (!query || !text) return text;
10+
11+
// Escape special regex characters in the query
12+
const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
13+
// const escapedQuery = query; //for testing purposes
14+
const regex = new RegExp(`(${escapedQuery})`, 'gi');
15+
return text.replace(regex, '<u>$1</u>');
16+
};
17+
718
function ListView(props) {
819
const [displayedCourses, setDisplayedCourses] = useState([]);
920
const [hasMore, setHasMore] = useState(true);
@@ -169,17 +180,27 @@ function ListView(props) {
169180
>
170181
<div>
171182
<div className="codeNameContainer" style={{ display: 'flex' }}>
172-
<p className="font-bold text-[#000061]">{course.code}</p>
173-
<p className="font-bold text-[#000061]" style={{color: "GrayText", opacity: 0.5, marginLeft: "0.5em"}}>{handlePeriods(course?.periods)}</p>
183+
<p className="font-bold text-[#000061]"
184+
dangerouslySetInnerHTML={{
185+
__html: highlightText(course.code, props.query)
186+
}}
187+
/>
188+
<p className="font-bold text-[#000061]"
189+
style={{color: "GrayText", opacity: 0.5, marginLeft: "0.5em"}}>
190+
{handlePeriods(course?.periods)}
191+
</p>
174192
</div>
175-
<p className="font-bold">{course.name} </p>
193+
<p className="font-bold"
194+
dangerouslySetInnerHTML={{
195+
__html: highlightText(course.name, props.query)
196+
}}
197+
/>
176198
<p
177199
className="text-gray-600"
178200
dangerouslySetInnerHTML={{
179201
__html: readMore[course.code]
180-
181-
? course?.description
182-
: (course?.description?.slice(0, 200)+"..."),
202+
? highlightText(course?.description, props.query)
203+
: highlightText(course?.description?.slice(0, 200) + "...", props.query)
183204
}}
184205
/>
185206
{course?.description?.length > 150 && (

0 commit comments

Comments
 (0)