Commit 8e0f829a authored by dev0tion's avatar dev0tion

Add global service to login component, improve code

parent 9295c144
...@@ -24,4 +24,4 @@ ...@@ -24,4 +24,4 @@
<p> Looks like you're new here. Please create or restore a wallet.</p> <p> Looks like you're new here. Please create or restore a wallet.</p>
</ng-template> </ng-template>
<button type="button" (click)="clickedCreate()" class="btn btn-success">Create or restore wallet</button> <button type="button" (click)="onCreateClicked()" class="btn btn-success">Create or restore wallet</button>
\ No newline at end of file
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { GlobalService } from '../shared/services/global.service';
import { ApiService } from '../shared/services/api.service'; import { ApiService } from '../shared/services/api.service';
import { WalletLoad } from '../shared/classes/wallet-load'; import { WalletLoad } from '../shared/classes/wallet-load';
...@@ -9,81 +10,70 @@ import { WalletLoad } from '../shared/classes/wallet-load'; ...@@ -9,81 +10,70 @@ import { WalletLoad } from '../shared/classes/wallet-load';
styleUrls: ['./login.component.css'] styleUrls: ['./login.component.css']
}) })
export class LoginComponent implements OnInit { export class LoginComponent implements OnInit {
constructor(private apiService: ApiService, private router: Router) { } constructor(private globalService: GlobalService, private apiService: ApiService, private router: Router) { }
private walletLoad: WalletLoad;
private hasWallet: boolean = false; private hasWallet: boolean = false;
private currentWalletName: string; private wallets: [string];
private wallets: [any];
private walletPath: string;
private password: string; private password: string;
private responseMessage: any;
private errorMessage: any;
ngOnInit() { ngOnInit() {
this.apiService.getWalletFiles() this.apiService.getWalletFiles()
.subscribe( .subscribe(
response => { response => {
if (response.status >= 200 && response.status < 400) { if (response.status >= 200 && response.status < 400) {
this.responseMessage=response; let responseMessage = response.json();
this.wallets = response.json().walletsFiles; this.wallets = responseMessage.walletsFiles;
this.walletPath = response.json().walletsPath; this.globalService.setWalletPath(responseMessage.walletsPath);
if (this.wallets.length > 0) { if (this.wallets.length > 0) {
this.hasWallet = true; this.hasWallet = true;
this.currentWalletName = this.wallets[0].slice(0, -5); for (let wallet in this.wallets) {
this.wallets[wallet] = this.wallets[wallet].slice(0, -5);
}
this.globalService.setCurrentWalletName(this.wallets[0]);
} else { } else {
this.hasWallet = false; this.hasWallet = false;
} }
} }
}, },
error => { error => {
this.errorMessage = <any>error; let errorMessage = <any>error;
if (error.status >= 400) { if (error.status >= 400) {
alert(this.errorMessage); alert(errorMessage);
console.log(this.errorMessage); console.log(errorMessage);
} }
} }
); );
} }
private onSubmit() { private onSubmit() {
let walletLoad = new WalletLoad(this.password, this.globalService.getWalletPath(), this.globalService.getCurrentWalletName());
this.walletLoad = new WalletLoad(); this.apiService.loadWallet(walletLoad)
this.walletLoad.password = this.password;
this.walletLoad.name = this.currentWalletName;
this.walletLoad.folderPath = this.walletPath;
console.log(this.walletLoad);
this.apiService.loadWallet(this.walletLoad)
.subscribe( .subscribe(
response => { response => {
console.log(response); console.log(response);
if (response.status >= 200 && response.status < 400) { if (response.status >= 200 && response.status < 400) {
this.responseMessage = response; let responseMessage = response.json();
this.router.navigate(['/wallet']); this.router.navigate(['/wallet']);
} }
}, },
error => { error => {
this.errorMessage = <any>error; let errorMessage = <any>error;
if (error.status === 403 && error.json().errors[0].message === "Wrong password, please try again.") { if (error.status === 403 && error.json().errors[0].message === "Wrong password, please try again.") {
alert("Wrong password, try again."); alert("Wrong password, try again.");
} else if (error.status >= 400) { } else if (error.status >= 400) {
alert(this.errorMessage); alert(errorMessage);
console.log(this.errorMessage); console.log(errorMessage);
} }
} }
); );
} }
private walletChanged(walletName: string) { private walletChanged(walletName: string) {
let walletNameNoJson: string = walletName.slice(0, -5); this.globalService.setCurrentWalletName(walletName);
this.currentWalletName = walletNameNoJson;
} }
private clickedCreate() { private onCreateClicked() {
this.router.navigate(['/setup']); this.router.navigate(['/setup']);
} }
} }
export class WalletLoad { export class WalletLoad {
password: string;
folderPath: string; constructor(password: string, folderPath: string, name: string) {
name: string; this.password = password;
this.folderPath = folderPath;
this.name = name;
}
private password: string;
private folderPath: string;
private name: string;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment