Commit 6b54ec2f authored by Pieterjan Vanhoof's avatar Pieterjan Vanhoof Committed by GitHub

Navigate with HTTP status, edit wallet load, unit tests (#26)

- Handling HTTP status codes (temporary catching all statuses)
- Added default unit tests for all components
- Edited wallet load function
- Moved API error handling to components
parents d727e6f6 685da94a
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { remote } from 'electron';
import { ApiService } from './shared/api/api.service';
@Component({
......@@ -13,9 +11,8 @@ import { ApiService } from './shared/api/api.service';
export class AppComponent implements OnInit {
constructor(private router: Router, private apiService: ApiService) {}
private errorMessage: string;
private response: any;
private isConfigured: boolean = true;
private errorMessage: any;
private responseMessage: any;
ngOnInit() {
this.checkWalletStatus();
......@@ -24,22 +21,19 @@ export class AppComponent implements OnInit {
private checkWalletStatus(){
this.apiService.getWalletStatus()
.subscribe(
response => this.response = response,
error => this.errorMessage = <any>error,
() => this.navigate()
response => {
if (response.status >= 200 && response.status < 400) {
this.responseMessage = response;
this.router.navigate(['/login']);
}
},
error => {
this.errorMessage = <any>error;
if (error.status === 400 || error.status === 404) {
this.router.navigate(['/setup']);
console.log(this.errorMessage);
}
}
);
}
private navigate() {
if (this.response.success === "true") {
// remote.dialog.showMessageBox({message: remote.app.getPath('userData')})
this.router.navigate(['/login'])
} else {
this.router.navigate(['/setup'])
}
}
private hasWallet() {
return true;
}
}
\ No newline at end of file
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { ApiService } from '../shared/api/api.service';
import { WalletLoad } from '../shared/wallet-load';
@Component({
selector: 'app-login',
......@@ -10,27 +11,34 @@ import { ApiService } from '../shared/api/api.service';
export class LoginComponent implements OnInit {
constructor(private apiService: ApiService, private router: Router) { }
private response: any;
private errorMessage: string;
private responseMessage: any;
private errorMessage: any;
private walletLoad: WalletLoad;
ngOnInit() {
}
private onSubmit() {
this.apiService.loadWallet("123")
this.walletLoad = new WalletLoad();
this.walletLoad.password = "123";
this.walletLoad.name = "test"
this.walletLoad.folderPath = "folderPath"
this.apiService.loadWallet(this.walletLoad)
.subscribe(
response => this.response = response,
error => this.errorMessage = error,
() => this.loadWallet()
response => {
if (response.status >= 200 && response.status < 400) {
this.responseMessage = response;
this.router.navigate['/wallet']
}
},
error => {
this.errorMessage = <any>error;
if (error.status >= 400) {
alert(this.errorMessage);
console.log(this.errorMessage);
}
}
);
}
private loadWallet() {
if (this.response.success === "true") {
this.router.navigate(['/wallet/send']);
} else {
alert("Something went wrong.")
}
}
}
}
\ No newline at end of file
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CreateComponent } from './create.component';
describe('CreateComponent', () => {
let component: CreateComponent;
let fixture: ComponentFixture<CreateComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CreateComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CreateComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
......@@ -15,7 +15,9 @@ export class CreateComponent {
constructor(private apiService: ApiService) {}
private newWallet: WalletCreation;
private responseMessage: string;
private errorMessage: string;
private createWallet(password: string, network: string, folderPath: string, name: string, ) {
this.newWallet = new WalletCreation();
......@@ -26,6 +28,18 @@ export class CreateComponent {
this.apiService
.createWallet(this.newWallet)
.subscribe((response: string) => this.responseMessage = response);
.subscribe(
response => {
if (response.status >= 200 && response.status < 400){
this.responseMessage = response;
}
},
error => {
if (error.status >= 400) {
this.errorMessage = error;
console.log(this.errorMessage);
}
}
);
}
}
......@@ -10,8 +10,11 @@ import { WalletRecovery } from '../../shared/wallet-recovery'
export class RecoverComponent implements OnInit {
constructor(private apiService: ApiService) { }
private walletRecovery: WalletRecovery;
private responseBody: string;
private responseMessage: string;
private errorMessage: string;
ngOnInit() {
}
......@@ -26,7 +29,18 @@ export class RecoverComponent implements OnInit {
this.apiService
.recoverWallet(this.walletRecovery)
.subscribe((response: string) => this.responseBody = response,
() => console.log("recoverWallet() completed"));
.subscribe(
response => {
if (response.status >= 200 && response.status < 400) {
this.responseMessage = response;
}
},
error => {
if (error.status >= 400) {
this.errorMessage = error;
console.log(this.errorMessage);
}
}
);
}
}
\ No newline at end of file
......@@ -6,6 +6,7 @@ import 'rxjs/add/operator/catch';
import { WalletCreation } from '../wallet-creation';
import { WalletRecovery } from '../wallet-recovery';
import { WalletLoad } from '../wallet-load';
import { Mnemonic } from '../mnemonic';
/**
......@@ -26,7 +27,7 @@ export class ApiService {
createWallet(data: WalletCreation): Observable<any> {
return this.http
.post(this.webApiUrl + '/wallet/create/', JSON.stringify(data), {headers: this.headers})
.map(response => response.json());
.map((response: Response) => response);
}
/**
......@@ -35,17 +36,16 @@ export class ApiService {
recoverWallet(data: WalletRecovery): Observable<any> {
return this.http
.post(this.webApiUrl + '/wallet/recover/', JSON.stringify(data), {headers: this.headers})
.map(response => response.json());
.map((response: Response) => response);
}
/**
* Load a wallet
*/
loadWallet(password: string): Observable<any> {
loadWallet(data: WalletLoad): Observable<any> {
return this.http
.get(this.webApiUrl + '/wallet/load/', {headers: this.headers, body: JSON.stringify(password)})
.map(response => response.json())
.catch(this.handleError);
.get(this.webApiUrl + '/wallet/load/', {headers: this.headers, body: JSON.stringify(data)})
.map((response: Response) => response);
}
/**
......@@ -54,8 +54,7 @@ export class ApiService {
getWalletStatus(): Observable<any> {
return this.http
.get(this.mockApiUrl + '/wallet/status')
.map((response:Response) => response.json())
.catch(this.handleError);
.map((response: Response) => response);
}
/**
......@@ -64,8 +63,7 @@ export class ApiService {
getWalletBalance(): Observable<any> {
return this.http
.get(this.webApiUrl + '/wallet/balance')
.map((response:Response) => response.json())
.catch(this.handleError);
.map((response: Response) => response);
}
/**
......@@ -74,8 +72,7 @@ export class ApiService {
getWalletHistory(): Observable<any> {
return this.http
.get(this.webApiUrl + '/wallet/history')
.map((response:Response) => response.json())
.catch(this.handleError);
.map((response: Response) => response);
}
/**
......@@ -84,24 +81,8 @@ export class ApiService {
getUnusedReceiveAddresses(): Observable<any> {
return this.http
.get(this.webApiUrl + '/wallet/receive')
.map((response:Response) => response.json())
.catch(this.handleError);
.map((response: Response) => response);
}
/**
* Handle errors from the API.
* @param error
*/
private handleError (error: Response | any) {
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}
}
export class WalletLoad {
password: string;
folderPath: string;
name: string;
}
\ No newline at end of file
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { HistoryComponent } from './history.component';
describe('HistoryComponent', () => {
let component: HistoryComponent;
let fixture: ComponentFixture<HistoryComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ HistoryComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(HistoryComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
......@@ -21,8 +21,17 @@ export class HistoryComponent {
private getWalletHistory() {
this.apiService.getWalletHistory()
.subscribe(
response => this.transactions = response.history,
error => this.errorMessage = <any>error
response => {
if (response.status >= 200 && response.status < 400) {
this.transactions = response.history;
}
},
error => {
if (error.status >= 400) {
this.errorMessage = <any>error;
console.log(this.errorMessage);
}
}
);
}
}
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DashboardComponent } from './dashboard.component';
describe('DashboardComponent', () => {
let component: DashboardComponent;
let fixture: ComponentFixture<DashboardComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ DashboardComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(DashboardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
......@@ -21,14 +21,19 @@ export class DashboardComponent {
private getWalletBalance() {
this.apiService.getWalletBalance()
.subscribe(
response => this.balanceResponse = response,
error => this.errorMessage = <any>error,
() => this.setBalance()
response => {
if (response.status >= 200 && response.status < 400) {
this.balanceResponse = response
this.confirmedBalance = this.balanceResponse.confirmed;
this.unconfirmedBalance = this.balanceResponse.unconfirmed;
}
},
error => {
if (error.status >= 400) {
this.errorMessage = <any>error;
console.log(this.errorMessage);
}
}
);
}
private setBalance() {
this.confirmedBalance = this.balanceResponse.confirmed;
this.unconfirmedBalance = this.balanceResponse.unconfirmed;
}
}
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { MenuComponent } from './menu.component';
describe('MenuComponent', () => {
let component: MenuComponent;
let fixture: ComponentFixture<MenuComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MenuComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MenuComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ReceiveComponent } from './receive.component';
describe('ReceiveComponent', () => {
let component: ReceiveComponent;
let fixture: ComponentFixture<ReceiveComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ReceiveComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ReceiveComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
......@@ -21,8 +21,17 @@ export class ReceiveComponent {
private getUnusedReceiveAddresses() {
this.apiService.getUnusedReceiveAddresses()
.subscribe(
response => this.addresses = response.addresses,
error => this.errorMessage = <any>error
response => {
if (response.status >= 200 && response.status < 400) {
this.addresses = response.addresses;
}
},
error => {
if (error.status >= 400) {
this.errorMessage = <any>error;
console.log(this.errorMessage);
}
}
);
}
}
\ No newline at end of file
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { SendComponent } from './send.component';
describe('SendComponent', () => {
let component: SendComponent;
let fixture: ComponentFixture<SendComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ SendComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SendComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { WalletComponent } from './wallet.component';
describe('WalletComponent', () => {
let component: WalletComponent;
let fixture: ComponentFixture<WalletComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ WalletComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(WalletComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
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