bstlboard-front/src/app/ui/commentform/commentform.ui.ts

54 lines
1.4 KiB
TypeScript

import { Component, OnInit, ViewChild, Input } from '@angular/core';
import { CommentService } from '../../services/comment.service';
import { FormBuilder, FormGroup, Validators, NgForm } from '@angular/forms';
import { Output, EventEmitter } from '@angular/core';
@Component({
selector: 'ui-commentform',
templateUrl: './commentform.ui.html',
styleUrls: ['./commentform.ui.scss']
})
export class UiCommentForm implements OnInit {
@Input() target: number;
@Input() parent: number;
@Output() replyCommentEvent = new EventEmitter<any>();
working: boolean = false;
form: FormGroup;
@ViewChild('formDirective') private formDirective: NgForm;
constructor(private commentService: CommentService,
private formBuilder: FormBuilder) { }
ngOnInit(): void {
this.form = this.formBuilder.group({
text: ['', Validators.required],
});
}
hasError(controlName: string): boolean {
return this.form.controls[controlName].errors != null;
}
create(): void {
if (this.working) {
return;
}
const comment: any = {};
comment.target = this.target;
comment.parent = this.parent;
comment.text = this.form.get("text").value;
this.working = true;
this.commentService.create(comment).subscribe({
next: (data) => {
this.formDirective.resetForm();
this.replyCommentEvent.emit(data);
this.working = false;
},
error: () => {
this.working = false;
}
});
}
}