import { Component, OnInit, OnDestroy } from '@angular/core'; import { DataService } from './../../services/data.service'; import { ActivatedRoute, Params, Router } from '@angular/router'; import { IntervalObservable } from 'rxjs/observable/IntervalObservable'; import { Subscription } from 'rxjs/Subscription'; import { Flight } from './../../classes/flight.class'; import { User } from './../../classes/user.class'; import { Plane } from './../../classes/plane.class'; @Component({ selector: 'app-flight', templateUrl: './flight.component.html', styleUrls: ['./flight.component.css'] }) export class FlightComponent implements OnInit, OnDestroy { loading:boolean = true; error:boolean = false; id:number = 0; flight:Flight = null; name:string = "Loading"; public subscription:Subscription; constructor(private data:DataService, private router: Router, private route: ActivatedRoute){} getFlight(id:number): void { this.data.load().then(()=> { this.data .flight(id) .subscribe(b => { var status = b.status_code; if(status != 200 && b.data.length != 1) { this.loading = false; this.error = true; } else{ this.flight = Flight.toFlight(b.data[0]); this.name = "Flight #" + this.flight.id; this.loading = false; } }); }); } endFlight():void { this.data.load().then(()=> { this.data.endFlight(this.flight.id).subscribe(b => { console.log("flight ended"); this.flight.endtime = Date.now().toString(); }); }); } ngOnInit(): void { this.id = this.route.snapshot.params['id']; this.getFlight(this.id); this.subscription = IntervalObservable.create(5000).subscribe(() => { console.log("Reloading data..."); this.getFlight(this.id); }); } ngOnDestroy(): void { this.subscription.unsubscribe(); } onClick(user: User): void { this.router.navigate(['/user', user.id]); } onClickPlane(plane: Plane): void { this.router.navigate(['/plane', plane.id]); } }