@@ -55,7 +55,7 @@ export class QuestionsComponent implements OnInit {
55
55
56
56
questionFormGroup ! : FormGroup ;
57
57
58
- selectedDifficulty ! : string ;
58
+ difficulty ! : string ;
59
59
60
60
question ! : Question ;
61
61
@@ -84,7 +84,7 @@ export class QuestionsComponent implements OnInit {
84
84
85
85
this . initFormGroup ( ) ;
86
86
87
- this . initListeners ( ) ;
87
+ // this.initListeners();
88
88
}
89
89
90
90
get isTitleInvalid ( ) : boolean {
@@ -98,12 +98,12 @@ export class QuestionsComponent implements OnInit {
98
98
}
99
99
100
100
get isDifficultyInvalid ( ) : boolean {
101
- const difficultyControl = this . questionFormGroup . controls [ 'selectedDifficulty ' ] ;
101
+ const difficultyControl = this . questionFormGroup . controls [ 'difficulty ' ] ;
102
102
return difficultyControl . dirty && difficultyControl . invalid ;
103
103
}
104
104
105
105
get isTopicsInvalid ( ) : boolean {
106
- const topicsControl = this . questionFormGroup . controls [ 'selectedTopics ' ] ;
106
+ const topicsControl = this . questionFormGroup . controls [ 'topics ' ] ;
107
107
return topicsControl . dirty && topicsControl . invalid ;
108
108
}
109
109
@@ -128,20 +128,13 @@ export class QuestionsComponent implements OnInit {
128
128
saveQuestion ( ) {
129
129
this . submitted = true ;
130
130
131
- if (
132
- ! this . question . title ?. trim ( ) ||
133
- ! this . question . topics ||
134
- this . question . topics ?. length == 0 ||
135
- ! this . question . difficulty ?. trim ( ) ||
136
- ! this . question . description ?. trim ( )
137
- ) {
131
+ if ( ! this . questionFormGroup . valid ) {
138
132
return ;
139
133
}
140
134
141
135
if ( this . question . id ) {
142
136
// update
143
- const { id, ...questionBody } = this . question ;
144
- this . handleEditQuestionResponse ( id , questionBody ) ;
137
+ this . handleEditQuestionResponse ( this . question . id , this . questionFormGroup . value ) ;
145
138
} else {
146
139
// add
147
140
this . handleAddQuestionResponse ( ) ;
@@ -153,50 +146,28 @@ export class QuestionsComponent implements OnInit {
153
146
154
147
resetFormGroup ( ) {
155
148
this . questionFormGroup . reset ( {
156
- selectedTopics : [ ] ,
157
- selectedDifficulty : '' ,
158
- textTitle : '' ,
159
- textDescription : '' ,
149
+ topics : [ ] ,
150
+ difficulty : '' ,
151
+ title : '' ,
152
+ description : '' ,
160
153
} ) ;
161
154
}
162
155
163
156
editQuestion ( question : Question ) {
164
157
this . question . id = question . id ;
165
158
this . questionFormGroup . patchValue ( {
166
- textTitle : question . title ,
167
- textDescription : question . description ,
168
- selectedTopics : question . topics ,
169
- selectedDifficulty : question . difficulty ,
159
+ title : question . title ,
160
+ description : question . description ,
161
+ topics : question . topics ,
162
+ difficulty : question . difficulty ,
170
163
} ) ;
171
164
this . isDialogVisible = true ;
172
165
}
173
166
174
- initListeners ( ) {
175
- // Dropdown difficulty listener
176
- this . questionFormGroup . get ( 'selectedDifficulty' ) ?. valueChanges . subscribe ( v => {
177
- this . question . difficulty = v ;
178
- } ) ;
179
-
180
- // Multiselect topics listener
181
- this . questionFormGroup . get ( 'selectedTopics' ) ?. valueChanges . subscribe ( v => {
182
- this . question . topics = v ;
183
- } ) ;
184
-
185
- // text title listener
186
- this . questionFormGroup . get ( 'textTitle' ) ?. valueChanges . subscribe ( v => {
187
- this . question . title = v ;
188
- } ) ;
189
-
190
- // text description listener
191
- this . questionFormGroup . get ( 'textDescription' ) ?. valueChanges . subscribe ( v => {
192
- this . question . description = v ;
193
- } ) ;
194
- }
195
-
196
167
initFormGroup ( ) {
197
168
this . questionFormGroup = new FormGroup ( {
198
- selectedTopics : new FormControl < string [ ] | null > ( [ ] , [ Validators . required ] ) ,
199
- selectedDifficulty : new FormControl < Difficulty [ ] | null > ( [ ] , [ Validators . required ] ) ,
169
+ topics : new FormControl < string [ ] | null > ( [ ] , [ Validators . required ] ) ,
170
+ difficulty : new FormControl < Difficulty [ ] | null > ( [ ] , [ Validators . required ] ) ,
200
171
title : new FormControl < string | null > ( '' , [ Validators . required ] ) ,
201
172
description : new FormControl < string | null > ( '' , [ Validators . required ] ) ,
202
173
} ) ;
@@ -221,14 +192,13 @@ export class QuestionsComponent implements OnInit {
221
192
}
222
193
223
194
handleAddQuestionResponse ( ) {
224
- this . questionService . addQuestion ( this . question ) . subscribe ( {
195
+ this . questionService . addQuestion ( this . questionFormGroup . value ) . subscribe ( {
225
196
next : ( response : SingleQuestionResponse ) => {
226
197
if ( this . questions ) {
227
198
this . questions = [ ...this . questions , response . data ] ;
228
199
}
229
200
} ,
230
201
error : ( error : HttpErrorResponse ) => {
231
- console . log ( error ) ;
232
202
this . messageService . add ( {
233
203
severity : 'error' ,
234
204
summary : 'Error' ,
@@ -280,10 +250,9 @@ export class QuestionsComponent implements OnInit {
280
250
this . questionService . updateQuestion ( id , question ) . subscribe ( {
281
251
next : ( response : SingleQuestionResponse ) => {
282
252
this . questions [ this . questions . findIndex ( x => x . id == id ) ] = response . data ;
253
+ this . questions = [ ...this . questions ] ;
283
254
} ,
284
255
error : ( error : HttpErrorResponse ) => {
285
- console . log ( error ) ;
286
- console . log ( question ) ;
287
256
this . messageService . add ( {
288
257
severity : 'error' ,
289
258
summary : 'Error' ,
@@ -315,8 +284,7 @@ export class QuestionsComponent implements OnInit {
315
284
value : topic ,
316
285
} ) ) || [ ] ;
317
286
} ,
318
- error : ( error : Error ) => {
319
- console . error ( error ) ;
287
+ error : ( ) => {
320
288
this . questions = [ ] ;
321
289
this . topics = [ ] ;
322
290
this . messageService . add ( {
0 commit comments