1
1
import React , { useState , useEffect } from 'react' ;
2
2
import Modal from "react-bootstrap/Modal" ;
3
- import Table from "react-bootstrap/Table" ;
4
3
import ButtonGroup from "react-bootstrap/ButtonGroup" ;
5
4
import CreateQn from "./CreateQn" ;
6
5
import EditQn from "./EditQn" ;
6
+ import DetailQn from "./DetailQn" ;
7
7
import questionService from "../../services/questions"
8
8
import userService from "../../services/users" ;
9
9
import categoryService from "../../services/categories" ;
@@ -17,6 +17,7 @@ function Question() {
17
17
const [ showDeleteModal , setShowDeleteModal ] = useState ( false ) ;
18
18
const [ questionToDelete , setQuestionToDelete ] = useState ( null ) ;
19
19
const [ showEditModal , setShowEditModal ] = useState ( false ) ;
20
+ const [ showDetailModal , setShowDetailModal ] = useState ( false ) ;
20
21
const [ currentQuestion , setCurrentQuestion ] = useState ( null ) ;
21
22
const [ isAdmin , setIsAdmin ] = useState ( false ) ;
22
23
@@ -57,7 +58,15 @@ function Question() {
57
58
setQuestions ( ( prevQuestions ) => [ ...prevQuestions , newQuestion ] ) ;
58
59
} ;
59
60
60
-
61
+ const handleShowDetailModal = ( question ) => {
62
+ setCurrentQuestion ( question ) ;
63
+ setShowDetailModal ( true ) ;
64
+ }
65
+
66
+ const handleCloseDetailModal = ( ) => {
67
+ setShowDetailModal ( false ) ;
68
+ }
69
+
61
70
const editQuestion = ( id , updatedQuestion ) => {
62
71
const updatedQuestions = questions . map ( ( q ) =>
63
72
q . _id === id ? { ...q , ...updatedQuestion } : q
@@ -127,55 +136,69 @@ function Question() {
127
136
128
137
129
138
const renderQuestionsTable = ( ) => {
130
- const CustomButtonComponent = ( props ) => {
131
- const question = props . data
132
- return < ButtonGroup className = "mb-2" >
133
- < button
134
- className = 'btn btn-success'
135
- onClick = { ( ) => handleShowEditModal ( question ) }
136
- >
137
- Edit
138
- </ button >
139
- < button className = 'btn btn-danger' size = "sm"
140
- onClick = { ( ) => handleShowDelete ( question . _id ) } >
141
- Delete
142
- </ button >
143
- </ ButtonGroup >
139
+ const editDeleteButtonComponent = ( props ) => {
140
+ const question = props . data ;
141
+ return (
142
+ < ButtonGroup className = "container-fluid mt-1 mb-1" >
143
+ < button
144
+ className = 'btn btn-success btn-sm'
145
+ onClick = { ( ) => handleShowEditModal ( question ) }
146
+ >
147
+ Edit
148
+ </ button >
149
+ < button className = 'btn btn-danger btn-sm' size = "sm"
150
+ onClick = { ( ) => handleShowDelete ( question . _id ) } >
151
+ Delete
152
+ </ button >
153
+ </ ButtonGroup >
154
+ ) ;
144
155
} ;
145
156
157
+ const showDetailButtonComponent = ( props ) => {
158
+ const question = props . data ;
159
+ return (
160
+ < ButtonGroup className = "container-fluid mt-1 mb-1" >
161
+ < button
162
+ className = 'btn btn-info btn-sm'
163
+ onClick = { ( ) => handleShowDetailModal ( question ) }
164
+ >
165
+ Show Detail
166
+ </ button >
167
+ </ ButtonGroup >
168
+ ) ;
169
+ } ;
170
+
146
171
const colDefs = [
147
172
{ field : "id" , flex : 1 , wrapText : true , sort : "asc" } ,
148
- { field : "title" , flex : 2 } ,
149
- { field : "description" , flex : 5 , wrapText : true , autoHeight : true } ,
150
- { field : "complexity" , flex : 1.5 ,
151
- comparator : ( valueA , valueB , nodeA , nodeB , isDescending ) => {
152
- if ( valueA == valueB ) return 0 ;
153
- if ( valueA == "Easy" || valueB == "Hard" ) return - 1 ;
154
- if ( valueA == "Hard" || valueB == "Easy" ) return 1 ;
155
- }
156
- }
173
+ { field : "title" , flex : 4 , wrapText : true } ,
174
+ { field : "category" , flex : 3 , autoHeight : true , cellDataType : 'text' } ,
175
+ {
176
+ field : "complexity" ,
177
+ flex : 1.5 ,
178
+ comparator : ( valueA , valueB ) => {
179
+ if ( valueA === valueB ) return 0 ;
180
+ if ( valueA === "Easy" || valueB === "Hard" ) return - 1 ;
181
+ if ( valueA === "Hard" || valueB === "Easy" ) return 1 ;
182
+ }
183
+ } ,
184
+ ...( isAdmin ? [ {
185
+ field : "action" ,
186
+ width : 200 ,
187
+ resizable : false ,
188
+ sortable : false ,
189
+ cellRenderer : editDeleteButtonComponent
190
+ } ] : [ {
191
+ field : "details" ,
192
+ width : 200 ,
193
+ resizable : false ,
194
+ sortable : false ,
195
+ cellRenderer : showDetailButtonComponent } ] )
157
196
] ;
158
-
159
- if ( isAdmin ) {
160
- colDefs = [
161
- { field : "id" , flex : 1 , wrapText : true , sort : "asc" } ,
162
- { field : "title" , flex : 2 } ,
163
- { field : "description" , flex : 5 , wrapText : true , autoHeight : true } ,
164
- { field : "complexity" , flex : 1.5 ,
165
- comparator : ( valueA , valueB , nodeA , nodeB , isDescending ) => {
166
- if ( valueA == valueB ) return 0 ;
167
- if ( valueA == "Easy" || valueB == "Hard" ) return - 1 ;
168
- if ( valueA == "Hard" || valueB == "Easy" ) return 1 ;
169
- }
170
- } ,
171
- { field : "action" , width : 200 , resizable : false , sortable : false , cellRenderer : CustomButtonComponent }
172
- ] ;
173
- }
174
-
197
+
175
198
return (
176
199
< div
177
- className = "container-fluid ag-theme-quartz" // applying the Data Grid theme
178
- style = { { height : 500 } } // the Data Grid will fill the size of the parent container
200
+ className = "container-fluid ag-theme-quartz"
201
+ style = { { height : 500 } }
179
202
>
180
203
< AgGridReact
181
204
rowData = { questions }
@@ -223,6 +246,19 @@ function Question() {
223
246
</ Modal . Body >
224
247
</ Modal >
225
248
249
+ { /* Detail Modal */ }
250
+ < Modal show = { showDetailModal } onHide = { handleCloseDetailModal } backdrop = "static" >
251
+ < Modal . Header closeButton >
252
+ < Modal . Title > Question Details</ Modal . Title >
253
+ </ Modal . Header >
254
+ < Modal . Body >
255
+ < DetailQn
256
+ question = { currentQuestion }
257
+ handleClose = { handleCloseDetailModal }
258
+ />
259
+ </ Modal . Body >
260
+ </ Modal >
261
+
226
262
227
263
< Modal show = { showDeleteModal } onHide = { handleCloseDelete } centered >
228
264
< Modal . Body className = "text-center" >
0 commit comments