import { Component, OnInit, OnDestroy, Injectable, ChangeDetectorRef } from '@angular/core'; import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot, ActivatedRoute } from '@angular/router'; import { MatDialog, MatDialogConfig} from '@angular/material/dialog'; import { FormBuilder, Validators, FormGroup, FormControl } from '@angular/forms'; import { UserService } from '../user.service'; import { FullnodeService } from '../fullnode.service'; import { ScanComponent} from '../scan/scan.component'; import { Tx } from '../tx.model'; import { Subscription, Observable } from 'rxjs'; import { v4 as uuidv4 } from 'uuid'; var QRCode = require('easyqrcodejs'); var URLSafeBase64 = require('urlsafe-base64'); var Buffer = require('buffer/').Buffer; @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.css'] }) export class LoginComponent implements OnInit { txs: Tx[] = []; intervalHolder: any; nodeAddress: string = ''; localToken: string | null = ''; selectedValue: number = 0.001; private FullnodeSub: Subscription = new Subscription(); private UserSub: Subscription = new Subscription(); public heightUpdate: Observable; public uZaddrUpdate: Observable; public txsUpdate: Observable; tickets = [ { value: 0.001, viewValue: 'One hour' },{ value: 0.005, viewValue: 'One day' } ]; entryForm: FormGroup; constructor( private fb: FormBuilder, private activatedRoute: ActivatedRoute, public fullnodeService: FullnodeService, private router: Router, public userService: UserService, private dialog: MatDialog, private _changeDetectorRef: ChangeDetectorRef ){ //this.fullnodeService.getAddr(); this.entryForm = fb.group({ selectedSession: [0.001, Validators.required] }); this.heightUpdate = fullnodeService.heightUpdate; this.uZaddrUpdate = userService.uZaddrUpdate; this.txsUpdate = userService.txUpdate; this.txsUpdate.subscribe((txs) => { this.txs = txs; }); } ngOnInit(){ //console.log('Activated route data in Component:::', this.activatedRoute.data); this.activatedRoute.data.subscribe((addrData) => { //console.log('FETCH ADDRESS', addrData); this.nodeAddress = addrData.response.addr; //console.log('Node addres ', this.nodeAddress); this.localToken = localStorage.getItem('s4z_token'); //console.log(localToken); if(this.localToken == null){ var token = uuidv4(); localStorage.setItem('s4z_token', token); this.localToken = token; } this.userService.findUser(); this.userService.uZaddrUpdate. subscribe((userAddr: string) => { if (userAddr.length != 0) { console.log('Log in found!'); this.router.navigate(['/shop']); } else { console.log('No login for existing token found'); //console.log('Showing QR code for login'); ////console.log(URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(localToken)))); //var codeString = `zcash:${this.nodeAddress}?amount=0.005&memo=${URLSafeBase64.encode(Buffer.from('ZGO::'.concat(this.localToken!)))}`; //console.log(codeString); //var qrcode = new QRCode(document.getElementById("qrcode"), { //text: codeString, //logo: "/assets/zcash.png", //logoWidth: 80, //logoHeight: 80 //}); } }); }); this.intervalHolder = setInterval(() => { this.fullnodeService.getHeight(); //this.userService.findUser(); this.loginCheck(); this._changeDetectorRef.markForCheck(); }, 1000 * 75); } loginCheck(){ this.userService.findUser(); this.userService.findPending(); this.txsUpdate.subscribe((txs) => { this.txs = txs; }); this.uZaddrUpdate.subscribe((userAddr: string) => { if (userAddr.length != 0) { console.log('Log in found in blockchain!'); this.router.navigate(['/shop']); } }); } login() { console.log('Dropdown:', this.entryForm.value.selectedSession); const dialogConfig = new MatDialogConfig(); dialogConfig.disableClose = true; dialogConfig.autoFocus = true; dialogConfig.data = { totalZec: this.entryForm.value.selectedSession, addr: this.nodeAddress, session: this.localToken }; const dialogRef = this.dialog.open(ScanComponent, dialogConfig); dialogRef.afterClosed().subscribe((val) => { console.log('Awaiting confirmation'); }); } ngOnDestroy(){ this.FullnodeSub.unsubscribe(); this.UserSub.unsubscribe(); clearInterval(this.intervalHolder); } }