Skip to content

Commit 60464d3

Browse files
committed
Update to use formGroup.value instead of field listeners
1 parent 9e8fc42 commit 60464d3

File tree

2 files changed

+21
-53
lines changed

2 files changed

+21
-53
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ <h3 class="m-0">Manage Questions</h3>
103103
required="true"
104104
[style]="{ width: '100%' }"
105105
[options]="topics"
106-
formControlName="selectedTopics"
106+
formControlName="topics"
107107
optionLabel="label"
108108
optionValue="value"
109109
placeholder="Select Topics" />
@@ -117,7 +117,7 @@ <h3 class="m-0">Manage Questions</h3>
117117
[required]="true"
118118
[style]="{ width: '100%' }"
119119
[options]="difficulties"
120-
formControlName="selectedDifficulty"
120+
formControlName="difficulty"
121121
optionLabel="label"
122122
optionValue="value"
123123
placeholder="Select Difficulty" />

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

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class QuestionsComponent implements OnInit {
5555

5656
questionFormGroup!: FormGroup;
5757

58-
selectedDifficulty!: string;
58+
difficulty!: string;
5959

6060
question!: Question;
6161

@@ -84,7 +84,7 @@ export class QuestionsComponent implements OnInit {
8484

8585
this.initFormGroup();
8686

87-
this.initListeners();
87+
// this.initListeners();
8888
}
8989

9090
get isTitleInvalid(): boolean {
@@ -98,12 +98,12 @@ export class QuestionsComponent implements OnInit {
9898
}
9999

100100
get isDifficultyInvalid(): boolean {
101-
const difficultyControl = this.questionFormGroup.controls['selectedDifficulty'];
101+
const difficultyControl = this.questionFormGroup.controls['difficulty'];
102102
return difficultyControl.dirty && difficultyControl.invalid;
103103
}
104104

105105
get isTopicsInvalid(): boolean {
106-
const topicsControl = this.questionFormGroup.controls['selectedTopics'];
106+
const topicsControl = this.questionFormGroup.controls['topics'];
107107
return topicsControl.dirty && topicsControl.invalid;
108108
}
109109

@@ -128,20 +128,13 @@ export class QuestionsComponent implements OnInit {
128128
saveQuestion() {
129129
this.submitted = true;
130130

131-
if (
132-
!this.question.title?.trim() ||
133-
!this.question.topics ||
134-
this.question.topics?.length == 0 ||
135-
!this.question.difficulty?.trim() ||
136-
!this.question.description?.trim()
137-
) {
131+
if (!this.questionFormGroup.valid) {
138132
return;
139133
}
140134

141135
if (this.question.id) {
142136
// update
143-
const { id, ...questionBody } = this.question;
144-
this.handleEditQuestionResponse(id, questionBody);
137+
this.handleEditQuestionResponse(this.question.id, this.questionFormGroup.value);
145138
} else {
146139
// add
147140
this.handleAddQuestionResponse();
@@ -153,50 +146,28 @@ export class QuestionsComponent implements OnInit {
153146

154147
resetFormGroup() {
155148
this.questionFormGroup.reset({
156-
selectedTopics: [],
157-
selectedDifficulty: '',
158-
textTitle: '',
159-
textDescription: '',
149+
topics: [],
150+
difficulty: '',
151+
title: '',
152+
description: '',
160153
});
161154
}
162155

163156
editQuestion(question: Question) {
164157
this.question.id = question.id;
165158
this.questionFormGroup.patchValue({
166-
textTitle: question.title,
167-
textDescription: question.description,
168-
selectedTopics: question.topics,
169-
selectedDifficulty: question.difficulty,
159+
title: question.title,
160+
description: question.description,
161+
topics: question.topics,
162+
difficulty: question.difficulty,
170163
});
171164
this.isDialogVisible = true;
172165
}
173166

174-
initListeners() {
175-
// Dropdown difficulty listener
176-
this.questionFormGroup.get('selectedDifficulty')?.valueChanges.subscribe(v => {
177-
this.question.difficulty = v;
178-
});
179-
180-
// Multiselect topics listener
181-
this.questionFormGroup.get('selectedTopics')?.valueChanges.subscribe(v => {
182-
this.question.topics = v;
183-
});
184-
185-
// text title listener
186-
this.questionFormGroup.get('textTitle')?.valueChanges.subscribe(v => {
187-
this.question.title = v;
188-
});
189-
190-
// text description listener
191-
this.questionFormGroup.get('textDescription')?.valueChanges.subscribe(v => {
192-
this.question.description = v;
193-
});
194-
}
195-
196167
initFormGroup() {
197168
this.questionFormGroup = new FormGroup({
198-
selectedTopics: new FormControl<string[] | null>([], [Validators.required]),
199-
selectedDifficulty: new FormControl<Difficulty[] | null>([], [Validators.required]),
169+
topics: new FormControl<string[] | null>([], [Validators.required]),
170+
difficulty: new FormControl<Difficulty[] | null>([], [Validators.required]),
200171
title: new FormControl<string | null>('', [Validators.required]),
201172
description: new FormControl<string | null>('', [Validators.required]),
202173
});
@@ -221,14 +192,13 @@ export class QuestionsComponent implements OnInit {
221192
}
222193

223194
handleAddQuestionResponse() {
224-
this.questionService.addQuestion(this.question).subscribe({
195+
this.questionService.addQuestion(this.questionFormGroup.value).subscribe({
225196
next: (response: SingleQuestionResponse) => {
226197
if (this.questions) {
227198
this.questions = [...this.questions, response.data];
228199
}
229200
},
230201
error: (error: HttpErrorResponse) => {
231-
console.log(error);
232202
this.messageService.add({
233203
severity: 'error',
234204
summary: 'Error',
@@ -280,10 +250,9 @@ export class QuestionsComponent implements OnInit {
280250
this.questionService.updateQuestion(id, question).subscribe({
281251
next: (response: SingleQuestionResponse) => {
282252
this.questions[this.questions.findIndex(x => x.id == id)] = response.data;
253+
this.questions = [...this.questions];
283254
},
284255
error: (error: HttpErrorResponse) => {
285-
console.log(error);
286-
console.log(question);
287256
this.messageService.add({
288257
severity: 'error',
289258
summary: 'Error',
@@ -315,8 +284,7 @@ export class QuestionsComponent implements OnInit {
315284
value: topic,
316285
})) || [];
317286
},
318-
error: (error: Error) => {
319-
console.error(error);
287+
error: () => {
320288
this.questions = [];
321289
this.topics = [];
322290
this.messageService.add({

0 commit comments

Comments
 (0)