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.
 
 
 
 

114 lines
3.9 KiB

  1. import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
  2. import {partnerForm} from "@app/_forms/apiForms";
  3. import {MediaObjectService, PartnerJsonld, PartnerService} from "@app/core/api/v1";
  4. import {ModalStatus} from "@app/_helpers/modal.states";
  5. import {FormGroup} from "@angular/forms";
  6. import {Subscription} from "rxjs";
  7. import {TranslateService} from "@ngx-translate/core";
  8. import {FormGroupInitializer} from "@app/_helpers/formgroup.initializer";
  9. import {AppHelperService} from "@app/_helpers/app-helper.service";
  10. @Component({
  11. selector: 'app-new-partner',
  12. templateUrl: './new-partner.component.html',
  13. styleUrl: './new-partner.component.scss'
  14. })
  15. export class NewPartnerComponent implements OnInit {
  16. @Input() public partner!: PartnerJsonld;
  17. @Output() public submit: EventEmitter<ModalStatus> = new EventEmitter<ModalStatus>();
  18. protected partnerForm: FormGroup;
  19. protected partnerSub: Subscription;
  20. protected selectedImage: File | null;
  21. protected mediaSub: Subscription;
  22. protected partnerNameOne: string;
  23. constructor(
  24. private partnerService: PartnerService,
  25. private mediaObjectService: MediaObjectService,
  26. private translateService: TranslateService,
  27. protected appHelperService: AppHelperService,
  28. ) {
  29. this.partnerForm = partnerForm;
  30. this.selectedImage = null;
  31. this.partnerSub = new Subscription();
  32. this.mediaSub = new Subscription();
  33. this.partnerNameOne = "";
  34. }
  35. ngOnInit(): void {
  36. this.partnerForm = FormGroupInitializer.initFormGroup(this.partnerForm, this.partner);
  37. this.translateService.get('basic.' + this.partner.partnerType + 'One').subscribe((translation: string) => {
  38. this.partnerNameOne = translation;
  39. });
  40. }
  41. // On submit form: Check if image is set
  42. onSubmit() {
  43. if (this.selectedImage !== null) {
  44. this.mediaSub = this.mediaObjectService.mediaObjectsPost(
  45. this.selectedImage
  46. ).subscribe(
  47. data => {
  48. this.partnerForm.patchValue({"logo": data.id});
  49. this.submitForm();
  50. }
  51. );
  52. } else {
  53. this.submitForm();
  54. }
  55. }
  56. // Submit all values of the form to the backend
  57. submitForm() {
  58. if (this.partnerForm.valid) {
  59. if (this.partner.id === null || this.partner.id === undefined) {
  60. // Create new contact
  61. this.partnerSub = this.partnerService.partnersPost(
  62. this.partnerForm.value as PartnerJsonld
  63. ).subscribe(
  64. data => {
  65. this.partnerForm.reset();
  66. this.submit.emit(ModalStatus.Submitted);
  67. }
  68. );
  69. } else {
  70. // Edit contact
  71. this.partnerSub = this.partnerService.partnersIdPatch(
  72. this.appHelperService.extractId(this.partner.id),
  73. this.partnerForm.value as PartnerJsonld
  74. ).subscribe(
  75. data => {
  76. this.partnerForm.reset();
  77. this.submit.emit(ModalStatus.Submitted);
  78. }
  79. );
  80. }
  81. }
  82. }
  83. // If file is selected, convert it
  84. onFileSelected(event: any) {
  85. const file: File = event.target.files[0];
  86. if (file) {
  87. this.selectedImage = file;
  88. }
  89. }
  90. onDeleteImage() {
  91. let confirmMessage = "";
  92. this.translateService.get('system.confirm-delete-image').subscribe((translation: string) => {
  93. confirmMessage = translation;
  94. });
  95. const userConfirmed = confirm(confirmMessage);
  96. if (userConfirmed) {
  97. this.partnerForm.patchValue({"logo": null});
  98. this.partnerForm.patchValue({"logoUrl": null});
  99. }
  100. }
  101. }