Project

General

Profile

« Previous | Next » 

Revision 60197

[Usage-Counts | Trunk]: Fix search bug on alalytics. Change loading

View differences:

analytics.component.ts
1
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
1
import {Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core';
2 2
import {CountryUsageStat, UsageStat} from '../entities/usage-stat';
3 3
import {FormControl, FormGroup} from '@angular/forms';
4
import {Observable, Subscription} from 'rxjs';
4
import {Observable, Subscriber, Subscription} from 'rxjs';
5 5
import {UsageStatsService} from '../services/usage-stats.service';
6 6
import {map, startWith} from 'rxjs/operators';
7 7
import {countries} from '../services/countries';
......
19 19
  templateUrl: 'analytics.component.html',
20 20
  styleUrls: ['analytics.component.css'],
21 21
})
22
export class AnalyticsComponent implements OnInit {
22
export class AnalyticsComponent implements OnInit, OnDestroy {
23 23
  public countryFb: FormGroup;
24 24
  public countries: Observable<string[]>;
25 25
  public country: string;
......
36 36
  description = "Track Countries Usage Activity. Worldwide Monthly Usage Events, Monthly Views & Downloads.";
37 37
  title = "OpenAIRE - UsageCounts | Analytics";
38 38
  subs: Subscription[] = [];
39
  searchSub: Subscription = null;
40
  
39 41
  constructor(private usageStatsService: UsageStatsService,
40 42
              private route: ActivatedRoute,
41 43
              private router: Router,
......
72 74
    }
73 75
  }
74 76
  
77
  public ngOnDestroy() {
78
    this.subs.forEach(subscription => {
79
      if (subscription instanceof Subscriber) {
80
        subscription.unsubscribe();
81
      }
82
    });
83
    this.clearTimeouts();
84
  }
85
  
75 86
  private init() {
76 87
    this.countryFb = new FormGroup({
77 88
      country: new FormControl(null)
......
119 130
    this.clearTimeouts();
120 131
    if (this.country && this.country.length > 0) {
121 132
      this.country = StringUtils.capitalize(this.country);
122
      this.usageStatsService.getCountryMetrics(this.country).subscribe(stats => {
133
      this.searchSub = this.usageStatsService.getCountryMetrics(this.country).subscribe(stats => {
123 134
        this.display = this.roundCountryUsageStat(stats);
124 135
        this.loading = false;
125 136
        if (this.display) {
......
177 188
      clearTimeout(timeout);
178 189
    });
179 190
    this.state = 0;
191
    if(this.searchSub instanceof Subscription) {
192
      this.searchSub.unsubscribe();
193
    }
180 194
  }
181 195
  
182 196
  reset() {
......
203 217
  public getSafeUrl(url: string): SafeUrl {
204 218
    return this.sanitizer.bypassSecurityTrustResourceUrl(url);
205 219
  }
206
  public ngOnDestroy() {
207
    for (let sub of this.subs) {
208
      sub.unsubscribe();
209
    }
210
  }
211 220
}

Also available in: Unified diff