Commit 8e0f829a authored by dev0tion's avatar dev0tion

Add global service to login component, improve code

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