-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #270 from icssc/advay/show-users-reviews
Add page where users reviews are displayed
- Loading branch information
Showing
7 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.user-reviews-container { | ||
padding: 2rem; | ||
background-color: white; | ||
border-radius: var(--border-radius); | ||
} | ||
|
||
.user-reviews { | ||
background-color: white; | ||
border-radius: var(--border-radius); | ||
margin-bottom: 3vh; | ||
|
||
.user-reviews-footer { | ||
flex-direction: row; | ||
width: 100%; | ||
align-items: flex-end; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import axios, { AxiosResponse } from "axios"; | ||
import React, { FC, useEffect, useState } from "react"; | ||
import SubReview from '../../component/Review/SubReview'; | ||
import Button from 'react-bootstrap/Button'; | ||
import { Divider } from 'semantic-ui-react'; | ||
import { ReviewData } from "src/types/types"; | ||
import './UserReviews.scss'; | ||
import { useCookies } from "react-cookie"; | ||
|
||
const UserReviews: FC = () => { | ||
const [reviews, setReviews] = useState<ReviewData[]>([]); | ||
const [loaded, setLoaded] = useState<boolean>(false); | ||
const [cookies, setCookie] = useCookies(['user']); | ||
|
||
const getUserReviews = async () => { | ||
const response: AxiosResponse<ReviewData[]> = await axios.get(`/api/reviews?userID=${cookies.user.id}`); | ||
setReviews(response.data); | ||
setLoaded(true); | ||
} | ||
|
||
useEffect(() => { | ||
getUserReviews(); | ||
}, []); | ||
|
||
const deleteReview = async (reviewID: string) => { | ||
await axios.delete('/api/reviews', { data: { id: reviewID } }); | ||
setReviews(reviews.filter(review => review._id !== reviewID)); | ||
} | ||
|
||
if (!loaded) { | ||
return <p>Loading...</p>; | ||
} else if (reviews.length === 0) { | ||
return <p>No reviews to display at the moment.</p>; | ||
} else { | ||
return <div className='user-reviews-container'> | ||
<h1>Your Reviews</h1> | ||
<p>Deleting a review will remove it permanently.</p> | ||
{ | ||
reviews.map((review, i) => <div key={`user-reviews-${i}`} className='user-reviews'> | ||
<Divider /> | ||
<SubReview review={review}></SubReview> | ||
<div className='user-reviews-footer'> | ||
<Button variant='danger' className='mr-3' onClick={() => deleteReview(review._id!)}>Delete</Button> | ||
</div> | ||
</div>) | ||
} | ||
</div> | ||
} | ||
}; | ||
|
||
export default UserReviews; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React, { FC, useEffect, useState } from "react"; | ||
import UserReviews from '../../component/UserReviews/UserReviews'; | ||
import Error from '../../component/Error/Error'; | ||
import './ReviewsPage.scss'; | ||
import { useLocation } from "react-router-dom"; | ||
import { useCookies } from "react-cookie"; | ||
|
||
const ReviewsPage: FC = () => { | ||
const location = useLocation(); | ||
const [loaded, setLoaded] = useState<boolean>(false); | ||
const [cookies, setCookie] = useCookies(['user']); | ||
const [authorized, setAuthorized] = useState<boolean>(false); | ||
|
||
// user has to be logged in to view this page | ||
const checkLoggedIn = async () => { | ||
const loggedIn: boolean = cookies.hasOwnProperty('user'); | ||
setAuthorized(loggedIn); | ||
setLoaded(true); | ||
} | ||
|
||
useEffect(() => { | ||
checkLoggedIn(); | ||
}, []); | ||
|
||
if (!loaded) { | ||
return <p>Loading...</p>; | ||
} | ||
else if (!authorized) { | ||
return ( | ||
<Error message='Access Denied: Log in to view this page.'></Error> | ||
); | ||
} | ||
else { | ||
if (location.pathname.includes('reviews')) { | ||
return <UserReviews /> | ||
} | ||
} | ||
return <Error message='Invalid Reviews Page'></Error> | ||
}; | ||
|
||
export default ReviewsPage; |