import {Component, OnDestroy, OnInit} from '@angular/core'; import {FieldType} from '@ngx-formly/material'; import {AllService} from '../../../all.service'; import {ActivatedRoute} from '@angular/router'; @Component({ selector: 'jhi-step-signature', templateUrl: './step-signature.component.html', styleUrls: ['./step-signature.component.scss'] }) export class StepSignatureComponent extends FieldType implements OnInit, OnDestroy { urlSignature = null; showIframe = true; status = ''; loadIframe = true; error = false; errorMessage = 'Une erreur est survenue, merci de contacter le support technique.'; signatureAsObserVable: any = null; showRefreshButton = false; constructor(public allService: AllService, private route: ActivatedRoute) { super(); } ngOnInit(): void { this.startProcess(); this.allService.showButtonPrevious = true; this.allService.showButtonNext = false; } ngOnDestroy(): void { super.ngOnDestroy(); this.signatureAsObserVable.unsubscribe(); } startProcess() { const contractid = this.route.snapshot.paramMap.get('contractid'); this.signatureAsObserVable = this.allService.getSignatureAsObserVable().subscribe((info) => { if (info !== 'signer') { this.error = true; this.showRefreshButton = false; this.showIframe = false; this.loadIframe = false; this.errorMessage = 'Une erreur est survenue, merci de contacter le support technique.'; this.signatureAsObserVable.unsubscribe(); return; } this.error = false; this.showRefreshButton = false; this.showIframe = true; this.loadIframe = true; const data = {}; this.clearZipCode(); for (const property of Object.keys(this.model)) { data[property] = Array.isArray(this.model[property]) ? null : this.model[property]; } this.allService.contractDocumentService.generateUrlSignature(contractid, data).toPromise().then( (response) => { this.error = false; this.showRefreshButton = false; this.showIframe = true; this.loadIframe = true; this.urlSignature = response.body.iFrame.split('=').pop(); window.setTimeout(this.initIframe.bind(this), 2000); this.signatureAsObserVable.unsubscribe(); }).catch((error) => { this.error = true; this.showRefreshButton = false; this.loadIframe = false; this.showIframe = false; if (error.status === 425) { this.errorMessage = 'L\'un des documents n\'est pas encore disponible, veuillez attendre quelques instants avant de rafraîchir la page.'; } else { this.errorMessage = 'Une erreur est survenue durant la génération de la signature, merci de contacter le support technique.'; } // Veuillez réessayer , dans cas ou ça merde , ben insistez pas !!! console.log('error signature', error); this.signatureAsObserVable.unsubscribe(); }); }); } refresh(): void { this.showRefreshButton = false; this.allService.saveDataStep.emit({model: this.model, productStepId: this.allService.activeStep.productStepId}); } initIframe() { const configuration = {redirectionMode: 'IN'}; const signerid = this.urlSignature; this.loadIframe = false; window.addEventListener('pdsEvent', (e) => { console.log('pdsEvent', event); this.status = event['detail'].status; this.allService.deactivateButtonPrevious = this.status === 'signed'; if (this.status === 'canceled' || this.status === 'failed' || this.status === 'signed') { this.showIframe = false; } if (this.status === 'canceled' || this.status === 'failed') { this.allService.deactivateButtonPrevious = false; window.location.reload(); } // e.detail.eventType return 'begin' or 'end' of signature process // e.detail.signerId get the Signer Id }); if (this.allService.configurationPreview.universignProd) { window['universignSigInit']('iframeContainerId', signerid, configuration); } else { window['universignSigInit']('iframeContainerId', signerid, configuration, 'https://sign.test.universign.eu'); } window.document.getElementById('iframeContainerId').getElementsByTagName('iframe')[0].addEventListener('load', () => { if (this.status === 'signed') { setTimeout(() => { this.allService.deactivateButtonPrevious = false; this.allService.currentStepCustomError = false; this.allService.stepsSubject.next('next'); }, 2000); } }); } clearZipCode(): void { if (this.model['insured_identity_current_zipcode_city']) { this.model['insured_identity_current_zipcode'] = null; this.model['insured_identity_current_city'] = null; } } }