This is my first Angular2 component, which makes two api calls: the first call provide the input for the the second one.
The following is my attempt at this code.
@Component({
selector: 'result-panel',
templateUrl: 'panel.component.html',
styleUrls: ['panel.component.css'],
providers:[FindIpZoneDataService,ApiService]
})
export class PanelComponent
{
networkDetail:Array<Network>;
query = "";
datafetchservice:any;
searching:boolean = false;
rowCount:boolean = false;
iperror :boolean = false;
nwerror :boolean = false;
userinputerror:boolean = false;
constructor(private dataservice :FindIpZoneDataService){
this.datafetchservice = dataservice;
}
getNetworkDetail(ip:string) {
this.networkDetail = [];
this.dataservice.getNetworkDetail(ip)
.map((response) => {
let res: Array<any> = [];
let extattrs: Array<any> = [];
res = response.json();
res.forEach((detail) => {
extattrs.push(detail.extattrs);
});
return extattrs;
})
.map((network:Array<any>) => {
console.log(network);
let result: Array<Network> = [];
if (network) {
network.forEach((detail) => {
result.push(new Network(detail['SITE Name'].value,
detail['InfoSec Security Zone'].value,
detail['Network Security Zone'].value,
detail['Data Classification'].value));
});
return result;
}
})
.subscribe(details => {
this.networkDetail = details;
this.rowCount = true;
this.searching = false;
},
err => {
//Valid Network not found
this.handleNwServiceError(err);
});
}
getIPNetwork(){
this.datafetchservice.getNetwork(this.query)
.map(response => response.json())
.subscribe(result => {
this.getNetworkDetail(result[0].network);
},
err => {
//InValid IP
this.handleIpServiceError(err);
});
}
private handleNwServiceError(error: any) {
//Observable.throw(error.json().error || 'Invalid Network - Server error');
console.log(error);
this.nwerror = true;
this.rowCount = false;
}
private handleIpServiceError(error: any) {
//Observable.throw(error.json().error || 'Invalid IP - Server error');
console.log(error);
this.iperror = true;
this.rowCount = false;
console.log('ip error');
}
Please let me know how I can improve it.