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' ;
@@ -30,6 +30,7 @@ import { CommonModule } from '@angular/common';
30
30
styleUrl : './question-dialog.component.css' ,
31
31
} )
32
32
export class QuestionDialogComponent implements OnInit {
33
+ @ViewChild ( 'topicSelector' ) topicSelector ! : MultiSelect ;
33
34
@Input ( ) question ! : Question ;
34
35
@Input ( ) headerMessage ! : string ;
35
36
@Input ( ) isDialogVisible = false ;
@@ -42,23 +43,22 @@ export class QuestionDialogComponent implements OnInit {
42
43
@Output ( ) successfulRequest = new EventEmitter < string > ( ) ;
43
44
44
45
questionFormGroup ! : FormGroup ;
45
-
46
46
submitted = false ;
47
47
48
48
questions : Question [ ] = [ ] ;
49
-
50
49
topicSearchValue = '' ;
51
-
52
- isNoResultsFound = false ;
53
-
54
- filteredTopics : Topic [ ] = [ ] ;
50
+ hasNoResultsFound = false ;
55
51
56
52
constructor ( private questionService : QuestionService ) { }
57
53
58
54
ngOnInit ( ) : void {
59
55
this . initFormGroup ( ) ;
60
56
}
61
57
58
+ get topicControl ( ) : FormControl {
59
+ return this . questionFormGroup . controls [ 'topics' ] as FormControl ;
60
+ }
61
+
62
62
get isTitleInvalid ( ) : boolean {
63
63
const titleControl = this . questionFormGroup . controls [ 'title' ] ;
64
64
return titleControl . dirty && titleControl . invalid ;
@@ -164,28 +164,29 @@ export class QuestionDialogComponent implements OnInit {
164
164
165
165
onFilterTopics ( event : { filter : string } ) {
166
166
this . topicSearchValue = event . filter ;
167
-
168
- this . filteredTopics = this . topics . filter ( topic =>
167
+ this . hasNoResultsFound = ! this . topics . some ( topic =>
169
168
topic . label . toLowerCase ( ) . includes ( this . topicSearchValue . toLowerCase ( ) ) ,
170
169
) ;
171
-
172
- this . isNoResultsFound = this . filteredTopics . length == 0 ;
173
170
}
174
171
175
172
addNewTopic ( ) {
173
+ const newTopic = this . topicSearchValue ;
176
174
const newValue : Topic = {
177
- label : this . topicSearchValue ,
178
- value : this . topicSearchValue ,
175
+ label : newTopic ,
176
+ value : newTopic ,
179
177
} ;
180
178
181
- const topicExists = this . topics . some (
182
- topic =>
183
- topic . label . toLowerCase ( ) === newValue . label . toLowerCase ( ) ||
184
- topic . value . toLowerCase ( ) === newValue . value . toLowerCase ( ) ,
185
- ) ;
179
+ const topicExists = this . topics . map ( t => t . label ) . some ( l => l . toLowerCase ( ) === newTopic . toLowerCase ( ) ) ;
186
180
187
- if ( ! topicExists ) {
188
- this . topics = [ ... this . topics , newValue ] ;
181
+ if ( topicExists ) {
182
+ return ;
189
183
}
184
+
185
+ this . topics . push ( newValue ) ;
186
+
187
+ // Immediately add the new topic, and clear the search filter
188
+ this . topicControl . setValue ( this . topicControl . value ?. concat ( [ newTopic ] ) ?? [ newTopic ] ) ;
189
+ this . topicSelector . resetFilter ( ) ;
190
+ this . hasNoResultsFound = false ;
190
191
}
191
192
}
0 commit comments