1
- import { Component , EventEmitter , Input , Output , OnInit } from '@angular/core' ;
1
+ import { Component , EventEmitter , Input , Output , OnInit , ViewChild } from '@angular/core' ;
2
2
import { Question , QuestionBody , SingleQuestionResponse } from './question.model' ;
3
3
import { FormControl , FormGroup , ReactiveFormsModule , Validators } from '@angular/forms' ;
4
4
import { DialogModule } from 'primeng/dialog' ;
5
- import { MultiSelectModule } from 'primeng/multiselect' ;
5
+ import { MultiSelect , MultiSelectModule } from 'primeng/multiselect' ;
6
6
import { DropdownModule } from 'primeng/dropdown' ;
7
7
import { ButtonModule } from 'primeng/button' ;
8
8
import { ConfirmationService , MessageService } from 'primeng/api' ;
@@ -29,6 +29,7 @@ import { DifficultyLevels } from './difficulty-levels.enum';
29
29
styleUrl : './question-dialog.component.css' ,
30
30
} )
31
31
export class QuestionDialogComponent implements OnInit {
32
+ @ViewChild ( 'topicSelector' ) topicSelector ! : MultiSelect ;
32
33
@Input ( ) question ! : Question ;
33
34
@Input ( ) isDialogVisible = false ;
34
35
@Output ( ) dialogClose = new EventEmitter < void > ( ) ;
@@ -38,7 +39,6 @@ export class QuestionDialogComponent implements OnInit {
38
39
@Output ( ) successfulRequest = new EventEmitter < string > ( ) ;
39
40
40
41
questionFormGroup ! : FormGroup ;
41
-
42
42
submitted = false ;
43
43
44
44
topicSearchValue = '' ;
@@ -49,10 +49,10 @@ export class QuestionDialogComponent implements OnInit {
49
49
50
50
difficulties ! : Difficulty [ ] ;
51
51
52
- isNoResultsFound = false ;
53
-
54
52
filteredTopics : Topic [ ] = [ ] ;
55
53
54
+ hasNoResultsFound = false ;
55
+
56
56
constructor ( private questionService : QuestionService ) { }
57
57
58
58
ngOnInit ( ) : void {
@@ -63,6 +63,10 @@ export class QuestionDialogComponent implements OnInit {
63
63
this . initTopics ( ) ;
64
64
}
65
65
66
+ get topicControl ( ) : FormControl {
67
+ return this . questionFormGroup . controls [ 'topics' ] as FormControl ;
68
+ }
69
+
66
70
get isTitleInvalid ( ) : boolean {
67
71
const titleControl = this . questionFormGroup . controls [ 'title' ] ;
68
72
return titleControl . dirty && titleControl . invalid ;
@@ -202,28 +206,29 @@ export class QuestionDialogComponent implements OnInit {
202
206
203
207
onFilterTopics ( event : { filter : string } ) {
204
208
this . topicSearchValue = event . filter ;
205
-
206
- this . filteredTopics = this . topics . filter ( topic =>
209
+ this . hasNoResultsFound = ! this . topics . some ( topic =>
207
210
topic . label . toLowerCase ( ) . includes ( this . topicSearchValue . toLowerCase ( ) ) ,
208
211
) ;
209
-
210
- this . isNoResultsFound = this . filteredTopics . length == 0 ;
211
212
}
212
213
213
214
addNewTopic ( ) {
215
+ const newTopic = this . topicSearchValue ;
214
216
const newValue : Topic = {
215
- label : this . topicSearchValue ,
216
- value : this . topicSearchValue ,
217
+ label : newTopic ,
218
+ value : newTopic ,
217
219
} ;
218
220
219
- const topicExists = this . topics . some (
220
- topic =>
221
- topic . label . toLowerCase ( ) === newValue . label . toLowerCase ( ) ||
222
- topic . value . toLowerCase ( ) === newValue . value . toLowerCase ( ) ,
223
- ) ;
221
+ const topicExists = this . topics . map ( t => t . label ) . some ( l => l . toLowerCase ( ) === newTopic . toLowerCase ( ) ) ;
224
222
225
- if ( ! topicExists ) {
226
- this . topics = [ ... this . topics , newValue ] ;
223
+ if ( topicExists ) {
224
+ return ;
227
225
}
226
+
227
+ this . topics . push ( newValue ) ;
228
+
229
+ // Immediately add the new topic, and clear the search filter
230
+ this . topicControl . setValue ( this . topicControl . value ?. concat ( [ newTopic ] ) ?? [ newTopic ] ) ;
231
+ this . topicSelector . resetFilter ( ) ;
232
+ this . hasNoResultsFound = false ;
228
233
}
229
234
}
0 commit comments