|
- import {AfterViewInit, Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
- import {FormGroup} from "@angular/forms";
- import {contactForm} from "@app/_forms/apiForms";
- import {ContactJsonld, ContactService, MediaObjectService} from "@app/core/api/v1";
- import {firstValueFrom, Subscription, window} from "rxjs";
- import {ModalStatus} from "@app/_helpers/modal.states";
- import {FormGroupInitializer} from "@app/_helpers/formgroup.initializer";
- import {TranslateService} from "@ngx-translate/core";
- import {AppHelperService} from "@app/_helpers/app-helper.service";
-
-
- @Component({
- selector: 'app-new-contact',
- templateUrl: './new-contact.component.html',
- styleUrl: './new-contact.component.scss'
- })
- export class NewContactComponent implements OnInit, AfterViewInit {
- @Input() public contact!: ContactJsonld;
- @Output() public submit: EventEmitter<ModalStatus> = new EventEmitter<ModalStatus>();
-
- protected contactForm: FormGroup;
- protected selectedImage: File | null;
- protected contactSub: Subscription;
- protected mediaSub: Subscription;
-
- protected birthdayValue: string;
-
- constructor(
- private contactService: ContactService,
- private mediaObjectService: MediaObjectService,
- private translateService: TranslateService,
- protected appHelperService: AppHelperService,
- ) {
- this.contactForm = contactForm;
- this.selectedImage = null;
-
- this.contactSub = new Subscription();
- this.mediaSub = new Subscription();
-
- this.birthdayValue = "";
-
- }
-
- ngOnInit(): void {
- console.log(this.contact);
- this.contactForm = FormGroupInitializer.initFormGroup(this.contactForm, this.contact);
- }
-
- ngAfterViewInit(): void {
- }
-
-
-
- protected onBirthdayChange(selectedItem: any) {
- // Set T12:00 for correct string
- let selectedItemValue = null;
- if (selectedItem.target.value !== "") {
- selectedItemValue = selectedItem.target.value + "T12:00";
- }
- this.contactForm.get('birthday')?.setValue(selectedItemValue);
- }
-
- // On submit form: Check if image is set
- onSubmit() {
- if (this.selectedImage !== null) {
- this.mediaSub = this.mediaObjectService.mediaObjectsPost(
- this.selectedImage
- ).subscribe(
- data => {
- this.contactForm.patchValue({"image": data.id});
- this.submitForm();
- }
- );
- } else {
- this.submitForm();
- }
- }
-
- // Submit all values of the form to the backend
- submitForm() {
- if (this.contactForm.valid) {
- if (this.contact.id === null || this.contact.id === undefined) {
- // Create new contact
- this.contactSub = this.contactService.contactsPost(
- this.contactForm.value as ContactJsonld
- ).subscribe(
- data => {
- this.contactForm.reset();
- this.submit.emit(ModalStatus.Submitted);
- }
- );
- } else {
- // Edit contact
- this.contactSub = this.contactService.contactsIdPatch(
- this.appHelperService.extractId(this.contact.id),
- this.contactForm.value as ContactJsonld
- ).subscribe(
- data => {
- this.contactForm.reset();
- this.submit.emit(ModalStatus.Submitted);
- }
- );
- }
- }
- }
-
- // If file is selected, convert it
- onFileSelected(event: any) {
- const file: File = event.target.files[0];
- if (file) {
- this.selectedImage = file;
- }
- }
-
- onDeleteImage() {
- let confirmMessage = "";
- this.translateService.get('system.confirm-delete-image').subscribe((translation: string) => {
- confirmMessage = translation;
- });
- const userConfirmed = confirm(confirmMessage);
- if (userConfirmed) {
- this.contactForm.patchValue({"image": null});
- this.contactForm.patchValue({"imageUrl": null});
- }
- }
- }
|