From 25981332fe0a079e7cf90b8f04bdf164febb9867 Mon Sep 17 00:00:00 2001 From: ddusichka <78527291+ddusichka@users.noreply.github.com> Date: Tue, 3 Dec 2024 21:04:42 -0500 Subject: [PATCH] handle missing values better --- frontend/app/MediaReviewsPage.tsx | 197 +++++++++++++++--------------- 1 file changed, 98 insertions(+), 99 deletions(-) diff --git a/frontend/app/MediaReviewsPage.tsx b/frontend/app/MediaReviewsPage.tsx index d1000361..aa369d02 100644 --- a/frontend/app/MediaReviewsPage.tsx +++ b/frontend/app/MediaReviewsPage.tsx @@ -82,19 +82,21 @@ const MediaReviewsPage = () => { const reviews = response.data; setUserReviews(reviews); - // Calculate the average score - const totalScore = response.data.reduce( - (sum: any, review: { rating: any }) => sum + review.rating, - 0 - ); // Sum of all ratings - const averageScore = - reviews.length > 0 ? totalScore / reviews.length : 0; // Avoid division by 0 - // Update userScore in mediaStats - setMediaStats((prev) => ({ - ...prev, - userScore: averageScore, - userRatings: reviews.length, - })); + if (reviews) { + // Calculate the average score + const totalScore = reviews.reduce( + (sum: any, review: { rating: any }) => sum + review.rating, + 0 + ); // Sum of all ratings + const averageScore = + reviews.length > 0 ? totalScore / reviews.length : 0; // Avoid division by 0 + // Update userScore in mediaStats + setMediaStats((prev) => ({ + ...prev, + userScore: averageScore, + userRatings: reviews.length, + })); + } } catch (error) { console.error(error); } @@ -152,103 +154,100 @@ const MediaReviewsPage = () => { }; return ( - - - - - - - {mediaCover && ( - - )} - - - {selectedFilter === "you" && ( - - - {mediaStats.userScore.toFixed(1)} - - Your Avg Rating - - )} - {selectedFilter === "friend" && ( - - - {mediaStats.friendScore.toFixed(1)} - - Friend Rating - - )} - {selectedFilter === "all" && ( - - - {mediaStats.avgScore.toFixed(1)} - - Avg Rating - - )} - {selectedFilter === "you" && ( - <> - - {formatLargeNumber(mediaStats.userRatings)} - - Your Ratings - - )} - {selectedFilter === "friend" && ( - <> - - {formatLargeNumber(mediaStats.friendRatings)} - - Friends Ratings - - )} - {selectedFilter === "all" && ( - <> - - {formatLargeNumber(mediaStats.totalRatings)} - - Total Ratings - - )} - + + + + + + {mediaCover && ( + + )} - - + {selectedFilter === "you" && ( - - {userReviews.map((review, index) => { - return ; - })} + + + {mediaStats.userScore.toFixed(1)} + + Your Avg Rating )} {selectedFilter === "friend" && ( - - {friendsReviews && - friendsReviews.length > 0 && - friendsReviews.map((review, index) => { - return ; - })} + + + {mediaStats.friendScore.toFixed(1)} + + Friend Rating )} {selectedFilter === "all" && ( - - {allReviews.map((review, index) => { - return ; - })} + + {mediaStats.avgScore.toFixed(1)} + Avg Rating )} + {selectedFilter === "you" && ( + <> + + {formatLargeNumber(mediaStats.userRatings)} + + Your Ratings + + )} + {selectedFilter === "friend" && ( + <> + + {formatLargeNumber(mediaStats.friendRatings)} + + Friends Ratings + + )} + {selectedFilter === "all" && ( + <> + + {formatLargeNumber(mediaStats.totalRatings)} + + Total Ratings + + )} - - + + + + {selectedFilter === "you" && ( + + {userReviews && + userReviews.map((review, index) => { + return ; + })} + + )} + {selectedFilter === "friend" && ( + + {friendsReviews && + friendsReviews.map((review, index) => { + return ; + })} + + )} + {selectedFilter === "all" && ( + + {allReviews && + allReviews.map((review, index) => { + return ; + })} + + )} + + ); };