Skip to content

Commit 6fc2041

Browse files
committed
Update topics dropdown to allow addition of new topics
1 parent e63f15c commit 6fc2041

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

frontend/src/app/questions/question-dialog.component.html

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,24 @@
3232
required="true"
3333
[style]="{ width: '100%' }"
3434
[options]="topics"
35+
(onFilter)="onFilterTopics($event)"
3536
formControlName="topics"
3637
optionLabel="label"
3738
optionValue="value"
38-
placeholder="Select Topics" />
39+
placeholder="Select Topics">
40+
<ng-template let-option pTemplate="item">
41+
<span>{{ option.label }}</span>
42+
</ng-template>
43+
44+
<ng-template pTemplate="footer">
45+
<p-button
46+
*ngIf="isNoResultsFound"
47+
type="button"
48+
label="Add as New Topic"
49+
icon="pi pi-plus"
50+
(click)="addNewTopic()" />
51+
</ng-template>
52+
</p-multiSelect>
3953
@if (isTopicsInvalid) {
4054
<small class="text-red-300">Topic(s) is required.</small>
4155
}

frontend/src/app/questions/question-dialog.component.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { QuestionService } from '../../_services/question.service';
1010
import { Difficulty } from './difficulty.model';
1111
import { Topic } from './topic.model';
1212
import { HttpErrorResponse } from '@angular/common/http';
13+
import { CommonModule } from '@angular/common';
1314

1415
@Component({
1516
selector: 'app-question-dialog',
@@ -22,6 +23,7 @@ import { HttpErrorResponse } from '@angular/common/http';
2223
MultiSelectModule,
2324
DropdownModule,
2425
QuestionDialogComponent,
26+
CommonModule,
2527
],
2628
providers: [QuestionService, ConfirmationService, MessageService],
2729
templateUrl: './question-dialog.component.html',
@@ -45,6 +47,12 @@ export class QuestionDialogComponent implements OnInit {
4547

4648
questions: Question[] = [];
4749

50+
topicSearchValue = '';
51+
52+
isNoResultsFound = false;
53+
54+
filteredTopics: Topic[] = [];
55+
4856
constructor(private questionService: QuestionService) {}
4957

5058
ngOnInit(): void {
@@ -153,4 +161,31 @@ export class QuestionDialogComponent implements OnInit {
153161
description: '',
154162
});
155163
}
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+
}
156191
}

frontend/src/app/questions/questions.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ export class QuestionsComponent implements OnInit {
9292
this.question = {} as Question;
9393
this.submitted = false;
9494
this.isDialogVisible = true;
95-
console.log('isDialogVisible: ' + this.isDialogVisible);
9695
}
9796

9897
editQuestion(question: Question) {

0 commit comments

Comments
 (0)