@@ -5,6 +5,7 @@ import ButtonGroup from "react-bootstrap/ButtonGroup";
5
5
import CreateQn from "./CreateQn" ;
6
6
import EditQn from "./EditQn" ;
7
7
import questionService from "../../services/questions"
8
+ import userService from "../../services/users" ;
8
9
9
10
function Question ( ) {
10
11
const [ questions , setQuestions ] = useState ( [ ] ) ;
@@ -13,6 +14,7 @@ function Question() {
13
14
const [ questionToDelete , setQuestionToDelete ] = useState ( null ) ;
14
15
const [ showEditModal , setShowEditModal ] = useState ( false ) ;
15
16
const [ currentQuestion , setCurrentQuestion ] = useState ( null ) ;
17
+ const [ isAdmin , setIsAdmin ] = useState ( false ) ;
16
18
17
19
const handleShow = ( ) => setShowComponent ( true ) ;
18
20
const handleClose = ( ) => setShowComponent ( false ) ;
@@ -25,6 +27,28 @@ function Question() {
25
27
. catch ( err => console . log ( err ) ) ;
26
28
} , [ ] ) ;
27
29
30
+ { /* Added to check the authorization status to determine whether to show edit, delete, add buttons */ }
31
+
32
+ useEffect ( ( ) => {
33
+ const token = sessionStorage . getItem ( 'jwt_token' ) ;
34
+ const authHeader = {
35
+ headers : {
36
+ 'Authorization' : `Bearer ${ token } ` ,
37
+ } ,
38
+ } ;
39
+
40
+ // verify token asynchronously, set auth status only after request completes
41
+ userService . verifyAdmin ( authHeader )
42
+ . then ( response => {
43
+ if ( response . status == 200 ) {
44
+ setIsAdmin ( true ) ;
45
+ }
46
+ } )
47
+ . catch ( e => {
48
+ console . log ( 'Error:' , e ) ;
49
+ } ) ;
50
+ } , [ ] ) ;
51
+
28
52
const easyQuestions = questions . filter ( q => q . complexity === "Easy" )
29
53
const mediumQuestions = questions . filter ( q => q . complexity === "Medium" )
30
54
const hardQuestions = questions . filter ( q => q . complexity === "Hard" )
@@ -85,7 +109,7 @@ function Question() {
85
109
< th > Title</ th >
86
110
< th > Description</ th >
87
111
< th > Category</ th >
88
- < th > Action</ th >
112
+ { isAdmin && ( < th > Action</ th > ) }
89
113
</ tr >
90
114
</ thead >
91
115
< tbody >
@@ -95,20 +119,22 @@ function Question() {
95
119
< td > { question . title } </ td >
96
120
< td > { question . description } </ td >
97
121
< td > { question . category ? question . category . join ( ", " ) : '' } </ td >
98
- < td >
99
- < ButtonGroup className = "mb-2" >
100
- < button
101
- className = 'btn btn-success'
102
- onClick = { ( ) => handleShowEditModal ( question ) }
103
- >
104
- Edit
105
- </ button >
106
- < button className = 'btn btn-danger' size = "sm"
107
- onClick = { ( ) => handleShowDelete ( question . _id ) } >
108
- Delete
109
- </ button >
110
- </ ButtonGroup >
111
- </ td >
122
+ { isAdmin && (
123
+ < td >
124
+ < ButtonGroup className = "mb-2" >
125
+ < button
126
+ className = 'btn btn-success'
127
+ onClick = { ( ) => handleShowEditModal ( question ) }
128
+ >
129
+ Edit
130
+ </ button >
131
+ < button className = 'btn btn-danger' size = "sm"
132
+ onClick = { ( ) => handleShowDelete ( question . _id ) } >
133
+ Delete
134
+ </ button >
135
+ </ ButtonGroup >
136
+ </ td >
137
+ ) }
112
138
</ tr >
113
139
) ) }
114
140
</ tbody >
@@ -121,10 +147,11 @@ function Question() {
121
147
< div className = 'bg-white rounded p-3 m-3' >
122
148
< div className = "d-flex justify-content-between" >
123
149
< h1 > Questions</ h1 >
124
- < button className = "btn btn-primary mt-3" onClick = { ( ) => handleShow ( ) } >
125
- { showComponent ? 'Hide' : 'Add question' }
126
- </ button >
127
-
150
+ { isAdmin && (
151
+ < button className = "btn btn-primary mt-3" onClick = { ( ) => handleShow ( ) } >
152
+ { showComponent ? 'Hide' : 'Add question' }
153
+ </ button >
154
+ ) }
128
155
< Modal show = { showComponent } onHide = { handleClose } >
129
156
< Modal . Header closeButton >
130
157
< Modal . Title > Add New Question</ Modal . Title >
0 commit comments