@@ -11,12 +11,13 @@ import { DialogModule } from 'primeng/dialog';
11
11
import { MultiSelectModule } from 'primeng/multiselect' ;
12
12
import { FormControl , FormGroup , ReactiveFormsModule } from '@angular/forms' ;
13
13
import { DropdownModule } from 'primeng/dropdown' ;
14
- import { Question , QuestionResponse } from './question.model' ;
14
+ import { Question } from './question.model' ;
15
15
import { Column } from './column.model' ;
16
- import { Topic , TopicResponse } from './topic.model' ;
16
+ import { Topic } from './topic.model' ;
17
17
import { Difficulty } from './difficulty.model' ;
18
18
import { DifficultyLevels } from './difficulty-levels.enum' ;
19
19
import { QuestionService } from './question.service' ;
20
+ import { forkJoin } from 'rxjs' ;
20
21
21
22
@Component ( {
22
23
selector : 'app-questions' ,
@@ -41,8 +42,6 @@ import { QuestionService } from './question.service';
41
42
styleUrl : './questions.component.css' ,
42
43
} )
43
44
export class QuestionsComponent implements OnInit {
44
- questionResponse ! : QuestionResponse ;
45
-
46
45
questions : Question [ ] = [ ] ;
47
46
48
47
topics ! : Topic [ ] ;
@@ -80,30 +79,28 @@ export class QuestionsComponent implements OnInit {
80
79
difficulty : '' ,
81
80
} ;
82
81
83
- this . questionService . getQuestions ( ) . subscribe ( {
84
- next : ( response : QuestionResponse ) => {
85
- this . questions = response . data ! ;
82
+ forkJoin ( {
83
+ questions : this . questionService . getQuestions ( ) ,
84
+ topics : this . questionService . getTopics ( ) ,
85
+ } ) . subscribe ( {
86
+ next : results => {
87
+ this . questions = results . questions . data || [ ] ;
88
+ this . topics =
89
+ results . topics . data ?. map ( topic => ( {
90
+ label : topic ,
91
+ value : topic ,
92
+ } ) ) || [ ] ;
86
93
} ,
87
94
error : ( error : Error ) => {
88
- // TODO: prompt an error on unsuccessful fetch
89
- console . log ( error ) ;
90
- } ,
91
- complete : ( ) => {
92
- // TODO: add loading state for this
93
- console . log ( 'complete' ) ;
94
- } ,
95
- } ) ;
96
-
97
- this . questionService . getTopics ( ) . subscribe ( {
98
- next : ( response : TopicResponse ) => {
99
- this . topics = response . data ! . map ( topic => ( {
100
- label : topic ,
101
- value : topic
102
- } ) )
103
- } ,
104
- error : ( error : Error ) => {
105
- // TODO: prompt an error on unsuccessful fetch
106
- console . log ( error ) ;
95
+ console . error ( error ) ;
96
+ this . questions = [ ] ;
97
+ this . topics = [ ] ;
98
+ this . messageService . add ( {
99
+ severity : 'error' ,
100
+ summary : 'Error' ,
101
+ detail : 'Failed to load data. Please try again later.' ,
102
+ life : 3000 ,
103
+ } ) ;
107
104
} ,
108
105
complete : ( ) => {
109
106
// TODO: add loading state for this
0 commit comments