Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

113 rader
3.8 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 {ApiConverter} from "@app/_helpers/api.converter";
  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. ) {
  28. this.partnerForm = partnerForm;
  29. this.selectedImage = null;
  30. this.partnerSub = new Subscription();
  31. this.mediaSub = new Subscription();
  32. this.partnerNameOne = "";
  33. }
  34. ngOnInit(): void {
  35. this.partnerForm = FormGroupInitializer.initFormGroup(this.partnerForm, this.partner);
  36. this.translateService.get('basic.' + this.partner.partnerType + 'One').subscribe((translation: string) => {
  37. this.partnerNameOne = translation;
  38. });
  39. }
  40. // On submit form: Check if image is set
  41. onSubmit() {
  42. if (this.selectedImage !== null) {
  43. this.mediaSub = this.mediaObjectService.mediaObjectsPost(
  44. this.selectedImage
  45. ).subscribe(
  46. data => {
  47. this.partnerForm.patchValue({"logo": data.id});
  48. this.submitForm();
  49. }
  50. );
  51. } else {
  52. this.submitForm();
  53. }
  54. }
  55. // Submit all values of the form to the backend
  56. submitForm() {
  57. if (this.partnerForm.valid) {
  58. if (this.partner.id === null || this.partner.id === undefined) {
  59. // Create new contact
  60. this.partnerSub = this.partnerService.partnersPost(
  61. this.partnerForm.value as PartnerJsonld
  62. ).subscribe(
  63. data => {
  64. this.partnerForm.reset();
  65. this.submit.emit(ModalStatus.Submitted);
  66. }
  67. );
  68. } else {
  69. // Edit contact
  70. this.partnerSub = this.partnerService.partnersIdPatch(
  71. ApiConverter.extractId(this.partner.id),
  72. this.partnerForm.value as PartnerJsonld
  73. ).subscribe(
  74. data => {
  75. this.partnerForm.reset();
  76. this.submit.emit(ModalStatus.Submitted);
  77. }
  78. );
  79. }
  80. }
  81. }
  82. // If file is selected, convert it
  83. onFileSelected(event: any) {
  84. const file: File = event.target.files[0];
  85. if (file) {
  86. this.selectedImage = file;
  87. }
  88. }
  89. onDeleteImage() {
  90. let confirmMessage = "";
  91. this.translateService.get('system.confirm-delete-image').subscribe((translation: string) => {
  92. confirmMessage = translation;
  93. });
  94. const userConfirmed = window.confirm(confirmMessage);
  95. if (userConfirmed) {
  96. this.partnerForm.patchValue({"logo": null});
  97. this.partnerForm.patchValue({"logoUrl": null});
  98. }
  99. }
  100. }