import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; import {partnerForm} from "@app/_forms/apiForms"; import {MediaObjectService, PartnerJsonld, PartnerService} from "@app/core/api/v1"; import {ModalStatus} from "@app/_helpers/modal.states"; import {FormGroup} from "@angular/forms"; import {Subscription} from "rxjs"; import {TranslateService} from "@ngx-translate/core"; import {FormGroupInitializer} from "@app/_helpers/formgroup.initializer"; import {AppHelperService} from "@app/_helpers/app-helper.service"; @Component({ selector: 'app-new-partner', templateUrl: './new-partner.component.html', styleUrl: './new-partner.component.scss' }) export class NewPartnerComponent implements OnInit { @Input() public partner!: PartnerJsonld; @Output() public submit: EventEmitter = new EventEmitter(); protected partnerForm: FormGroup; protected partnerSub: Subscription; protected selectedImage: File | null; protected mediaSub: Subscription; protected partnerNameOne: string; constructor( private partnerService: PartnerService, private mediaObjectService: MediaObjectService, private translateService: TranslateService, protected appHelperService: AppHelperService, ) { this.partnerForm = partnerForm; this.selectedImage = null; this.partnerSub = new Subscription(); this.mediaSub = new Subscription(); this.partnerNameOne = ""; } ngOnInit(): void { this.partnerForm = FormGroupInitializer.initFormGroup(this.partnerForm, this.partner); this.translateService.get('basic.' + this.partner.partnerType + 'One').subscribe((translation: string) => { this.partnerNameOne = translation; }); } // On submit form: Check if image is set onSubmit() { if (this.selectedImage !== null) { this.mediaSub = this.mediaObjectService.mediaObjectsPost( this.selectedImage ).subscribe( data => { this.partnerForm.patchValue({"logo": data.id}); this.submitForm(); } ); } else { this.submitForm(); } } // Submit all values of the form to the backend submitForm() { if (this.partnerForm.valid) { if (this.partner.id === null || this.partner.id === undefined) { // Create new contact this.partnerSub = this.partnerService.partnersPost( this.partnerForm.value as PartnerJsonld ).subscribe( data => { this.partnerForm.reset(); this.submit.emit(ModalStatus.Submitted); } ); } else { // Edit contact this.partnerSub = this.partnerService.partnersIdPatch( this.appHelperService.extractId(this.partner.id), this.partnerForm.value as PartnerJsonld ).subscribe( data => { this.partnerForm.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.partnerForm.patchValue({"logo": null}); this.partnerForm.patchValue({"logoUrl": null}); } } }