Skip to content

Commit 4d3cb1c

Browse files
committed
Improve add new topic behaviour
Upon adding a new topic, * Immediately adds the topic instead of the user having to press the new topic checkbox * Clear the search filter box since there would be no other results
1 parent 4d03d12 commit 4d3cb1c

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<label for="questionTopics">Topics</label>
3030

3131
<p-multiSelect
32+
#topicSelector
3233
required="true"
3334
class="w-12"
3435
[style]="{ width: '100%' }"
@@ -45,7 +46,7 @@
4546
<ng-template pTemplate="footer" class="w-12 p-fluid">
4647
<p-button
4748
class="w-12 p-fluid"
48-
*ngIf="isNoResultsFound"
49+
*ngIf="hasNoResultsFound"
4950
type="button"
5051
label="Add as New Topic"
5152
icon="pi pi-plus"

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Component, EventEmitter, Input, Output, OnInit } from '@angular/core';
1+
import { Component, EventEmitter, Input, Output, OnInit, ViewChild } from '@angular/core';
22
import { Question, QuestionBody, SingleQuestionResponse } from './question.model';
33
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
44
import { DialogModule } from 'primeng/dialog';
5-
import { MultiSelectModule } from 'primeng/multiselect';
5+
import { MultiSelect, MultiSelectModule } from 'primeng/multiselect';
66
import { DropdownModule } from 'primeng/dropdown';
77
import { ButtonModule } from 'primeng/button';
88
import { ConfirmationService, MessageService } from 'primeng/api';
@@ -30,6 +30,7 @@ import { CommonModule } from '@angular/common';
3030
styleUrl: './question-dialog.component.css',
3131
})
3232
export class QuestionDialogComponent implements OnInit {
33+
@ViewChild('topicSelector') topicSelector!: MultiSelect;
3334
@Input() question!: Question;
3435
@Input() headerMessage!: string;
3536
@Input() isDialogVisible = false;
@@ -42,23 +43,22 @@ export class QuestionDialogComponent implements OnInit {
4243
@Output() successfulRequest = new EventEmitter<string>();
4344

4445
questionFormGroup!: FormGroup;
45-
4646
submitted = false;
4747

4848
questions: Question[] = [];
49-
5049
topicSearchValue = '';
51-
52-
isNoResultsFound = false;
53-
54-
filteredTopics: Topic[] = [];
50+
hasNoResultsFound = false;
5551

5652
constructor(private questionService: QuestionService) {}
5753

5854
ngOnInit(): void {
5955
this.initFormGroup();
6056
}
6157

58+
get topicControl(): FormControl {
59+
return this.questionFormGroup.controls['topics'] as FormControl;
60+
}
61+
6262
get isTitleInvalid(): boolean {
6363
const titleControl = this.questionFormGroup.controls['title'];
6464
return titleControl.dirty && titleControl.invalid;
@@ -164,28 +164,29 @@ export class QuestionDialogComponent implements OnInit {
164164

165165
onFilterTopics(event: { filter: string }) {
166166
this.topicSearchValue = event.filter;
167-
168-
this.filteredTopics = this.topics.filter(topic =>
167+
this.hasNoResultsFound = !this.topics.some(topic =>
169168
topic.label.toLowerCase().includes(this.topicSearchValue.toLowerCase()),
170169
);
171-
172-
this.isNoResultsFound = this.filteredTopics.length == 0;
173170
}
174171

175172
addNewTopic() {
173+
const newTopic = this.topicSearchValue;
176174
const newValue: Topic = {
177-
label: this.topicSearchValue,
178-
value: this.topicSearchValue,
175+
label: newTopic,
176+
value: newTopic,
179177
};
180178

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());
186180

187-
if (!topicExists) {
188-
this.topics = [...this.topics, newValue];
181+
if (topicExists) {
182+
return;
189183
}
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;
190191
}
191192
}

0 commit comments

Comments
 (0)