@@ -10,17 +10,15 @@ import { MarkdownEditor } from "@components/ui/markdown-editor";
10
10
import type { Review } from "types/reviews" ;
11
11
import Link from "next/link" ;
12
12
13
- const academicYearOptions = [
14
- { value : "2020" , label : "2020" } ,
15
- { value : "2021" , label : "2021" } ,
16
- { value : "2022" , label : "2022" } ,
17
- { value : "2023" , label : "2023" } ,
18
- { value : "2024" , label : "2024" }
19
- ] ;
20
-
21
13
interface WriteReviewFormProps {
22
14
courseCode ?: string ;
23
- onSubmit : ( academicYear : string , description : string , rating : number ) => Promise < boolean > ;
15
+ onSubmit : (
16
+ section : string ,
17
+ term : string ,
18
+ academicYear : string ,
19
+ description : string ,
20
+ rating : number
21
+ ) => Promise < boolean > ;
24
22
previousReview ?: Review ;
25
23
}
26
24
@@ -33,7 +31,7 @@ const reviewGuidelines = [
33
31
{
34
32
text : (
35
33
< div >
36
- Kindly refrain from mentioning names and write your reviews with respect .{ " " }
34
+ Constructive criticism is encouraged .{ " " }
37
35
< span className = "underline" >
38
36
< Link href = "/guidelines" target = "_blank" >
39
37
Click here to learn more
@@ -48,6 +46,8 @@ const reviewGuidelines = [
48
46
49
47
// Keys for local storage
50
48
const reviewFormKeys = ( courseCode : string ) => ( {
49
+ sectionKey : `reviewFormSection_${ courseCode } ` ,
50
+ termKey : `reviewFormTerm_${ courseCode } ` ,
51
51
academicYearKey : `reviewFormAcademicYear_${ courseCode } ` ,
52
52
ratingKey : `reviewFormRating_${ courseCode } ` ,
53
53
descriptionKey : `reviewFormDescription_${ courseCode } `
@@ -57,6 +57,8 @@ export default function WriteReviewForm({ courseCode, onSubmit, previousReview }
57
57
const reviewKeys = useMemo ( ( ) => reviewFormKeys ( courseCode || "" ) , [ courseCode ] ) ;
58
58
const [ academicYear , setAcademicYear ] = useState < string | null > ( previousReview ?. academicYear || null ) ;
59
59
const [ rating , setRating ] = useState ( previousReview ?. rating || 0 ) ;
60
+ const [ term , setTerm ] = useState < string | null > ( null ) ;
61
+ const [ section , setSection ] = useState < string | null > ( null ) ;
60
62
61
63
const markdownEditor = useEditor ( {
62
64
extensions : [
@@ -78,10 +80,14 @@ export default function WriteReviewForm({ courseCode, onSubmit, previousReview }
78
80
79
81
useEffect ( ( ) => {
80
82
if ( ! previousReview ) {
83
+ const savedSection = localStorage . getItem ( reviewKeys . sectionKey ) ;
84
+ const savedTerm = localStorage . getItem ( reviewKeys . termKey ) ;
81
85
const savedAcademicYear = localStorage . getItem ( reviewKeys . academicYearKey ) ;
82
86
const savedRating = localStorage . getItem ( reviewKeys . ratingKey ) ;
83
87
const savedDescription = localStorage . getItem ( reviewKeys . descriptionKey ) ;
84
88
89
+ if ( savedSection ) setSection ( savedSection ) ;
90
+ if ( savedTerm ) setTerm ( savedTerm ) ;
85
91
if ( savedAcademicYear ) setAcademicYear ( savedAcademicYear ) ;
86
92
if ( savedRating ) setRating ( parseFloat ( savedRating ) ) ;
87
93
if ( savedDescription && markdownEditor ) markdownEditor . commands . setContent ( savedDescription ) ;
@@ -91,6 +97,8 @@ export default function WriteReviewForm({ courseCode, onSubmit, previousReview }
91
97
useEffect ( ( ) => {
92
98
if ( rating ) localStorage . setItem ( reviewKeys . ratingKey , rating . toString ( ) ) ;
93
99
if ( academicYear ) localStorage . setItem ( reviewKeys . academicYearKey , academicYear ) ;
100
+ if ( section ) localStorage . setItem ( reviewKeys . sectionKey , section ) ;
101
+ if ( term ) localStorage . setItem ( reviewKeys . termKey , term ) ;
94
102
if ( markdownEditor ) {
95
103
const saveMarkdown = ( ) => {
96
104
localStorage . setItem ( reviewKeys . descriptionKey , markdownEditor . storage . markdown . getMarkdown ( ) ) ;
@@ -100,29 +108,44 @@ export default function WriteReviewForm({ courseCode, onSubmit, previousReview }
100
108
markdownEditor . off ( "update" , saveMarkdown ) ;
101
109
} ;
102
110
}
103
- } , [ markdownEditor , academicYear , reviewKeys , rating ] ) ;
111
+ } , [ markdownEditor , academicYear , reviewKeys , rating , section , term ] ) ;
104
112
105
113
const handleSubmit = ( e : React . FormEvent < HTMLFormElement > ) : void => {
106
114
e . preventDefault ( ) ;
107
115
const currentDescription = markdownEditor ?. storage . markdown . getMarkdown ( ) || "" ;
108
116
109
- if ( ! academicYear || ! currentDescription || ! rating ) {
117
+ const missingFields : string [ ] = [ ] ;
118
+ const fields = [
119
+ { name : "academicYear" , label : "academic year" } ,
120
+ { name : "currentDescription" , label : "review descriptions" } ,
121
+ { name : "rating" , label : "ratings" } ,
122
+ { name : "section" , label : "section" } ,
123
+ { name : "term" , label : "term" }
124
+ ] ;
125
+
126
+ fields . forEach ( ( field ) => {
127
+ if ( ! eval ( field . name ) ) {
128
+ missingFields . push ( field . label ) ;
129
+ }
130
+ } ) ;
131
+
132
+ if ( missingFields . length > 0 ) {
110
133
notifications . show ( {
111
134
title : "Hold on! Your review still contains some missing fields" ,
112
135
color : "red" ,
113
- message :
114
- "Make sure you have filled all the fields such as academic year, ratings, and review descriptions" ,
136
+ message : `Please fill in the following fields: ${ missingFields . join ( ", " ) } ` ,
115
137
autoClose : 5000
116
138
} ) ;
117
139
return ;
118
140
}
119
-
120
- onSubmit ( academicYear , currentDescription , rating ) . then ( ( success ) => {
141
+ onSubmit ( section ! ! , term ! ! , academicYear ! ! , currentDescription , rating ) . then ( ( success ) => {
121
142
if ( success ) {
122
143
resetForm ( ) ;
123
144
localStorage . removeItem ( reviewKeys . academicYearKey ) ;
124
145
localStorage . removeItem ( reviewKeys . ratingKey ) ;
125
146
localStorage . removeItem ( reviewKeys . descriptionKey ) ;
147
+ localStorage . removeItem ( reviewKeys . termKey ) ;
148
+ localStorage . removeItem ( reviewKeys . sectionKey ) ;
126
149
}
127
150
} ) ;
128
151
} ;
@@ -139,13 +162,30 @@ export default function WriteReviewForm({ courseCode, onSubmit, previousReview }
139
162
) ) }
140
163
141
164
< Paper w = "100%" h = "100%" >
142
- < Flex direction = "row" gap = "sm" my = "sm" >
165
+ < Flex align = "end" wrap = "wrap" direction = "row" gap = "xs" my = "sm" >
166
+ < Select
167
+ className = "w-[100px]"
168
+ data = { [ "1" , "2" , "3" , "4" , "5" ] }
169
+ value = { section }
170
+ label = "Section"
171
+ onChange = { setSection }
172
+ placeholder = "Section"
173
+ />
174
+ < Select
175
+ className = "w-[100px]"
176
+ data = { [ "1" , "2" , "3" ] }
177
+ value = { term }
178
+ label = "Term"
179
+ onChange = { setTerm }
180
+ placeholder = "Term"
181
+ />
143
182
< Select
144
- data = { academicYearOptions }
183
+ className = "w-[100px]"
184
+ data = { [ "2021" , "2022" , "2023" , "2024" , "2025" ] }
145
185
value = { academicYear }
146
- defaultSearchValue = { academicYear || undefined }
186
+ label = "Year"
147
187
onChange = { setAcademicYear }
148
- placeholder = "Academic year "
188
+ placeholder = "Year "
149
189
/>
150
190
< Rating size = "lg" defaultValue = { 0 } fractions = { 2 } value = { rating } onChange = { setRating } />
151
191
</ Flex >
0 commit comments