import { Component, OnInit, OnDestroy } from '@angular/core'; import { DataService } from './../../services/data.service'; import { IntervalObservable } from 'rxjs/observable/IntervalObservable'; import { Subscription } from 'rxjs/Subscription'; import { User } from "./../../classes/user.class"; import { Params, ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-user', templateUrl: './user.component.html', styleUrls: ['./user.component.css'] }) export class UserComponent implements OnInit, OnDestroy { loading:boolean = true; error:boolean = false; name:string = "Loading"; id:number = 0; public user:User = new User(0, "Loading", "Loading", "Loading"); public subscription:Subscription; constructor(private route: ActivatedRoute, private data:DataService){} getUser(id:number): void { this.data.load().then(()=> { this.data .user(id) .subscribe(b => { var status = b.status_code; if(status != 200 && b.data.length != 1) { this.loading = false; this.error = true; } else{ this.user = User.toUser(b.data[0]); this.name = this.user.name; this.loading = false; } }); }); } ngOnInit(): void { this.id = this.route.snapshot.params['id']; this.getUser(this.id); this.subscription = IntervalObservable.create(5000).subscribe(() => { console.log("Reloading data..."); this.getUser(this.id); }); } ngOnDestroy(): void { this.subscription.unsubscribe(); } }