Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anonymous warning modal #458

Merged
merged 15 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added client/src/assets/img/anonymous.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions client/src/modules/Course/Components/AnonymousWarning.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React from 'react'

import styles from '../Styles/AnonymousWarning.module.css'

import Anonymous from '../../../assets/img/anonymous.png'
import { useAuthOptionalLogin } from '../../../auth/auth_utils'
import { useHistory } from 'react-router-dom'

type Props = {
open: boolean
}

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

if (!open) {
return <></>
}

if (isLoggedIn) {
return (
<div className={styles.warningContainer}>
<div className={styles.anonymousLogo}>
<img src={Anonymous} className="anonymous-logo" alt="anonymous logo" />
</div>
<div className={styles.message}>
<span className="line1">Don't worry - all your reviews are<br /></span> <span className={styles.line2}> anonymous!</span>
</div>
<button
className={`${styles.button}`}
onClick={() => history.push('/profile')}
>
Submit Review
</button>
<div className={styles.pastReviewsMessage}>
You will be redirected to your past reviews page
</div>
</div>
)
}
/**
* Anonymous warning modal that is rendered when the user is not signed in
*/
else {
return (
<div className={styles.warningContainer}>
<div className={styles.anonymousLogo}>
<img src={Anonymous} className="anonymous-logo" alt="anonymous logo" />
</div>
<div className={styles.message}>
<span className="line1">Login to submit - all your reviews are<br /></span> <span className={styles.line2}> anonymous!</span>
</div>
<button
className={`${styles.button}`}
onClick={() => signIn('profile')}
>
Login
</button>
<div className={styles.pastReviewsMessage}>
You will be redirected to your past reviews page
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is a bug on my end, but after redirecting to the profile, the submitted reviews don't show up in the main panel (but the number is updated on the left side):
IMG_7752

</div>
</div>
)
}
}

export default AnonymousWarning
37 changes: 20 additions & 17 deletions client/src/modules/Course/Components/Course.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import { Class, Review } from 'common'
import { Session } from '../../../session-store'

import { useAuthOptionalLogin } from '../../../auth/auth_utils'

Check warning on line 25 in client/src/modules/Course/Components/Course.tsx

View workflow job for this annotation

GitHub Actions / build

'useAuthOptionalLogin' is defined but never used

import ReviewModal from './ReviewModal'

Expand All @@ -40,8 +40,6 @@
const [pageStatus, setPageStatus] = useState<PageStatus>(PageStatus.Loading)
const [scrolled, setScrolled] = useState(false)

const { isLoggedIn, token, signIn } = useAuthOptionalLogin()

/**
* Arrow functions for sorting reviews
*/
Expand Down Expand Up @@ -154,6 +152,23 @@
setCourseReviews(currentReviews)
}

/**
* Attempts to report review, and filters out the reported review locally
* @param reviewId - id of review to report
*/
async function reportReview(reviewId: string) {
try {
const response = await axios.post('/api/reviews/report', { id: reviewId })
if (response.status === 200) {
setCourseReviews(
courseReviews?.filter((element) => element._id !== reviewId)
)
}
} catch (e) {
toast.error('Failed to report review.')
}
}

/**
* Save review information to session storage and begin redirect to auth
*/
Expand All @@ -166,19 +181,7 @@
})
Session.setPersistent({ review_num: selectedClass?.classNum })
Session.setPersistent({ courseId: selectedClass?._id })

signIn('course')
}

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

/** Modal Open and Close Logic */
const [open, setOpen] = useState<boolean>(false)
Expand Down Expand Up @@ -304,7 +307,7 @@

<ReviewModal
open={open}
setOpen={setOpen}
setReviewOpen={setOpen}
submitReview={onSubmitReview}
professorOptions={
selectedClass.classProfessors ? selectedClass.classProfessors : []
Expand All @@ -315,4 +318,4 @@
}

return <Loading />
}
}
61 changes: 53 additions & 8 deletions client/src/modules/Course/Components/ReviewModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useEffect, useState } from 'react'
import axios from 'axios'

import MultiSelect from './MultiSelect'
import SingleSelect from './SingleSelect'
Expand All @@ -10,16 +11,18 @@ import closeIcon from '../../../assets/icons/X.svg'

// Data
import majors from '../../Globals/majors'
import AnonymousWarning from './AnonymousWarning'
import { useAuthOptionalLogin } from '../../../auth/auth_utils'

const ReviewModal = ({
open,
setOpen,
setReviewOpen,
submitReview,
professorOptions,
}: Modal) => {
// Modal Logic
function closeModal() {
setOpen(false)
setReviewOpen(false)
}
// Content & Options
const placeholdertext =
Expand Down Expand Up @@ -53,6 +56,11 @@ const ReviewModal = ({
const [difficulty, setDifficulty] = useState<number>(3)
const [workload, setWorkload] = useState<number>(3)

const [anonymousOpen, setAnonymousOpen] = useState<boolean>(false)
const [noReviews, setNoReviews] = useState<boolean>(false)

const {isLoggedIn, netId, signIn} = useAuthOptionalLogin()

const [valid, setValid] = useState<Valid>({
professor: false,
major: false,
Expand All @@ -66,8 +74,23 @@ const ReviewModal = ({
}, [professorOptions])
useEffect(() => {
setAllowSubmit(valid.professor && valid.major && valid.grade && valid.text)
if (isLoggedIn) {getNoReviews()}
}, [valid])

/**
* Determines if the current user has no reviews, so they should receive
* the anonymous modal
*/
async function getNoReviews() {
qiandrewj marked this conversation as resolved.
Show resolved Hide resolved
const response = await axios.post('/api/profiles/count-reviews', {
netId,
})
const res = response.data
if (response.status === 200) {
setNoReviews(res.result === 0)
}
}

function onProfessorChange(newSelectedProfessors: string[]) {
setSelectedProfessors(newSelectedProfessors)
if (newSelectedProfessors.length > 0)
Expand Down Expand Up @@ -101,8 +124,8 @@ const ReviewModal = ({
return false
}

// Called by onSubmitReview if the user should not see anonymous
function handleSubmitReview() {
console.log('tried to submit')
if (validReview()) {
const newReview: NewReview = {
rating: overall,
Expand All @@ -115,9 +138,31 @@ const ReviewModal = ({
major: selectedMajors,
}
submitReview(newReview)
}
}

console.log('Submitting')
} else return
// Handle click of submit button
function onSubmitReview() {
if (!noReviews && isLoggedIn) {
handleSubmitReview()
signIn('profile')
} else {
handleSubmitReview()
setAnonymousOpen(true)
setReviewOpen(false)
}
}

if (!open && anonymousOpen) {
return (
<div className = {styles.modalbg}>
<div className = {styles.modal}>
<AnonymousWarning
open = {anonymousOpen}
/>
</div>
</div>
)
}

if (!open) {
Expand Down Expand Up @@ -203,7 +248,7 @@ const ReviewModal = ({
></textarea>
<button
className={styles.submitbutton}
onClick={handleSubmitReview}
onClick={() => {onSubmitReview()}}
disabled={!allowSubmit}
>
Submit Review
Expand All @@ -217,7 +262,7 @@ const ReviewModal = ({

type Modal = {
open: boolean
setOpen: (open: boolean) => void
setReviewOpen: (open: boolean) => void
submitReview: (review: NewReview) => void
professorOptions: string[]
}
Expand All @@ -240,4 +285,4 @@ type Valid = {
text: boolean
}

export default ReviewModal
export default ReviewModal
60 changes: 60 additions & 0 deletions client/src/modules/Course/Styles/AnonymousWarning.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
.warningContainer {


display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
background: white;
padding: 50px;
border-radius: 8px;
}

.anonymousLogo {
max-width: 25%;
max-height: 25%;
margin: auto;
}

.message {
text-align: center;
margin-top: 30px;

font-family: Source Sans Pro;
font-style: normal;
font-size: 24px;
font-weight: 300;
line-height: normal;
text-align: center;

color: #000000;
}

.line2 {
font-weight: 600;
}

.button {
background-color: #0076ff;
color: #ffffff;
width: 50%;
border-radius: 10px;
margin-top: 20px;
padding: 11px 10px;
text-align: center;
font-family: Source Sans Pro;
font-size: 18px;
font-style: normal;
line-height: normal;
cursor: pointer;
}

.pastReviewsMessage {
margin-top: 10px;
margin-left: 15px;
font-family: 'Source Sans Pro', sans-serif;
font-size: 12px;
color: #A1A1A1;
font-weight: var(--regular-weight);
}
2 changes: 1 addition & 1 deletion client/src/modules/Globals/ProfileDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function ProfileDropdown({
type="button"
className={`${styles.signinbutton}`}
onClick={() => {
signIn('home')
signIn('profile')
}}
>
Login
Expand Down
Loading
Loading