diff --git a/src/frontend/app/profile/profile.component.ts b/src/frontend/app/profile/profile.component.ts index eb3a5f80a6d1f34f5bfaf549d614c319ecad80a8..2a5475acf9cb371fd80f8a72d76fad710d7c1dfb 100644 --- a/src/frontend/app/profile/profile.component.ts +++ b/src/frontend/app/profile/profile.component.ts @@ -12,7 +12,7 @@ import { Router } from '@angular/router'; import { tap, exhaustMap, filter, catchError, finalize, switchMap } from 'rxjs/operators'; import { AuthService } from '../service/auth.service'; import { Observable, of } from 'rxjs'; -import { ModalController } from '@ionic/angular'; +import { AlertController, ModalController } from '@ionic/angular'; @Component({ selector: 'app-my-profile', @@ -75,17 +75,19 @@ export class ProfileComponent implements OnInit { signatureScaling: any = 25; originalSize: boolean = false; - constructor(private translate: TranslateService, + constructor( public http: HttpClient, - private router: Router, public sanitizer: DomSanitizer, public notificationService: NotificationService, public signaturesService: SignaturesContentService, public authService: AuthService, - private cookieService: CookieService, public filtersService: FiltersService, + public modalController: ModalController, private renderer: Renderer2, - public modalController: ModalController + private translate: TranslateService, + private alertController: AlertController, + private cookieService: CookieService, + private router: Router, ) { } ngOnInit(): void { @@ -321,22 +323,54 @@ export class ProfileComponent implements OnInit { }); } - deleteSubstitute() { - const r = confirm(this.translate.instant('lang.deleteSubstitution') + ' ?'); - - if (r) { - this.profileInfo.substitute = null; - - this.http.put('../rest/users/' + this.authService.user.id + '/substitute', { substitute: this.profileInfo.substitute }) - .subscribe(() => { - this.authService.updateUserInfoWithTokenRefresh(); - this.filtersService.resfreshDocuments(); - if (this.signaturesService.documentsList.length > 0 && this.signaturesService.documentsList[this.signaturesService.indexDocumentsList].owner === false) { - this.router.navigate(['/documents']); + async deleteSubstitute() { + const alert = await this.alertController.create({ + header: this.translate.instant('lang.confirmMsg'), + buttons: [ + { + text: this.translate.instant('lang.no'), + role: 'cancel', + cssClass: 'secondary', + handler: () => { } + }, + { + text: this.translate.instant('lang.yes'), + handler: () => { + const substitutedSignatures: any[] = this.signaturesService.signaturesList.filter((signature: any) => signature.substituted); + this.profileInfo.substitute = null; + this.http.put('../rest/users/' + this.authService.user.id + '/substitute', { substitute: this.profileInfo.substitute }).pipe( + tap((data: any) => { + this.authService.updateUserInfoWithTokenRefresh(); + this.filtersService.resfreshDocuments(); + if (substitutedSignatures.length > 0) { + substitutedSignatures.forEach((signature: any) => { + this.http.patch('../rest/users/' + this.authService.user.id + '/signatures/' + signature.id + '/substituted', { 'substituted': false }).pipe( + tap(() => { + this.signaturesService.signaturesList.find((item: any) => item.id === signature.id).substituted = false; + }), + catchError((err: any) => { + this.notificationService.handleErrors(err); + return of(false); + }) + ).subscribe(); + }); + } + if (this.signaturesService.documentsList.length > 0 && this.signaturesService.documentsList[this.signaturesService.indexDocumentsList].owner === false) { + this.router.navigate(['/documents']); + } + this.notificationService.success('lang.substitutionDeleted'); + }), + catchError((err: any) => { + this.notificationService.handleErrors(err); + return of(false); + }) + ).subscribe(); } - this.notificationService.success('lang.substitutionDeleted'); - }); - } + } + ] + }); + + await alert.present(); } handleFileInput(files: FileList) {