import { Component , OnInit } from '@angular/core' ; import { ActivatedRoute , ParamMap , Router } from '@angular/router' ; import { User , UserService } from 'carey-user' ; import { switchMap } from 'rxjs/operators' ; import { EmailService } from '../service/email.service' ; @ Component ( { selector : 'app-email-redirect' , templateUrl : './email-redirect.component.html' , styleUrls : [ './email-redirect.component.css' ] } ) export class EmailRedirectComponent implements OnInit { constructor ( private emailService , private route , private router , private userService ) { } ngOnInit ( ) { let redirect$ = this . route . queryParamMap . pipe ( switchMap ( ( params ) => this . emailService . fetchToken ( params . get ( 'code' ) ) ) ) ; redirect$ . subscribe ( ( token ) => this . handleResponse ( token ) , err => this . handleError ( err ) ) ; } private handleResponse ( token ) { let user = this . userService . user ; this . emailService . updateUserEmailChoice ( user , 'GMAIL' ) . subscribe ( ( response ) => this . handleUpdateEmailChoiceResponse ( response ) , err => this . handleUpdateEmailChoiceError ( err ) ) ; } private handleUpdateEmailChoiceResponse ( response ) { this . reroute ( ) ; } private handleUpdateEmailChoiceError ( err ) { console . error ( err ) ; } private reroute ( ) { let route = '/user/email/inbox' ; this . router . navigate ( [ route ] ) ; } private handleError ( err ) { console . error ( err ) ; } }