socialnetworknews-web

A open-source Paper.li clone
git clone git://archive.git.mtrnord.blog/SocialNetworkNews/socialnetworknews-web.git
Log | Files | Refs | README | LICENSE

navbar.component.ts (2300B)


      1 import {throwError as observableThrowError, of} from 'rxjs';
      2 import {delay, mergeMap, retryWhen, take, concat} from 'rxjs/operators';
      3 import { Component, OnInit } from '@angular/core';
      4 import {ToastrService} from 'ngx-toastr';
      5 import {ApiService} from '../api.service';
      6 
      7 @Component({
      8   selector: 'app-navbar',
      9   templateUrl: './navbar.component.html',
     10   styleUrls: ['./navbar.component.scss']
     11 })
     12 export class NavbarComponent implements OnInit {
     13   authenticated: boolean;
     14 
     15   constructor(public apiService: ApiService, private toastr: ToastrService) { }
     16 
     17   ngOnInit() {
     18     if (!this.apiService.inCallback) {
     19       this.authCheck();
     20     }
     21   }
     22 
     23   authCheck() {
     24     this.apiService.checkIfUserIsAuthenticated()
     25       .pipe(
     26         // TODO: Better variable naming
     27         retryWhen(oerror => {
     28           return oerror
     29             .pipe(
     30               mergeMap((error: any) => {
     31                 if (String(error.status).startsWith('50')) {
     32                   return of(error.status).pipe(
     33                     delay(1000)
     34                   );
     35                 } else if (error.status === 404) {
     36                   return observableThrowError({error: 'Sorry, there was an error. The Server just doesn\'t find any data for the requested time :('});
     37                 } else if (error.status === 401) {
     38                   return observableThrowError({error: '401'});
     39                 }
     40                 return observableThrowError({error: 'Unknown error'});
     41               }),
     42               take(5),
     43                 // TODO: Allow to link to a Status Page
     44               concat(observableThrowError({error: 'Sorry, there was an error (after 5 retries). This probably means we can\'t reach our API Server :('}))
     45             );
     46         })
     47       )
     48       .subscribe(
     49       data => {
     50         this.authenticated = true;
     51         if (!this.apiService.userUUID) {
     52           this.apiService.userUUID = data.headers.get('UUID');
     53         }
     54       },
     55       err => {
     56         if (err['error'] === '401') {
     57           this.authenticated = false;
     58         } else {
     59           this.toastr.error(err['error'], 'Error connecting API', {
     60             positionClass: 'toast-top-center',
     61             disableTimeOut: true
     62           });
     63           throw err;
     64         }
     65       },
     66       () => console.log('done checking if authenticated')
     67     );
     68   }
     69 
     70 }