96 lines
2.3 KiB
TypeScript
96 lines
2.3 KiB
TypeScript
import { Component, OnInit, OnDestroy } from '@angular/core';
|
|
import { DataService } from './../../services/data.service';
|
|
import { Router } from '@angular/router';
|
|
import { IntervalObservable } from 'rxjs/observable/IntervalObservable';
|
|
import { Subscription } from 'rxjs/Subscription';
|
|
|
|
import { Plane } from "./../../classes/plane.class";
|
|
|
|
@Component({
|
|
selector: 'app-planes',
|
|
templateUrl: './planes.component.html',
|
|
styleUrls: ['./planes.component.css']
|
|
})
|
|
|
|
export class PlanesComponent implements OnInit, OnDestroy {
|
|
|
|
loading:boolean = true;
|
|
error:boolean = false;
|
|
|
|
planes:Array<Plane> = new Array<Plane>();
|
|
|
|
public subscription:Subscription;
|
|
|
|
constructor(private router: Router, private data:DataService){}
|
|
|
|
getPlanesData(): void {
|
|
|
|
this.data.load().then(()=> {
|
|
this.data
|
|
.planes()
|
|
.subscribe(response => {
|
|
|
|
var status = response.status_code;
|
|
|
|
if(status != 200)
|
|
{
|
|
this.loading = false;
|
|
this.error = true;
|
|
}
|
|
else{
|
|
|
|
this.error = false;
|
|
|
|
for(let i = 0; i < response.data.length; i++)
|
|
{
|
|
var temp:Plane = Plane.toPlane(response.data[i]);
|
|
var plane:Plane = this.getPlaneByID(temp.id);
|
|
|
|
if(plane)
|
|
{
|
|
if(!temp.equals(plane))
|
|
{
|
|
// push new one, delete old
|
|
var index = this.planes.indexOf(plane);
|
|
this.planes.splice(index, 1);
|
|
this.planes.push(temp);
|
|
}
|
|
} else {
|
|
this.planes.push(temp);
|
|
}
|
|
|
|
}
|
|
|
|
this.planes.sort((a, b) => {
|
|
return a.name > b.name ? 1 : -1;
|
|
});
|
|
|
|
this.loading = false;
|
|
}
|
|
|
|
});
|
|
});
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
this.getPlanesData();
|
|
|
|
this.subscription = IntervalObservable.create(5000).subscribe(() => {
|
|
console.log("Reloading data...");
|
|
this.getPlanesData();
|
|
});
|
|
}
|
|
|
|
ngOnDestroy(): void {
|
|
this.subscription.unsubscribe();
|
|
}
|
|
|
|
onClick(plane: Plane): void {
|
|
this.router.navigate(['/plane', plane.id]);
|
|
}
|
|
|
|
getPlaneByID(id:number): Plane {
|
|
return this.planes.find((f) => {return f.id === id});
|
|
}
|
|
|
|
} |