Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

109 строки
3.7 KiB

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