papers-list.component.ts (1851B)
1 import {throwError as observableThrowError, of} from 'rxjs'; 2 import {Component, OnInit} from '@angular/core'; 3 import {ApiService, Paper} from '../api.service'; 4 import * as Raven from 'raven-js'; 5 import {ToastrService} from 'ngx-toastr'; 6 import {concat, delay, mergeMap, retryWhen, take} from 'rxjs/operators'; 7 8 @Component({ 9 selector: 'app-papers-list', 10 templateUrl: './papers-list.component.html', 11 styleUrls: ['./papers-list.component.scss'] 12 }) 13 export class PapersListComponent implements OnInit { 14 data: (Paper)[]; 15 16 constructor(private apiService: ApiService, private toastr: ToastrService) { 17 Raven.captureBreadcrumb({ 18 message: 'Listing Papers', 19 category: 'papers-list' 20 }); 21 } 22 23 ngOnInit() { 24 this.getPapers(); 25 } 26 27 getPapers() { 28 this.apiService.getPapers() 29 // TODO: Better variable naming 30 .pipe( 31 retryWhen(oerror => { 32 return oerror 33 .pipe( 34 mergeMap((error: any) => { 35 if (String(error.status).startsWith('50')) { 36 return of(error.status) 37 .pipe( 38 delay(1000) 39 ); 40 } 41 return observableThrowError({error: 'Unknown error'}); 42 }), 43 take(5), 44 // TODO: Allow to link to a Status Page 45 concat(observableThrowError({error: 'Sorry, there was an error (after 5 retries). This probably means we can\'t reach our API Server :('})) 46 ); 47 }) 48 ) 49 .subscribe( 50 data => this.data = data, 51 err => { 52 this.toastr.error(err['error'], 'Error connecting API', { 53 positionClass: 'toast-top-center', 54 disableTimeOut: true 55 }); 56 throw err; 57 }, 58 () => console.log('done loading Papers') 59 ); 60 } 61 62 }