Skip to content

Commit

Permalink
review gets submitted from profile page after being redirected by ano…
Browse files Browse the repository at this point in the history
…nymous modal
  • Loading branch information
qiandrewj committed Sep 26, 2024
1 parent 0bf521e commit 38ef646
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
4 changes: 1 addition & 3 deletions client/src/modules/Course/Components/AnonymousWarning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import { useHistory } from 'react-router-dom'

type Props = {
open: boolean
validReview: boolean
review: NewReview
}

const AnonymousWarning = ({ open, validReview, review }: Props) => {
const AnonymousWarning = ({ open }: Props) => {
const {isLoggedIn, signIn} = useAuthOptionalLogin()
const history = useHistory()

Expand Down
16 changes: 0 additions & 16 deletions client/src/modules/Course/Components/ReviewModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,27 +154,11 @@ const ReviewModal = ({
}

if (!open && anonymousOpen) {

// Handling the new review to be passed into the anonymous warning
const valid = validReview()
const newReview: NewReview = {
rating: overall,
difficulty: difficulty,
workload: workload,
professors: selectedProfessors,
text: reviewText,
isCovid: false,
grade: selectedGrade,
major: selectedMajors,
}

return (
<div className = {styles.modalbg}>
<div className = {styles.modal}>
<AnonymousWarning
open = {anonymousOpen}
validReview = {valid}
review = {newReview}
/>
</div>
</div>
Expand Down
58 changes: 58 additions & 0 deletions client/src/modules/Profile/Component/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import React, { useEffect, useState } from 'react'
import { Redirect } from 'react-router-dom'

import axios from 'axios'
import { toast, ToastContainer } from 'react-toastify'

Check warning on line 5 in client/src/modules/Profile/Component/Profile.tsx

View workflow job for this annotation

GitHub Actions / build

'ToastContainer' is defined but never used
import 'react-toastify/dist/ReactToastify.css'
import { Session } from '../../../session-store'

import { Review as ReviewType } from 'common'

Expand All @@ -11,10 +14,12 @@ import Loading from '../../Globals/Loading'
import { UserInfo } from './UserInfo'
import { NoReviews } from './NoReviews'
import { PendingReviews } from './PendingReviews'
import type { NewReview } from '../../../types'

import { useAuthMandatoryLogin } from '../../../auth/auth_utils'
import { randomPicture } from '../../Globals/profile_picture'


import styles from '../Styles/Profile.module.css'
import { PastReviews } from './PastReviews'

Expand Down Expand Up @@ -108,6 +113,59 @@ const Profile = () => {
setLoading(false)
}, [reviews])

/**
* Clear review stored in session storage
*/
function clearSessionReview() {
Session.setPersistent({ review: '' })
Session.setPersistent({ review_major: '' })
Session.setPersistent({ review_num: '' })
Session.setPersistent({ courseId: '' })
}

/**
* Checks if there is a review stored in Session (i.e. this redirected from
* auth)
*/
useEffect(() => {
/**
* Submit review and clear session storage
*/
async function submitReview(review: NewReview, courseId: string) {
try {
const response = await axios.post('/api/insertReview', {
token: token,
review: review,
courseId: courseId,
})

clearSessionReview()
if (response.status === 200) {
toast.success(
'Thanks for reviewing! New reviews are updated every 24 hours.'
)
} else {
toast.error('An error occurred, please try again.')
}
} catch (e) {
clearSessionReview()
toast.error('An error occurred, please try again.')
}
}

const sessionReview = Session.get('review')
const sessionCourseId = Session.get('courseId')
if (
sessionReview !== undefined &&
sessionReview !== '' &&
sessionCourseId !== undefined &&
sessionCourseId !== '' &&
isLoggedIn
) {
submitReview(sessionReview, sessionCourseId)
}
}, [isLoggedIn, token])

function sortReviewsBy(event: React.ChangeEvent<HTMLSelectElement>) {
const value = event.target.value
const currentReviews = reviews && [...reviews]
Expand Down

0 comments on commit 38ef646

Please sign in to comment.