|
| 1 | +import { Component, EventEmitter, Input, Output, OnInit } from '@angular/core'; |
| 2 | +import { Question, QuestionBody, SingleQuestionResponse } from './question.model'; |
| 3 | +import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms'; |
| 4 | +import { DialogModule } from 'primeng/dialog'; |
| 5 | +import { MultiSelectModule } from 'primeng/multiselect'; |
| 6 | +import { DropdownModule } from 'primeng/dropdown'; |
| 7 | +import { ButtonModule } from 'primeng/button'; |
| 8 | +import { ConfirmationService, MessageService } from 'primeng/api'; |
| 9 | +import { QuestionService } from '../../_services/question.service'; |
| 10 | +import { Difficulty } from './difficulty.model'; |
| 11 | +import { Topic } from './topic.model'; |
| 12 | +import { HttpErrorResponse } from '@angular/common/http'; |
| 13 | + |
| 14 | +@Component({ |
| 15 | + selector: 'app-question-dialog', |
| 16 | + standalone: true, |
| 17 | + imports: [ |
| 18 | + ButtonModule, |
| 19 | + DialogModule, |
| 20 | + ButtonModule, |
| 21 | + ReactiveFormsModule, |
| 22 | + MultiSelectModule, |
| 23 | + DropdownModule, |
| 24 | + QuestionDialogComponent, |
| 25 | + ], |
| 26 | + providers: [QuestionService, ConfirmationService, MessageService], |
| 27 | + templateUrl: './question-dialog.component.html', |
| 28 | + styleUrl: './question-dialog.component.css', |
| 29 | +}) |
| 30 | +export class QuestionDialogComponent implements OnInit { |
| 31 | + @Input() question!: Question; |
| 32 | + @Input() headerMessage!: string; |
| 33 | + @Input() isDialogVisible = false; |
| 34 | + @Input() topics!: Topic[]; |
| 35 | + @Input() difficulties!: Difficulty[]; |
| 36 | + @Output() dialogClose = new EventEmitter<void>(); |
| 37 | + @Output() questionUpdate = new EventEmitter<Question>(); |
| 38 | + @Output() questionAdd = new EventEmitter<Question>(); |
| 39 | + @Output() errorReceive = new EventEmitter<string>(); |
| 40 | + @Output() successfulRequest = new EventEmitter<string>(); |
| 41 | + |
| 42 | + questionFormGroup!: FormGroup; |
| 43 | + |
| 44 | + submitted = false; |
| 45 | + |
| 46 | + questions: Question[] = []; |
| 47 | + |
| 48 | + constructor(private questionService: QuestionService) {} |
| 49 | + |
| 50 | + ngOnInit(): void { |
| 51 | + this.initFormGroup(); |
| 52 | + } |
| 53 | + |
| 54 | + get isTitleInvalid(): boolean { |
| 55 | + const titleControl = this.questionFormGroup.controls['title']; |
| 56 | + return titleControl.dirty && titleControl.invalid; |
| 57 | + } |
| 58 | + |
| 59 | + get isDescriptionInvalid(): boolean { |
| 60 | + const descriptionControl = this.questionFormGroup.controls['description']; |
| 61 | + return descriptionControl.dirty && descriptionControl.invalid; |
| 62 | + } |
| 63 | + |
| 64 | + get isDifficultyInvalid(): boolean { |
| 65 | + const difficultyControl = this.questionFormGroup.controls['difficulty']; |
| 66 | + return difficultyControl.dirty && difficultyControl.invalid; |
| 67 | + } |
| 68 | + |
| 69 | + get isTopicsInvalid(): boolean { |
| 70 | + const topicsControl = this.questionFormGroup.controls['topics']; |
| 71 | + return topicsControl.dirty && topicsControl.invalid; |
| 72 | + } |
| 73 | + |
| 74 | + saveQuestion() { |
| 75 | + this.submitted = true; |
| 76 | + |
| 77 | + if (!this.questionFormGroup.valid) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if (this.question.id) { |
| 82 | + // update |
| 83 | + this.handleEditQuestionResponse(this.question.id, this.questionFormGroup.value); |
| 84 | + } else { |
| 85 | + // add |
| 86 | + this.handleAddQuestionResponse(); |
| 87 | + } |
| 88 | + |
| 89 | + this.dialogClose.emit(); |
| 90 | + this.question = {} as Question; |
| 91 | + } |
| 92 | + |
| 93 | + cancel() { |
| 94 | + this.resetFormGroup(); |
| 95 | + this.dialogClose.emit(); |
| 96 | + } |
| 97 | + |
| 98 | + show() { |
| 99 | + this.setFormValue(); |
| 100 | + } |
| 101 | + |
| 102 | + handleEditQuestionResponse(id: number, question: QuestionBody) { |
| 103 | + this.questionService.updateQuestion(id, question).subscribe({ |
| 104 | + next: (response: SingleQuestionResponse) => { |
| 105 | + this.questionUpdate.emit(response.data); |
| 106 | + }, |
| 107 | + error: (error: HttpErrorResponse) => { |
| 108 | + this.errorReceive.emit(error.error.message); |
| 109 | + }, |
| 110 | + complete: () => { |
| 111 | + this.successfulRequest.emit('Question has been updated successfully'); |
| 112 | + }, |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + handleAddQuestionResponse() { |
| 117 | + this.questionService.addQuestion(this.questionFormGroup.value).subscribe({ |
| 118 | + next: (response: SingleQuestionResponse) => { |
| 119 | + this.questionAdd.emit(response.data); |
| 120 | + }, |
| 121 | + error: (error: HttpErrorResponse) => { |
| 122 | + this.errorReceive.emit('Failed to add new question. ' + error.error.message); |
| 123 | + }, |
| 124 | + complete: () => { |
| 125 | + this.successfulRequest.emit('New Question Added'); |
| 126 | + }, |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + initFormGroup() { |
| 131 | + this.questionFormGroup = new FormGroup({ |
| 132 | + topics: new FormControl<string[] | null>([], [Validators.required]), |
| 133 | + difficulty: new FormControl<Difficulty[] | null>([], [Validators.required]), |
| 134 | + title: new FormControl<string | null>('', [Validators.required]), |
| 135 | + description: new FormControl<string | null>('', [Validators.required]), |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + setFormValue() { |
| 140 | + this.questionFormGroup.patchValue({ |
| 141 | + title: this.question.title, |
| 142 | + description: this.question.description, |
| 143 | + topics: this.question.topics, |
| 144 | + difficulty: this.question.difficulty, |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + resetFormGroup() { |
| 149 | + this.questionFormGroup.reset({ |
| 150 | + topics: [], |
| 151 | + difficulty: '', |
| 152 | + title: '', |
| 153 | + description: '', |
| 154 | + }); |
| 155 | + } |
| 156 | +} |
0 commit comments