Skip to content

Commit

Permalink
Merge pull request #33 from subinsk/courses-ui
Browse files Browse the repository at this point in the history
Courses UI Updated
  • Loading branch information
AnshumanDhiman authored Jan 15, 2023
2 parents 6546a28 + c47386c commit d5a4576
Show file tree
Hide file tree
Showing 20 changed files with 1,253 additions and 180 deletions.
30 changes: 30 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"postinstall": "npx prisma generate"
},
"dependencies": {
"@lottiefiles/react-lottie-player": "^3.5.0",
Expand All @@ -27,11 +28,13 @@
"react-dom": "18.2.0",
"react-hot-toast": "^2.4.0",
"react-icons": "^4.7.1",
"react-stars": "^2.2.5",
"react-table": "^7.8.0",
"typescript": "4.9.4"
},
"devDependencies": {
"@types/js-cookie": "^3.0.2",
"@types/react-stars": "^2.2.1",
"@types/react-table": "^7.7.12",
"autoprefixer": "^10.4.13",
"postcss": "^8.4.20",
Expand Down
54 changes: 37 additions & 17 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ datasource db {
}

model user {
user_id String @id @default(uuid())
email String @unique
name String
notes note[]
courses course[]
subjects subject[]
pyqs pyq[]
instructors instructor[]
note_upvotes note_upvote[]
course_upvotes course_upvote[]
pyq_upvotes pyq_upvote[]
user_id String @id @default(uuid())
email String @unique
name String
notes note[]
courses course[]
subjects subject[]
pyqs pyq[]
instructors instructor[]
note_upvotes note_upvote[]
course_reviews course_review[]
pyq_upvotes pyq_upvote[]
course_review_upvotes course_review_upvote[]
}

model subject {
Expand All @@ -42,6 +43,7 @@ model instructor {
updated_at DateTime @updatedAt
notes note[]
pyqs pyq[]
course course[]
}

model note {
Expand Down Expand Up @@ -75,25 +77,43 @@ model note_upvote {
model course {
id Int @id @unique @default(autoincrement())
title String @db.VarChar(255)
code String @unique @db.VarChar(100)
instructor instructor @relation(fields: [instructor_id], references: [id], onDelete: Restrict, onUpdate: Cascade)
instructor_id Int
code String @db.VarChar(100)
anonymous Boolean
created_by user @relation(fields: [created_by_id], references: [user_id], onDelete: Restrict)
created_by_id String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
upvotes course_upvote[]
reviews course_review[]
@@unique([instructor_id, code])
}

model course_upvote {
id Int @id @unique @default(autoincrement())
user user @relation(fields: [user_id], references: [user_id], onDelete: Cascade, onUpdate: Cascade)
model course_review {
id Int @id @unique @default(autoincrement())
user user @relation(fields: [user_id], references: [user_id], onDelete: Cascade, onUpdate: Cascade)
user_id String
course course @relation(fields: [course_id], references: [id], onDelete: Cascade, onUpdate: Cascade)
course course @relation(fields: [course_id], references: [id], onDelete: Cascade, onUpdate: Cascade)
course_id Int
rating Float @default(0.0)
comment String
anonymous Boolean
upvotes course_review_upvote[]
@@unique([user_id, course_id])
}

model course_review_upvote {
id Int @id @unique @default(autoincrement())
user user @relation(fields: [user_id], references: [user_id], onDelete: Cascade, onUpdate: Cascade)
user_id String
course_review course_review @relation(fields: [course_review_id], references: [id], onDelete: Cascade, onUpdate: Cascade)
course_review_id Int
@@unique([user_id, course_review_id])
}

model pyq {
id Int @id @unique @default(autoincrement())
title String @db.VarChar(255)
Expand Down
2 changes: 1 addition & 1 deletion src/components/Common/ModalContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const ModalContainer: FC<{
!showModal && 'hidden'
} flex justify-center items-center fixed top-0 left-0 right-0 z-50 w-full bg-black/50 overflow-x-hidden overflow-y-auto h-full`}
>
<div className="relative w-full pt-32 md:pt-48 px-5 h-full max-w-2xl md:h-auto">
<div className="relative w-full h-full mt-16 flex flex-col justify-center max-w-2xl md:h-auto">
<div className="relative bg-white rounded-lg shadow">
<div className="flex items-start justify-between p-4 border-b rounded-t">
<h3 className="text-xl font-semibold text-gray-900">
Expand Down
83 changes: 73 additions & 10 deletions src/components/Courses/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import React, { Dispatch, FC, SetStateAction, useEffect, useState } from 'react'
import { toast } from 'react-hot-toast'
import { ModalContainer } from '../Common/ModalContainer'
import { Input } from '../Common/Input'
import { SelectInput } from '../Common/SelectInput'
import { getInstructors } from '../../services/db/instructors/getInstructors'
import { addInstructor } from '../../services/db/instructors/addInstructor'

export const Modal: FC<{
isUpdateModal?: boolean
Expand All @@ -27,23 +30,54 @@ export const Modal: FC<{
const [title, setTitle] = useState<string>('')
const [code, setCode] = useState<string>('')
const [isAnonymous, setIsAnonymous] = useState<boolean>(false)
const [instructors, setInstructors] = useState<any[]>([])

const [instructorNameInput, setInstructorNameInput] = useState<string>('')
const [selectedInstructorId, setSelectedInstructorId] = useState<number>(0)
const [showInstructorNameDropdown, setShowInstructorNameDropdown] =
useState<boolean>(false)
const [selectedInstructorName, setSelectedInstructorName] =
useState<string>('')

//? functions
const actionHandler = async (e: any) => {
e.preventDefault()
if (title === '' || code === '') {
if (
title === '' ||
code === '' ||
(selectedInstructorId === 0 && selectedInstructorName === '')
) {
toast.error('Please fill all the details!')
} else {
setIsLoading(true)
await actionFunction({
id: isUpdateModal ? selectedEntity.id : null,
title: title,
code: code,
isAnonymous: isAnonymous,
refetch: refetch,
})
if (
!instructors.find(
(instructor) => instructor.name === selectedInstructorName
)
) {
const instructor = await addInstructor(selectedInstructorName)
await actionFunction({
id: isUpdateModal ? selectedEntity.id : null,
title: title,
code: code,
instructorId: instructor.id,
isAnonymous: isAnonymous,
refetch: refetch,
})
} else {
await actionFunction({
id: isUpdateModal ? selectedEntity.id : null,
title: title,
code: code,
instructorId: selectedInstructorId,
isAnonymous: isAnonymous,
refetch: refetch,
})
}
setTitle('')
setCode('')
setSelectedInstructorName('')
setSelectedInstructorId(0)
setIsAnonymous(false)
setShowModal(false)
setIsLoading(false)
Expand All @@ -55,6 +89,7 @@ export const Modal: FC<{
if (selectedEntity) {
setTitle(selectedEntity.title)
setCode(selectedEntity.code)
setSelectedInstructorName(selectedEntity.instructor.name)
setIsAnonymous(selectedEntity.anonymous)
}
}, [selectedEntity])
Expand All @@ -71,6 +106,17 @@ export const Modal: FC<{
document.removeEventListener('keydown', keyPressHandler, false)
}, [setShowModal])

useEffect(() => {
getInstructors().then((res) => setInstructors(res))
}, [])

useEffect(() => {
instructors.map((instructor) => {
if (instructor.name === selectedInstructorName)
setSelectedInstructorId(instructor.id)
})
}, [selectedInstructorName, instructors])

return (
<ModalContainer
header={header}
Expand All @@ -80,15 +126,15 @@ export const Modal: FC<{
<div className="flex flex-col p-10 space-y-2">
{/* Title */}
<Input
inputTitle="Title"
inputTitle="Course Name"
value={title}
setValue={setTitle}
placeholder={
'e.g. Artificial Intelligence and Data Science'
}
type={'text'}
/>

{/* Code */}
<Input
inputTitle="Code"
value={code}
Expand All @@ -97,6 +143,23 @@ export const Modal: FC<{
type={'text'}
/>

{/* Instructor */}
{/* Instructor */}
<SelectInput
dropdownItems={instructors}
dropdownKey={'name'}
dropdownValue={'name'}
inputName={'instructor'}
inputTitle={'Instructor'}
placeholder={'e.g. Dr. R.S. Sharma'}
selectedValue={selectedInstructorName}
setSelectedValue={setSelectedInstructorName}
inputValue={instructorNameInput}
setInputValue={setInstructorNameInput}
showDropdown={showInstructorNameDropdown}
setShowDropdown={setShowInstructorNameDropdown}
type={'text'}
/>
{/* Anonymous */}
<div className="flex flex-col space-y-1">
<span className="font-semibold">
Expand Down
Loading

0 comments on commit d5a4576

Please sign in to comment.