Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

61 rinda
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 {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. ) {
  22. this.postForm = postForm;
  23. this.postSub = new Subscription();
  24. }
  25. ngOnInit(): void {
  26. this.postForm = FormGroupInitializer.initFormGroup(this.postForm, this.posting);
  27. console.log(this.postForm);
  28. }
  29. onSubmit() {
  30. if (this.postForm.valid) {
  31. if (this.posting.id === null || this.posting.id === undefined) {
  32. // Create new post
  33. this.postSub = this.postService.postsPost(
  34. this.postForm.value as PostJsonld
  35. ).subscribe(
  36. data => {
  37. this.postForm.reset();
  38. this.submit.emit(ModalStatus.Submitted);
  39. }
  40. );
  41. } else {
  42. // Edit post
  43. this.postSub = this.postService.postsIdPatch(
  44. ApiConverter.extractId(this.posting.id),
  45. this.postForm.value as PostJsonld
  46. ).subscribe(
  47. data => {
  48. this.postForm.reset();
  49. this.submit.emit(ModalStatus.Submitted);
  50. }
  51. );
  52. }
  53. }
  54. }
  55. }