You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

62 regels
2.1 KiB

  1. import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
  2. import {ModalStatus} from "@app/_helpers/modal.states";
  3. import {FormGroup} from "@angular/forms";
  4. import {postForm} from "@app/_forms/apiForms";
  5. import {PartnerJsonld, PostJsonld, PostService} from "@app/core/api/v1";
  6. import {FormGroupInitializer} from "@app/_helpers/formgroup.initializer";
  7. import {ApiConverter} from "@app/_helpers/api.converter";
  8. import {Subscription} from "rxjs";
  9. @Component({
  10. selector: 'app-new-posting',
  11. templateUrl: './new-posting.component.html',
  12. styleUrl: './new-posting.component.scss'
  13. })
  14. export class NewPostingComponent implements OnInit {
  15. @Input() public posting!: PostJsonld;
  16. @Output() public submit: EventEmitter<ModalStatus> = new EventEmitter<ModalStatus>();
  17. protected postForm: FormGroup;
  18. protected postSub: Subscription;
  19. constructor(
  20. private postService: PostService,
  21. protected apiConverter: ApiConverter,
  22. ) {
  23. this.postForm = postForm;
  24. this.postSub = new Subscription();
  25. }
  26. ngOnInit(): void {
  27. this.postForm = FormGroupInitializer.initFormGroup(this.postForm, this.posting);
  28. console.log(this.posting);
  29. }
  30. onSubmit() {
  31. if (this.postForm.valid) {
  32. if (this.posting.id === null || this.posting.id === undefined) {
  33. // Create new post
  34. this.postSub = this.postService.postsPost(
  35. this.postForm.value as PostJsonld
  36. ).subscribe(
  37. data => {
  38. this.postForm.reset();
  39. this.submit.emit(ModalStatus.Submitted);
  40. }
  41. );
  42. } else {
  43. // Edit post
  44. this.postSub = this.postService.postsIdPatch(
  45. this.apiConverter.extractId(this.posting.id),
  46. this.postForm.value as PostJsonld
  47. ).subscribe(
  48. data => {
  49. this.postForm.reset();
  50. this.submit.emit(ModalStatus.Submitted);
  51. }
  52. );
  53. }
  54. }
  55. }
  56. }