import { Injectable , OnDestroy } from '@angular/core' ; import { BehaviorSubject , Observable , of , Subscription } from 'rxjs' ; import { catchError , take } from 'rxjs/operators' ; import { ApiService } from '../../../../../common/api/api.service' ; import { FormToastService } from '../../../../../common/services/form-toast.service' ; import { OnboardingV3PanelService } from '../../onboarding-panel.service' ; export type PhoneVerificationStep = 'InputNumberStep' | 'ConfirmCodeStep' ; /**
 * Phone verification service for onboarding v3.
 */ @ Injectable ( { providedIn : 'root' } ) export class OnboardingV3VerifyPhoneService implements OnDestroy { private subscriptions : Subscription [ ] = [ ] ; /**
     * BehaviourSubject holding the current PhoneVerificationStep.
     */ public readonly verificationStep$ : BehaviorSubject < PhoneVerificationStep > = new BehaviorSubject < PhoneVerificationStep > ( 'InputNumberStep' ) ; /**
     * BehaviourSubject holding whether loading is in progress
     */ public readonly inProgress$ : BehaviorSubject < boolean > = new BehaviorSubject < boolean > ( false ) ; /**
     * BehaviourSubject holding the secret value passed to the front-end
     * by the api/v2/blockchain/rewards/verify call.
     */ private readonly secret$ : BehaviorSubject < string > = new BehaviorSubject < string > ( '' ) ; constructor ( private panel , private toast , private api ) { } ngOnDestroy ( ) { for ( let subscription of this . subscriptions ) { subscription . unsubscribe ( ) ; } } param { number } phoneNumber returns { void } public sendCode ( phoneNumber ) { this . inProgress$ . next ( true ) ; this . subscriptions . push ( this . api . post ( 'api/v2/blockchain/rewards/verify' , { number : phoneNumber } ) . pipe ( take ( 1 ) , catchError ( e => this . handleError ( e ) ) ) . subscribe ( ( response ) => { if ( ! response ) { return ; } this . secret$ . next ( response . secret ) ; this . verificationStep$ . next ( 'ConfirmCodeStep' ) ; this . inProgress$ . next ( false ) ; } ) ) ; } param { number } param { string } returns { void } public verifyCode ( phoneNumber , code ) { this . inProgress$ . next ( true ) ; this . subscriptions . push ( this . api . post ( 'api/v2/blockchain/rewards/confirm' , { number : phoneNumber , code : code , secret : this . secret$ . getValue ( ) , } ) . pipe ( take ( 1 ) , catchError ( e => this . handleError ( e ) ) ) . subscribe ( ( response ) => { if ( ! response ) { return ; } this . inProgress$ . next ( false ) ; this . toast . success ( 'Verification successful' ) ; this . panel . forceComplete$ . next ( 'VerifyUniquenessStep' ) ; this . panel . dismiss$ . next ( true ) ; } ) ) ; } param { message ? } e returns private handleError ( e ) { this . inProgress$ . next ( false ) ; this . toast . error ( e . message ? e . message : 'An unknown error has occurred' ) ; return of ( null ) ; } }