Skip to content

Commit a4ea983

Browse files
committed
Fix minor issues
- Removed some of the @input in question-dialog - Updated ngIf to @if - Updated dialog box cancel button to use EventEmitter instead
1 parent 4d03d12 commit a4ea983

File tree

4 files changed

+53
-62
lines changed

4 files changed

+53
-62
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,9 @@
4343
</ng-template>
4444

4545
<ng-template pTemplate="footer" class="w-12 p-fluid">
46-
<p-button
47-
class="w-12 p-fluid"
48-
*ngIf="isNoResultsFound"
49-
type="button"
50-
label="Add as New Topic"
51-
icon="pi pi-plus"
52-
(click)="addNewTopic()" />
46+
@if (isNoResultsFound) {
47+
<p-button type="button" label="Add as New Topic" icon="pi pi-plus" (click)="addNewTopic()" />
48+
}
5349
</ng-template>
5450
</p-multiSelect>
5551
@if (isTopicsInvalid) {
@@ -90,7 +86,7 @@
9086
</form>
9187

9288
<ng-template pTemplate="footer">
93-
<p-button label="Cancel" [text]="true" severity="secondary" (onClick)="isDialogVisible = false" />
89+
<p-button label="Cancel" [text]="true" severity="secondary" (onClick)="dialogClose.emit()" />
9490
<p-button
9591
label="Save"
9692
class="p-button-success"

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

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +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';
13+
import { DifficultyLevels } from './difficulty-levels.enum';
1414

1515
@Component({
1616
selector: 'app-question-dialog',
@@ -23,18 +23,14 @@ import { CommonModule } from '@angular/common';
2323
MultiSelectModule,
2424
DropdownModule,
2525
QuestionDialogComponent,
26-
CommonModule,
2726
],
2827
providers: [QuestionService, ConfirmationService, MessageService],
2928
templateUrl: './question-dialog.component.html',
3029
styleUrl: './question-dialog.component.css',
3130
})
3231
export class QuestionDialogComponent implements OnInit {
3332
@Input() question!: Question;
34-
@Input() headerMessage!: string;
3533
@Input() isDialogVisible = false;
36-
@Input() topics!: Topic[];
37-
@Input() difficulties!: Difficulty[];
3834
@Output() dialogClose = new EventEmitter<void>();
3935
@Output() questionUpdate = new EventEmitter<Question>();
4036
@Output() questionAdd = new EventEmitter<Question>();
@@ -45,10 +41,14 @@ export class QuestionDialogComponent implements OnInit {
4541

4642
submitted = false;
4743

48-
questions: Question[] = [];
49-
5044
topicSearchValue = '';
5145

46+
headerMessage = '';
47+
48+
topics!: Topic[];
49+
50+
difficulties!: Difficulty[];
51+
5252
isNoResultsFound = false;
5353

5454
filteredTopics: Topic[] = [];
@@ -57,6 +57,10 @@ export class QuestionDialogComponent implements OnInit {
5757

5858
ngOnInit(): void {
5959
this.initFormGroup();
60+
61+
this.initDifficulties();
62+
63+
this.initTopics();
6064
}
6165

6266
get isTitleInvalid(): boolean {
@@ -105,6 +109,16 @@ export class QuestionDialogComponent implements OnInit {
105109

106110
show() {
107111
this.setFormValue();
112+
this.setHeaderMessage();
113+
}
114+
115+
setHeaderMessage() {
116+
if (this.question.id) {
117+
this.headerMessage = 'Edit Question';
118+
} else {
119+
this.submitted = false;
120+
this.headerMessage = 'Create new question';
121+
}
108122
}
109123

110124
handleEditQuestionResponse(id: number, question: QuestionBody) {
@@ -144,6 +158,30 @@ export class QuestionDialogComponent implements OnInit {
144158
});
145159
}
146160

161+
initDifficulties() {
162+
this.difficulties = [
163+
{ label: DifficultyLevels.EASY, value: DifficultyLevels.EASY },
164+
{ label: DifficultyLevels.MEDIUM, value: DifficultyLevels.MEDIUM },
165+
{ label: DifficultyLevels.HARD, value: DifficultyLevels.HARD },
166+
];
167+
}
168+
169+
initTopics() {
170+
this.questionService.getTopics().subscribe({
171+
next: response => {
172+
this.topics =
173+
response.data?.map(topic => ({
174+
label: topic,
175+
value: topic,
176+
})) || [];
177+
},
178+
error: (error: HttpErrorResponse) => {
179+
this.topics = [];
180+
this.errorReceive.emit('Failed to load topics. ' + error.error.message);
181+
},
182+
});
183+
}
184+
147185
setFormValue() {
148186
this.questionFormGroup.patchValue({
149187
title: this.question.title,

frontend/src/app/questions/questions.component.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ <h3 class="m-0">Manage Questions</h3>
7777
</ng-container>
7878
<app-question-dialog
7979
[isDialogVisible]="isDialogVisible"
80-
[headerMessage]="dialogHeader"
81-
[topics]="topics"
82-
[difficulties]="difficulties"
8380
[question]="question"
8481
(dialogClose)="onDialogClose()"
8582
(questionUpdate)="onQuestionUpdate($event)"

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

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@ import { ConfirmationService, MessageService } from 'primeng/api';
99
import { ConfirmDialogModule } from 'primeng/confirmdialog';
1010
import { DialogModule } from 'primeng/dialog';
1111
import { MultiSelectModule } from 'primeng/multiselect';
12-
import { FormGroup, ReactiveFormsModule } from '@angular/forms';
12+
import { ReactiveFormsModule } from '@angular/forms';
1313
import { DropdownModule } from 'primeng/dropdown';
1414
import { ProgressSpinnerModule } from 'primeng/progressspinner';
1515
import { Question } from './question.model';
16-
import { Column } from './column.model';
17-
import { Topic } from './topic.model';
18-
import { Difficulty } from './difficulty.model';
19-
import { DifficultyLevels } from './difficulty-levels.enum';
2016
import { QuestionService } from '../../_services/question.service';
2117
import { forkJoin } from 'rxjs';
2218
import { HttpErrorResponse } from '@angular/common/http';
@@ -51,26 +47,12 @@ export class QuestionsComponent implements OnInit {
5147

5248
questions: Question[] = [];
5349

54-
topics!: Topic[];
55-
56-
difficulties!: Difficulty[];
57-
58-
questionFormGroup!: FormGroup;
59-
60-
difficulty!: string;
61-
6250
question!: Question;
6351

6452
selectedQuestions!: Question[] | null;
6553

66-
submitted = false;
67-
6854
isDialogVisible = false;
6955

70-
cols: Column[] = [];
71-
72-
dialogHeader = '';
73-
7456
constructor(
7557
private questionService: QuestionService,
7658
private messageService: MessageService,
@@ -83,19 +65,14 @@ export class QuestionsComponent implements OnInit {
8365

8466
// fetch data from API call
8567
this.handleInitData();
86-
87-
this.initDifficulties();
8868
}
8969

9070
openNewQuestion() {
91-
this.dialogHeader = 'Create new question';
9271
this.question = {} as Question;
93-
this.submitted = false;
9472
this.isDialogVisible = true;
9573
}
9674

9775
editQuestion(question: Question) {
98-
this.dialogHeader = 'Edit Question';
9976
this.question = question;
10077
this.isDialogVisible = true;
10178
}
@@ -111,14 +88,6 @@ export class QuestionsComponent implements OnInit {
11188
});
11289
}
11390

114-
initDifficulties() {
115-
this.difficulties = [
116-
{ label: DifficultyLevels.EASY, value: DifficultyLevels.EASY },
117-
{ label: DifficultyLevels.MEDIUM, value: DifficultyLevels.MEDIUM },
118-
{ label: DifficultyLevels.HARD, value: DifficultyLevels.HARD },
119-
];
120-
}
121-
12291
initQuestion() {
12392
this.question = {
12493
id: -1,
@@ -148,21 +117,12 @@ export class QuestionsComponent implements OnInit {
148117
}
149118

150119
handleInitData() {
151-
forkJoin({
152-
questions: this.questionService.getQuestions(),
153-
topics: this.questionService.getTopics(),
154-
}).subscribe({
155-
next: results => {
156-
this.questions = results.questions.data || [];
157-
this.topics =
158-
results.topics.data?.map(topic => ({
159-
label: topic,
160-
value: topic,
161-
})) || [];
120+
this.questionService.getQuestions().subscribe({
121+
next: response => {
122+
this.questions = response.data || [];
162123
},
163124
error: () => {
164125
this.questions = [];
165-
this.topics = [];
166126
this.onErrorReceive('Failed to load data. Please try again later.');
167127
},
168128
complete: () => {

0 commit comments

Comments
 (0)