import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; import {ModalStatus} from "@app/_helpers/modal.states"; import {FormGroup} from "@angular/forms"; import {postForm} from "@app/_forms/apiForms"; import {PostJsonld, PostService} from "@app/core/api/v1"; import {FormGroupInitializer} from "@app/_helpers/formgroup.initializer"; import {ApiConverter} from "@app/_helpers/api.converter"; import {Subscription} from "rxjs"; @Component({ selector: 'app-new-posting', templateUrl: './new-posting.component.html', styleUrl: './new-posting.component.scss' }) export class NewPostingComponent implements OnInit { @Input() public posting!: PostJsonld; @Output() public submit: EventEmitter = new EventEmitter(); protected postForm: FormGroup; protected postSub: Subscription; constructor( private postService: PostService ) { this.postForm = postForm; this.postSub = new Subscription(); } ngOnInit(): void { this.postForm = FormGroupInitializer.initFormGroup(this.postForm, this.posting); console.log(this.postForm); } onSubmit() { if (this.postForm.valid) { if (this.posting.id === null || this.posting.id === undefined) { // Create new post this.postSub = this.postService.postsPost( this.postForm.value as PostJsonld ).subscribe( data => { this.postForm.reset(); this.submit.emit(ModalStatus.Submitted); } ); } else { // Edit post this.postSub = this.postService.postsIdPatch( ApiConverter.extractId(this.posting.id), this.postForm.value as PostJsonld ).subscribe( data => { this.postForm.reset(); this.submit.emit(ModalStatus.Submitted); } ); } } } }