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

init #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

init #18

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
21 changes: 14 additions & 7 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,22 @@ model CollegeCampus {
}

model Lecturer {
id String @id @unique
id String @id @unique
name String
rating Float @default(0)
personalSideRating Int @default(0)
scientificSideRating Int @default(0)
recommendationRating Int @default(0)
rating Float @default(0)
personalSideRating Int @default(0)
scientificSideRating Int @default(0)
recommendationRating Int @default(0)
role String
amountOfReviews Int @default(0)
achievements Achievement[]
amountOfReviews Int @default(0)

oneStar Int? @default(0)
twoStar Int? @default(0)
threeStar Int? @default(0)
fourStar Int? @default(0)
fiveStar Int? @default(0)

achievements Achievement[]

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down
12 changes: 4 additions & 8 deletions src/app/lecturer/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,16 @@ export default function Page({ params }: { params: { id: string } }): JSX.Elemen
const { data: lecturer = {}, error, isLoading, mutate, isValidating } = useSWR(`/api/lecturer?id=${params.id}`, fetcher)
const {
name,
hasReviews,
amountOfReviews,
role,
achievements,
contacts,
img,
rating,
personalSideRating,
scientificSideRating,
recommendationRating,
teachCourses,
createdAt,
workInColleges,
workInCampus
workInCampus,
ratings
} = useMemo(() => lecturer ?? {} as lecturer, [lecturer])

if (isLoading) return <Loading />
Expand All @@ -108,14 +104,14 @@ export default function Page({ params }: { params: { id: string } }): JSX.Elemen
amountOfReviews={amountOfReviews}
achievements={achievements}
contacts={contacts}
hasReviews={hasReviews}
ratings={ratings}
teachCourses={teachCourses}
role={role}
/>

<div className="divider px-4" />

<Reviews hasReviews={hasReviews} mutate={mutate} />
<Reviews paramsId={params.id} />
<ReviewModal name={name} id={params.id} mutate={mutate} lecturer={lecturer} />

</div>
Expand Down
62 changes: 20 additions & 42 deletions src/components/Lecturer/Ratings.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'

export const Ratings = ({ hasReviews, amountOfReviews = hasReviews?.length, rating }: { hasReviews: any, amountOfReviews?: number, rating: number }) => {
export const Ratings = ({ ratings, amountOfReviews, rating }: { ratings: any, amountOfReviews?: number, rating: number }) => {
return (
<div className="bg-base-300 rounded-lg p-4">
<h2 className="card-title mb-3">Ratings</h2>
Expand Down Expand Up @@ -28,55 +28,33 @@ export const Ratings = ({ hasReviews, amountOfReviews = hasReviews?.length, rati


{
[...Array(5)].map((_, i) => (
hasReviews?.length === 0 ?
<div
key={i}
className="flex items-center justify-center space-x-4">

<span
className="text-sm font-medium text-blue-600 dark:text-blue-500 inline-block">
{5 - i} star
</span>
<div
className="h-5 flex-1 bg-gray-200 rounded dark:bg-gray-700">

<div
className="h-5 bg-purple-700 rounded"
style={{ width: `0%` }} />

</div>
ratings &&
[...ratings].map((_, i) => (
<div
key={i}
className="flex items-center justify-center space-x-4">

<span
className="text-sm w-9 font-medium text-blue-600 dark:text-blue-500">
0%
</span>
<span
className="text-sm font-medium text-blue-600 dark:text-blue-500 inline-block">
{5 - i} star
</span>

</div>
:
<div
key={i}
className="flex items-center justify-center space-x-4">
className="h-5 flex-1 bg-gray-200 rounded dark:bg-gray-700">

<span
className="text-sm font-medium text-blue-600 dark:text-blue-500 inline-block">
{5 - i} star
</span>
<div
className="h-5 flex-1 bg-gray-200 rounded dark:bg-gray-700">

<div
className="h-5 bg-purple-700 rounded"
style={{ width: `${hasReviews?.filter((review: any) => review.rating === 5 - i)?.length / hasReviews?.length * 100}%` }} />
className="h-5 bg-purple-700 rounded"
style={{ width: `${ratings[i].count / amountOfReviews * 100}%` }}
/>

</div>
</div>

<span
className="text-sm w-9 font-medium text-blue-600 dark:text-blue-500">
{Math.round(hasReviews?.filter((review: any) => review.rating === 5 - i)?.length / hasReviews?.length * 100)}%
</span>
<span
className="text-sm w-9 font-medium text-blue-600 dark:text-blue-500">
{Math.round(ratings[i].count / amountOfReviews * 100)}%
</span>

</div>
</div>
))

}
Expand Down
10 changes: 7 additions & 3 deletions src/components/Lecturer/Reviews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import React, { memo } from 'react'
import TimeAgo from 'javascript-time-ago'
import en from 'javascript-time-ago/locale/en'
import ReviewCommentTemplete from '../Shared/ReviewCommentTemplete'
import useSWR from 'swr'

TimeAgo.addDefaultLocale(en)
const timeAgo = new TimeAgo('en-US')
const fetcher = (url: URL) => fetch(url).then((res) => res.json())

export const Reviews = memo(({ hasReviews, mutate }: { hasReviews: any, mutate?: any }) => {
export const Reviews = memo(({ paramsId }: { paramsId: string }) => {
const { data: reviews, error, isLoading, mutate, isValidating } = useSWR(`/api/review?lecturerId=${paramsId}`, fetcher)

if (isLoading) return <div>Loading...</div>
return (
<section id='style-1' className="bg-base-300 rounded-lg flex-1">
<div className='sm:p-4 p-2 '>
Expand All @@ -17,8 +21,8 @@ export const Reviews = memo(({ hasReviews, mutate }: { hasReviews: any, mutate?:
<div className='overflow-y-auto max-h-96 min-h-16 sm:px-4 px-2'>
{

hasReviews[0] ? (
hasReviews.map(({ id, avatar, author, comment, rating, personalSideRating, scientificSideRating, recommendationRating, createdAt, score }) => (
reviews[0] ? (
reviews.map(({ id, avatar, author, comment, rating, personalSideRating, scientificSideRating, recommendationRating, createdAt, score }) => (

<ReviewCommentTemplete
key={id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const DesktopUpperSection = (
contacts,
achievements,
rating,
hasReviews,
ratings,
amountOfReviews,
teachCourses
}
Expand All @@ -36,7 +36,7 @@ const DesktopUpperSection = (

</div>

<Ratings rating={rating} hasReviews={hasReviews} amountOfReviews={amountOfReviews} />
<Ratings rating={rating} ratings={ratings} amountOfReviews={amountOfReviews} />
<Courses teachCourses={teachCourses} />

</section>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Shared/PageUpperSection/UpperSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const UpperSection = memo((
contacts,
achievements,
rating,
hasReviews,
ratings,
amountOfReviews,
teachCourses,
role
Expand Down Expand Up @@ -47,7 +47,7 @@ const UpperSection = memo((
<DesktopUpperSection
teachCourses={teachCourses ?? []}
contacts={contacts ?? []}
hasReviews={hasReviews ?? []}
ratings={ratings ?? []}
achievements={achievements ?? []}
amountOfReviews={amountOfReviews ?? 0}
rating={rating ?? 0} />
Expand Down
38 changes: 30 additions & 8 deletions src/pages/api/lecturer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,47 @@ const lecturersAPI = async (req: NextApiRequest, res: NextApiResponse) => {
name: true,
},
},
hasReviews: {

orderBy: {
createdAt: 'desc'
},
},
achievements: {
orderBy: {
gotAt: 'desc'
}

},
contacts: true,
}
})
const reviews = await prisma.review.findMany({
where: {
lecturerId: id as string,

}
})
res.status(200).json(lecturer)

const ratings = [
{ star: 5, count: 0 },
{ star: 4, count: 0 },
{ star: 3, count: 0 },
{ star: 2, count: 0 },
{ star: 1, count: 0 },
]
ratings.forEach((rating) => {
reviews.forEach((review) => {
if (review.rating === rating.star) {
rating.count += 1
}
})
})

const result = {
...lecturer,
'totalRatings': reviews.length,
ratings,
}

res.status(200).json(result)
}
catch (error) {
res.status(500).json({ message: error })
console.log(error)
}

break
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/review/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const reviewsAPI = async (req: NextApiRequest, res: NextApiResponse) => {
data: {
avatar: avatar as string,
author: author as string,
rating: Math.round((Number(personalSideRating) + Number(scientificSideRating) + Number(recommendationRating)) / 3),
rating: Math.round(((Number(personalSideRating) + Number(scientificSideRating) + Number(recommendationRating)) / 3) * 10) / 10,
comment: comment as string,
personalSideRating: Number(personalSideRating),
scientificSideRating: Number(scientificSideRating),
Expand Down Expand Up @@ -103,7 +103,7 @@ const reviewsAPI = async (req: NextApiRequest, res: NextApiResponse) => {
id: lecturerId as string
},
data: {
rating: Math.round(avgRatings._avg.rating),
rating: Math.round((avgRatings._avg.rating) * 10) / 10,
personalSideRating: Math.round(avgRatings._avg.personalSideRating),
scientificSideRating: Math.round(avgRatings._avg.scientificSideRating),
recommendationRating: Math.round(avgRatings._avg.recommendationRating),
Expand Down