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.
 
 
 
 

127 rader
4.2 KiB

  1. import {AfterViewInit, 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 {firstValueFrom, Subscription, window} 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 {AppHelperService} from "@app/_helpers/app-helper.service";
  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, AfterViewInit {
  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 appHelperService: AppHelperService,
  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. console.log(this.contact);
  37. this.contactForm = FormGroupInitializer.initFormGroup(this.contactForm, this.contact);
  38. }
  39. ngAfterViewInit(): void {
  40. }
  41. protected onBirthdayChange(selectedItem: any) {
  42. // Set T12:00 for correct string
  43. let selectedItemValue = null;
  44. if (selectedItem.target.value !== "") {
  45. selectedItemValue = selectedItem.target.value + "T12:00";
  46. }
  47. this.contactForm.get('birthday')?.setValue(selectedItemValue);
  48. }
  49. // On submit form: Check if image is set
  50. onSubmit() {
  51. if (this.selectedImage !== null) {
  52. this.mediaSub = this.mediaObjectService.mediaObjectsPost(
  53. this.selectedImage
  54. ).subscribe(
  55. data => {
  56. this.contactForm.patchValue({"image": data.id});
  57. this.submitForm();
  58. }
  59. );
  60. } else {
  61. this.submitForm();
  62. }
  63. }
  64. // Submit all values of the form to the backend
  65. submitForm() {
  66. if (this.contactForm.valid) {
  67. if (this.contact.id === null || this.contact.id === undefined) {
  68. // Create new contact
  69. this.contactSub = this.contactService.contactsPost(
  70. this.contactForm.value as ContactJsonld
  71. ).subscribe(
  72. data => {
  73. this.contactForm.reset();
  74. this.submit.emit(ModalStatus.Submitted);
  75. }
  76. );
  77. } else {
  78. // Edit contact
  79. this.contactSub = this.contactService.contactsIdPatch(
  80. this.appHelperService.extractId(this.contact.id),
  81. this.contactForm.value as ContactJsonld
  82. ).subscribe(
  83. data => {
  84. this.contactForm.reset();
  85. this.submit.emit(ModalStatus.Submitted);
  86. }
  87. );
  88. }
  89. }
  90. }
  91. // If file is selected, convert it
  92. onFileSelected(event: any) {
  93. const file: File = event.target.files[0];
  94. if (file) {
  95. this.selectedImage = file;
  96. }
  97. }
  98. onDeleteImage() {
  99. let confirmMessage = "";
  100. this.translateService.get('system.confirm-delete-image').subscribe((translation: string) => {
  101. confirmMessage = translation;
  102. });
  103. const userConfirmed = confirm(confirmMessage);
  104. if (userConfirmed) {
  105. this.contactForm.patchValue({"image": null});
  106. this.contactForm.patchValue({"imageUrl": null});
  107. }
  108. }
  109. }