diff --git a/angular/src/app/directives/download-app.directive.ts b/angular/src/app/directives/download-app.directive.ts index 40eaa18..502bf27 100644 --- a/angular/src/app/directives/download-app.directive.ts +++ b/angular/src/app/directives/download-app.directive.ts @@ -4,9 +4,15 @@ import { HostListener, OnInit, Renderer2, + Inject, } from '@angular/core'; +import { lastValueFrom } from 'rxjs'; import { MatSnackBar } from '@angular/material/snack-bar'; import { MessageService } from 'primeng/api'; +import { CookiesService } from 'src/app/services/cookies.service'; +import { RpcService, JsonrpcService } from 'src/app/services/jsonrpc.service'; +import { DOCUMENT } from '@angular/common'; +import { AppleWalletService } from 'src/app/services/apple-wallet.service'; @Directive({ selector: '[appDownloadApp]', @@ -20,6 +26,10 @@ export class DownloadAppDirective implements OnInit { public renderer: Renderer2, private el: ElementRef, private _snackBar: MatSnackBar, + private appleWallet: AppleWalletService, + private cookiesService: CookiesService, + @Inject(DOCUMENT) private document: Document, + private jsonrpc: JsonrpcService, ) {} ngOnInit(): void { @@ -54,15 +64,13 @@ export class DownloadAppDirective implements OnInit { } @HostListener('click', ['$event']) - downloadApp(event: MouseEvent) { + async downloadApp(event: MouseEvent) { if (event) { event.preventDefault(); } if (this.deviceType === 'ios') { - this._snackBar.open(`Для установки нажмите на кнопку поделиться в Вашем браузере и выберите пункт 'На экран «Домой»'`, '', { - duration: 5000, - }); - return; + await this.addCardToWallet(); + return; } if (!this.deferredPrompt) { this.messageService.add({ @@ -83,5 +91,31 @@ export class DownloadAppDirective implements OnInit { } this.deferredPrompt = null; }); + } + + async addCardToWallet() { + const token = this.cookiesService.getItem('token'); + const accountData = ( + await lastValueFrom( + this.jsonrpc.rpc( + { + method: 'getTokenData', + params: [], + }, + RpcService.authService, + true + ) + ) + ).data; + if (token && accountData.user_id) { + this.appleWallet.generateCard(token, accountData.user_id).subscribe({ + next: (res: any) => { + this.document.location.href = res.url; + }, + error: (err) => { + console.log('Error: ', err); + }, + }); + } } } diff --git a/angular/src/app/services/apple-wallet.service.ts b/angular/src/app/services/apple-wallet.service.ts index e184003..0c4e5c5 100644 --- a/angular/src/app/services/apple-wallet.service.ts +++ b/angular/src/app/services/apple-wallet.service.ts @@ -6,7 +6,7 @@ import { environment } from 'src/environments/environment'; providedIn: 'root', }) export class AppleWalletService { - private url: string = environment.appleWalletEndpoint; + private url: string = environment.appleWalletEndpoint; constructor( private http: HttpClient, ) {}