From 99b596bddd8f789ce6727c7d59330c2c63f4085b Mon Sep 17 00:00:00 2001 From: mjaydenkim Date: Fri, 17 Jan 2025 03:09:15 -0500 Subject: [PATCH] Add notifs for major changes when logging in Also got rid of a console log lol --- .../src/modules/Profile/Component/Profile.tsx | 22 ++++++++++++++++++- .../modules/Profile/Component/UserInfo.tsx | 1 - server/src/profile/profile.controller.ts | 4 +--- server/src/profile/profile.router.ts | 1 - 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/client/src/modules/Profile/Component/Profile.tsx b/client/src/modules/Profile/Component/Profile.tsx index 0115a039..e2223837 100644 --- a/client/src/modules/Profile/Component/Profile.tsx +++ b/client/src/modules/Profile/Component/Profile.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useRef, useState } from 'react'; import { Redirect } from 'react-router-dom'; import axios from 'axios'; @@ -29,6 +29,7 @@ const Profile = () => { const [pendingReviews, setPendingReviews] = useState([]); const [approvedReviews, setApprovedReviews] = useState([]); + const savedSessionReview = useRef(null); const [upvoteCount, setUpvoteCount] = useState(0); const { isLoggedIn, token, netId, isAuthenticating, signOut } = @@ -128,10 +129,28 @@ const Profile = () => { } } + const checkForUpdatedMajor = async (review: NewReview) => { + const getMajorsReq = await axios.post('/api/profiles/get-majors', { netId }) + if (getMajorsReq.status === 200) { + const oldMajors = getMajorsReq.data.majors + if (oldMajors && review && + JSON.stringify(oldMajors) !== JSON.stringify(review.major)) { + toast.info('Your major has been changed ' + + (oldMajors.length !== 0 && 'from ') + + oldMajors.join(',') + ' to ' + + review.major.join(', ').replace(/(, )(?!.*\1)/, + (review.major.length > 2 ? ', and ' : ' and ') + ) + "." + ) + } + } + } + // Only update reviews if we have a given user's netId + they are no longer authenticating. if (netId && !isAuthenticating) { getReviews(); getReviewsHelpful(); + if (savedSessionReview.current) checkForUpdatedMajor(savedSessionReview.current) } }, [netId, isAuthenticating]); @@ -186,6 +205,7 @@ const Profile = () => { sessionCourseId !== '' && isLoggedIn ) { + savedSessionReview.current = sessionReview; submitReview(sessionReview, sessionCourseId); } }, [isLoggedIn, token]); diff --git a/client/src/modules/Profile/Component/UserInfo.tsx b/client/src/modules/Profile/Component/UserInfo.tsx index 406d56a5..1d6010d5 100644 --- a/client/src/modules/Profile/Component/UserInfo.tsx +++ b/client/src/modules/Profile/Component/UserInfo.tsx @@ -11,7 +11,6 @@ type UserInfoProps = { reviewsTotal: number; upvoteCount: number; netId: string; - majors: string[]; signOut: () => void; } diff --git a/server/src/profile/profile.controller.ts b/server/src/profile/profile.controller.ts index f3f20304..00cf27fb 100644 --- a/server/src/profile/profile.controller.ts +++ b/server/src/profile/profile.controller.ts @@ -41,9 +41,7 @@ export const getStudentReviewDocs = async ({ netId }: ProfileInfoRequestType) => return reviews.filter((review) => review !== null); }; -export const getStudentMajors = async ({ - netId - }: ProfileInfoRequestType) => { +export const getStudentMajors = async ({ netId }: ProfileInfoRequestType) => { const student = await findStudent(netId); if (!student) { diff --git a/server/src/profile/profile.router.ts b/server/src/profile/profile.router.ts index 8b05e0f6..8b9be9cc 100644 --- a/server/src/profile/profile.router.ts +++ b/server/src/profile/profile.router.ts @@ -120,7 +120,6 @@ profileRouter.post('/get-majors', async (req, res) => { */ profileRouter.post('/set-majors', async (req, res) => { try { - console.log(req.body) const { netId, majors }: ProfileMajorPostType = req.body; const profile: Profile = new Profile({ netId });