|
|
@@ -1,4 +1,4 @@
|
|
|
-import { Component, inject, OnInit } from '@angular/core';
|
|
|
+import { Component, EventEmitter, inject, Input, OnInit, Output } from '@angular/core';
|
|
|
import { AsyncPipe } from '@angular/common';
|
|
|
import { DataService } from '../data-service';
|
|
|
import { Observable } from 'rxjs';
|
|
|
@@ -10,7 +10,7 @@ import { Router } from '@angular/router';
|
|
|
@Component({
|
|
|
selector: 'is-article-form',
|
|
|
templateUrl: './article-form.html',
|
|
|
- styleUrl: 'article-form.css',
|
|
|
+ styleUrl: './article-form.css',
|
|
|
imports: [
|
|
|
AsyncPipe,
|
|
|
FormsModule,
|
|
|
@@ -18,15 +18,27 @@ import { Router } from '@angular/router';
|
|
|
]
|
|
|
})
|
|
|
export class ArticleForm implements OnInit {
|
|
|
- formSubmitted = false;
|
|
|
- articleForm = new FormGroup({
|
|
|
- title: new FormControl('', Validators.required),
|
|
|
- text: new FormControl('', Validators.required),
|
|
|
- categoryName: new FormControl('', Validators.required),
|
|
|
- articleGroup: new FormControl(null, Validators.required),
|
|
|
- })
|
|
|
private dataService = inject(DataService)
|
|
|
|
|
|
+ @Input() title?: string;
|
|
|
+ @Input() submitButtonName?: string;
|
|
|
+
|
|
|
+ @Input()
|
|
|
+ articleInput: ArticleInput = { title: null, text: null, categoryName: null, articleGroupId: null };
|
|
|
+ @Output()
|
|
|
+ articleInputChange = new EventEmitter<ArticleInput>();
|
|
|
+
|
|
|
+ // title = input.required<string>();
|
|
|
+ formSubmitted = false;
|
|
|
+ articleForm = new FormGroup({
|
|
|
+ title: new FormControl<string | null>('', Validators.required),
|
|
|
+ text: new FormControl<string | null>('', Validators.required),
|
|
|
+ categoryName: new FormControl<CategoryName|null>(
|
|
|
+ CategoryName.sport,
|
|
|
+ Validators.required
|
|
|
+ ),
|
|
|
+ articleGroupId: new FormControl<number|null>(null, Validators.required),
|
|
|
+ });
|
|
|
|
|
|
articleGroups$?: Observable<ArticleGroup[]>;
|
|
|
categoryNames: CategoryName[] = Object.values(CategoryName);
|
|
|
@@ -35,6 +47,12 @@ export class ArticleForm implements OnInit {
|
|
|
}
|
|
|
|
|
|
ngOnInit() {
|
|
|
+ this.articleForm.patchValue({
|
|
|
+ title: this.articleInput.title,
|
|
|
+ text: this.articleInput.text,
|
|
|
+ categoryName: this.articleInput.categoryName,
|
|
|
+ articleGroupId: this.articleInput.articleGroupId,
|
|
|
+ });
|
|
|
this.articleGroups$ = this.dataService.getArticleGroups();
|
|
|
this.listenToInputChange("title");
|
|
|
this.listenToInputChange("text");
|
|
|
@@ -45,33 +63,37 @@ export class ArticleForm implements OnInit {
|
|
|
|
|
|
this.articleForm.updateValueAndValidity();
|
|
|
console.log(this.articleForm.valid);
|
|
|
- console.log(this.articleForm.controls.articleGroup.value);
|
|
|
+ console.log(this.articleForm.controls.articleGroupId.value);
|
|
|
console.log(this.articleForm.controls.title.errors);
|
|
|
console.log(this.articleForm.controls.text.errors);
|
|
|
console.log(this.articleForm.controls.categoryName.errors);
|
|
|
- console.log(this.articleForm.controls.articleGroup.errors);
|
|
|
+ console.log(this.articleForm.controls.articleGroupId.errors);
|
|
|
if (!this.articleForm.valid) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- let article: Article = {
|
|
|
- id: 0,
|
|
|
+ const articleInput: ArticleInput = {
|
|
|
title: this.articleForm.value.title!,
|
|
|
text: this.articleForm.value.text!,
|
|
|
- categoryName: this.articleForm.value.categoryName! as CategoryName,
|
|
|
- articleGroupId: this.articleForm.value.articleGroup!
|
|
|
+ categoryName: this.articleForm.value.categoryName!,
|
|
|
+ articleGroupId: this.articleForm.value.articleGroupId!
|
|
|
};
|
|
|
|
|
|
- this.dataService.postArticle(article).subscribe((article: Article) => {
|
|
|
- console.log(article)
|
|
|
- this.router.navigateByUrl('/');
|
|
|
- });
|
|
|
+ this.articleInputChange.emit(articleInput);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // this.dataService.postArticle(article).subscribe((article: Article) => {
|
|
|
+ // console.log(article)
|
|
|
+ // this.router.navigateByUrl('/');
|
|
|
+ // });
|
|
|
}
|
|
|
|
|
|
- listenToInputChange(controlName: keyof typeof this.articleForm.controls){
|
|
|
+ listenToInputChange(controlName: 'title' | 'text'){
|
|
|
console.log(this.articleForm.controls)
|
|
|
const control = this.articleForm.controls[controlName];
|
|
|
control.valueChanges.subscribe(value => {
|
|
|
+ if (typeof value !== 'string') return;
|
|
|
const regExp = new RegExp('^([A-Za-z]|\\s)*$')
|
|
|
const errors = { ...control.errors };
|
|
|
|
|
|
@@ -85,4 +107,11 @@ export class ArticleForm implements OnInit {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+}
|
|
|
+
|
|
|
+export interface ArticleInput {
|
|
|
+ title: string|null;
|
|
|
+ text: string|null;
|
|
|
+ categoryName: CategoryName|null;
|
|
|
+ articleGroupId: number|null;
|
|
|
}
|