@@ -10,6 +10,7 @@ import { QuestionService } from '../../_services/question.service';
10
10
import { Difficulty } from './difficulty.model' ;
11
11
import { Topic } from './topic.model' ;
12
12
import { HttpErrorResponse } from '@angular/common/http' ;
13
+ import { CommonModule } from '@angular/common' ;
13
14
14
15
@Component ( {
15
16
selector : 'app-question-dialog' ,
@@ -22,6 +23,7 @@ import { HttpErrorResponse } from '@angular/common/http';
22
23
MultiSelectModule ,
23
24
DropdownModule ,
24
25
QuestionDialogComponent ,
26
+ CommonModule ,
25
27
] ,
26
28
providers : [ QuestionService , ConfirmationService , MessageService ] ,
27
29
templateUrl : './question-dialog.component.html' ,
@@ -45,6 +47,12 @@ export class QuestionDialogComponent implements OnInit {
45
47
46
48
questions : Question [ ] = [ ] ;
47
49
50
+ topicSearchValue = '' ;
51
+
52
+ isNoResultsFound = false ;
53
+
54
+ filteredTopics : Topic [ ] = [ ] ;
55
+
48
56
constructor ( private questionService : QuestionService ) { }
49
57
50
58
ngOnInit ( ) : void {
@@ -153,4 +161,31 @@ export class QuestionDialogComponent implements OnInit {
153
161
description : '' ,
154
162
} ) ;
155
163
}
164
+
165
+ onFilterTopics ( event : { filter : string } ) {
166
+ this . topicSearchValue = event . filter ;
167
+
168
+ this . filteredTopics = this . topics . filter ( topic =>
169
+ topic . label . toLowerCase ( ) . includes ( this . topicSearchValue . toLowerCase ( ) ) ,
170
+ ) ;
171
+
172
+ this . isNoResultsFound = this . filteredTopics . length == 0 ;
173
+ }
174
+
175
+ addNewTopic ( ) {
176
+ const newValue : Topic = {
177
+ label : this . topicSearchValue ,
178
+ value : this . topicSearchValue ,
179
+ } ;
180
+
181
+ const topicExists = this . topics . some (
182
+ topic =>
183
+ topic . label . toLowerCase ( ) === newValue . label . toLowerCase ( ) ||
184
+ topic . value . toLowerCase ( ) === newValue . value . toLowerCase ( ) ,
185
+ ) ;
186
+
187
+ if ( ! topicExists ) {
188
+ this . topics = [ ...this . topics , newValue ] ;
189
+ }
190
+ }
156
191
}
0 commit comments