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.
 
 
 
 

86 lines
3.0 KiB

  1. import {Component, EventEmitter, Input, OnInit, Output, ViewChild} 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 {
  6. PartnerJsonld,
  7. PartnerService,
  8. PostJsonld,
  9. PostService,
  10. ProductJsonld,
  11. ProductService
  12. } from "@app/core/api/v1";
  13. import {FormGroupInitializer} from "@app/_helpers/formgroup.initializer";
  14. import {AppHelperService} from "@app/_helpers/app-helper.service";
  15. import {SearchSelectComponent} from "@app/_components/search-select/search-select.component";
  16. import {ListGetDataFunctionType} from "@app/_components/list/list-get-data-function-type";
  17. @Component({
  18. selector: 'app-new-post',
  19. templateUrl: './new-post.component.html',
  20. styleUrl: './new-post.component.scss'
  21. })
  22. export class NewPostComponent implements OnInit {
  23. @Input() public posting!: PostJsonld;
  24. @Input() public product!: ProductJsonld;
  25. @Input() public partner!: PartnerJsonld;
  26. @Output() public submit: EventEmitter<ModalStatus> = new EventEmitter<ModalStatus>();
  27. @ViewChild('productSearchSelect', { static: false }) productSearchSelect!: SearchSelectComponent;
  28. protected readonly SearchSelectComponent = SearchSelectComponent;
  29. protected postForm: FormGroup;
  30. constructor(
  31. private postService: PostService,
  32. protected productService: ProductService,
  33. protected partnerService: PartnerService,
  34. protected appHelperService: AppHelperService,
  35. ) {
  36. this.postForm = postForm;
  37. }
  38. ngOnInit(): void {
  39. this.postForm = FormGroupInitializer.initFormGroup(this.postForm, this.posting);
  40. this.partnerService.partnersIdGet(this.appHelperService.extractId(this.posting.partnerIri)).subscribe(
  41. data => {
  42. this.partner = data;
  43. }
  44. );
  45. }
  46. getProducts: ListGetDataFunctionType = (index: number, pageSize: number, term?: string) => {
  47. return this.productService.productsGetCollection(
  48. index,
  49. pageSize,
  50. term
  51. );
  52. }
  53. onSubmit() {
  54. if (this.postForm.valid) {
  55. if (this.posting.id === null || this.posting.id === undefined) {
  56. // Create new post
  57. this.postService.postsPost(
  58. this.postForm.value as PostJsonld
  59. ).subscribe(
  60. data => {
  61. this.postForm.reset();
  62. this.submit.emit(ModalStatus.Submitted);
  63. }
  64. );
  65. } else {
  66. // Edit post
  67. this.postService.postsIdPatch(
  68. this.appHelperService.extractId(this.posting.id),
  69. this.postForm.value as PostJsonld
  70. ).subscribe(
  71. data => {
  72. this.postForm.reset();
  73. this.submit.emit(ModalStatus.Submitted);
  74. }
  75. );
  76. }
  77. }
  78. }
  79. }