Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

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