|
- 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 {PartnerJsonld, 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<ModalStatus> = new EventEmitter<ModalStatus>();
-
- protected postForm: FormGroup;
- protected postSub: Subscription;
-
- constructor(
- private postService: PostService,
- protected apiConverter: ApiConverter,
- ) {
- this.postForm = postForm;
- this.postSub = new Subscription();
- }
-
- ngOnInit(): void {
- this.postForm = FormGroupInitializer.initFormGroup(this.postForm, this.posting);
- console.log(this.posting);
- }
-
- 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(
- this.apiConverter.extractId(this.posting.id),
- this.postForm.value as PostJsonld
- ).subscribe(
- data => {
- this.postForm.reset();
- this.submit.emit(ModalStatus.Submitted);
- }
- );
- }
- }
- }
- }
|