diff --git a/client/src/modules/Results/Components/PreviewCard.tsx b/client/src/modules/Results/Components/PreviewCard.tsx index 5d97e91f..0a6db885 100644 --- a/client/src/modules/Results/Components/PreviewCard.tsx +++ b/client/src/modules/Results/Components/PreviewCard.tsx @@ -18,46 +18,48 @@ export const PreviewCard = ({ course }: PreviewCardProps) => { const [rating, setRating] = useState('-'); const [diff, setDiff] = useState('-'); const [workload, setWorkload] = useState('-'); - const [topReview, setTopReview] = useState(); + const [topReview, setTopReview] = useState(null); const [numReviews, setNumReviews] = useState(0); const [topReviewLikes, setTopReviewLikes] = useState(0); const [offered, setOffered] = useState(''); + const [loading, setLoading] = useState(true); useEffect(() => { - if (course) { - updateGauges(); - } + setLoading(true); + updateCourseInfo(); + // only set loading to false when top review information has been obtained + setLoading(topReview === null); + }, []); + + useEffect(() => { + updateCourseInfo(); }, [course]); - const updateGauges = () => { + const updateCourseInfo = () => { if (course) { setId(course._id); setRating(course.classRating ? String(course.classRating) : '-'); setDiff(course.classDifficulty ? String(course.classDifficulty) : '-'); setWorkload(course.classWorkload ? String(course.classWorkload) : '-'); - updateTopReview(); + + axios.post(`/api/courses/get-reviews`, { courseId: course ? course._id : id }) + .then((response) => { + const reviews = response.data.result; + if (reviews && reviews.length > 0) { + reviews.sort((a: ReviewType, b: ReviewType) => + (a.likes || 0) < (b.likes || 0) ? 1 : -1 + ); + setTopReview(reviews[0]); + setTopReviewLikes(reviews[0].likes || 0); + setNumReviews(reviews.length); + setOffered(lastOfferedSems(course)); + } else { + setTopReview({}); + setNumReviews(0); + } + }); } - }; - - const updateTopReview = () => { - axios - .post(`/api/courses/get-reviews`, { courseId: course ? course._id : id }) - .then((response) => { - const reviews = response.data.result; - if (reviews && reviews.length > 0) { - reviews.sort((a: ReviewType, b: ReviewType) => - (a.likes || 0) < (b.likes || 0) ? 1 : -1 - ); - setTopReview(reviews[0]); - setTopReviewLikes(reviews[0].likes || 0); - setNumReviews(reviews.length); - setOffered(lastOfferedSems(course)); - } else { - setTopReview({}); - setNumReviews(0); - } - }); - }; + } if (!course) { // Return fallback if course is undefined @@ -113,7 +115,7 @@ export const PreviewCard = ({ course }: PreviewCardProps) => { )} - {numReviews === 0 && topReview !== undefined && ( + {numReviews === 0 && !loading && ( <> Bear Icon
diff --git a/client/src/modules/Results/Components/ResultsDisplay.tsx b/client/src/modules/Results/Components/ResultsDisplay.tsx index 36df4793..f24b68bd 100644 --- a/client/src/modules/Results/Components/ResultsDisplay.tsx +++ b/client/src/modules/Results/Components/ResultsDisplay.tsx @@ -263,13 +263,13 @@ export const ResultsDisplay = ({ return (
- {loading && } + {loading && } {/* Case where results are returned, even if zero */} {!loading && (
{/* Case where no results returned */}
-

Search Results

+ {courseList.length !== 0 &&

Search Results

} {/* setSearchListViewEnabled(!searchListViewEnabled)}*/} @@ -277,24 +277,27 @@ export const ResultsDisplay = ({ {/* {searchListViewEnabled ? 'Hide' : 'View'} Search Results*/} {/**/}
-
-
Filter
-
-
Semester
- {renderCheckboxes('semesters')} + {courseList.length !== 0 && ( +
+
Filter
+
+
Semester
+ {renderCheckboxes('semesters')} +
+
+
Level
+ {renderCheckboxes('levels')} +
-
-
Level
- {renderCheckboxes('levels')} -
-
+ )} {filteredItems.length !== 0 && (
{searchListViewEnabled && ( <>
- We found {filteredItems.length} courses for " + We found {filteredItems.length} courses for + " {userInput} "
@@ -339,9 +342,9 @@ export const ResultsDisplay = ({
)} -
+
)} - {filteredItems.length === 0 && ( + {filteredItems.length === 0 && courseList.length !== 0 && (
{searchListViewEnabled && ( <> @@ -372,8 +375,11 @@ export const ResultsDisplay = ({ {filteredItems.length === 0 && (
- Bear Icon -
No classes found. Try searching something else or switching up the filters!
+ Bear Icon +
+ No classes found. Try searching something else + {courseList.length !== 0 ? " or switching up the filters!" : "!"} +
{/*