Skip to content

Commit

Permalink
Add notifs for major changes when logging in
Browse files Browse the repository at this point in the history
Also got rid of a console log lol
  • Loading branch information
mjaydenkim committed Jan 17, 2025
1 parent 03e7824 commit 99b596b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
22 changes: 21 additions & 1 deletion client/src/modules/Profile/Component/Profile.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -29,6 +29,7 @@ const Profile = () => {
const [pendingReviews, setPendingReviews] = useState<ReviewType[]>([]);
const [approvedReviews, setApprovedReviews] = useState<ReviewType[]>([]);

const savedSessionReview = useRef<NewReview | null>(null);
const [upvoteCount, setUpvoteCount] = useState(0);

const { isLoggedIn, token, netId, isAuthenticating, signOut } =
Expand Down Expand Up @@ -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]);

Expand Down Expand Up @@ -186,6 +205,7 @@ const Profile = () => {
sessionCourseId !== '' &&
isLoggedIn
) {
savedSessionReview.current = sessionReview;
submitReview(sessionReview, sessionCourseId);
}
}, [isLoggedIn, token]);
Expand Down
1 change: 0 additions & 1 deletion client/src/modules/Profile/Component/UserInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type UserInfoProps = {
reviewsTotal: number;
upvoteCount: number;
netId: string;
majors: string[];
signOut: () => void;
}

Expand Down
4 changes: 1 addition & 3 deletions server/src/profile/profile.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion server/src/profile/profile.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down

0 comments on commit 99b596b

Please sign in to comment.