File

src/app/stores/app-state.store.ts

Index

Properties
Methods
Accessors

Constructor

constructor(sessionStore: SessionStore, limitsService: LimitsService)
Parameters :
Name Type Optional
sessionStore SessionStore no
limitsService LimitsService no

Methods

clear
clear()
Returns : void
getCombinedInfo
getCombinedInfo()
Returns : CombinedRequest
getContactInfo
getContactInfo()
Returns : string
getDefaultAccount
getDefaultAccount()
onChange
onChange(property: string)
Parameters :
Name Type Optional
property string no
Returns : void
setContactInfo
setContactInfo(input: string)
Parameters :
Name Type Optional
input string no
Returns : void
setSecretWord
setSecretWord(secret: string)
Parameters :
Name Type Optional
secret string no
Returns : void
trim
trim(input: string)
Parameters :
Name Type Optional
input string no
Returns : any

Properties

Private _session
_session: Session
Type : Session
Private _state
_state:
Default value : new AppStateModel()
Private _stateSrc
_stateSrc:
Default value : new BehaviorSubject<AppStateModel>(this._state)
Private amountSubject
amountSubject: Subject<number>
Type : Subject<number>
Default value : new Subject()
Private request
request: Subscription
Type : Subscription
state$
state$:
Default value : this._stateSrc.asObservable()

Accessors

isValid
getisValid()
loadingIsLimits
getloadingIsLimits()
setloadingIsLimits(result: boolean)
Parameters :
Name Type Optional
result boolean no
Returns : void
limitsResult
getlimitsResult()
setlimitsResult(result: )
Parameters :
Name Optional
result no
Returns : void
accounts
getaccounts()
setaccounts(value: [])
Parameters :
Name Type Optional
value [] no
Returns : void
useMfaModal
getuseMfaModal()
setuseMfaModal(value: boolean)
Parameters :
Name Type Optional
value boolean no
Returns : void
hasPassedMfa
gethasPassedMfa()
sethasPassedMfa(value: boolean)
Parameters :
Name Type Optional
value boolean no
Returns : void
hideMaskedAccount
gethideMaskedAccount()
sethideMaskedAccount(value: boolean)
Parameters :
Name Type Optional
value boolean no
Returns : void
isNew
getisNew()
setisNew(value: boolean)
Parameters :
Name Type Optional
value boolean no
Returns : void
passMfa
getpassMfa()
passSecret
getpassSecret()
secretWasSet
getsecretWasSet()
template
gettemplate()
settemplate(template: )
Parameters :
Name Optional
template no
Returns : void
payment
getpayment()
setpayment(payment: )
Parameters :
Name Optional
payment no
Returns : void
bankAccount
getbankAccount()
setbankAccount(bankAccount: )
Parameters :
Name Optional
bankAccount no
Returns : void
multiFactorModel
getmultiFactorModel()
setmultiFactorModel(multiFactorModel: )
Parameters :
Name Optional
multiFactorModel no
Returns : void
acknowledgeDuplicate
getacknowledgeDuplicate()
setacknowledgeDuplicate(value: boolean)
Parameters :
Name Type Optional
value boolean no
Returns : void
duplicateInfo
getduplicateInfo()
setduplicateInfo(value: )
Parameters :
Name Optional
value no
Returns : void
selectedMethod
getselectedMethod()
setselectedMethod(selectedMethod: )
Parameters :
Name Optional
selectedMethod no
Returns : void
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import { debounceTime, filter, first, map } from 'rxjs/operators';
import { Subject } from 'rxjs/Subject';
import { Subscription } from 'rxjs/Subscription';
import {
  AppStateModel,
  BankAccount,
  CombinedRequest,
  MultiFactorModel,
  Payment,
  SelectedMultiFactorMethod,
  Session,
  Template,
} from '../models';
import { DuplicateInfo } from '../models/duplicate-info';
import { LimitsResult } from '../models/limits-result.model';
import { LimitsService } from '../services/limits.service';
import { SessionStore } from './session.store';

const PHONE = 'PHONE';
const EMAIL = 'EMAIL';

@Injectable()
export class AppState {
  private _state = new AppStateModel();
  private _stateSrc = new BehaviorSubject<AppStateModel>(this._state);
  private _session: Session;
  state$ = this._stateSrc.asObservable();
  private amountSubject: Subject<number> = new Subject();
  private request: Subscription;

  constructor(private sessionStore: SessionStore, private limitsService: LimitsService) {
    console.log('constructing app state store');
    this.sessionStore.session$
      .filter((session) => !!session && !!session.accounts)
      .subscribe((session) => {
        console.log('state store got session');
        this._session = session;
        this.multiFactorModel = null;
        this.accounts = session.accounts;
      });
    this.amountSubject.pipe(debounceTime(500)).subscribe((amount: number) => {
      if (amount === 0) {
        return;
      }
      this.loadingIsLimits = true;
      if (this.request) {
        this.request.unsubscribe();
      }
      this.request = this.limitsService.verify(amount).subscribe((result) => {
        this.limitsResult = result;
        this.loadingIsLimits = false;
      });
    });
  }

  onChange(property: string) {
    console.log(`on state change: ${property}`);
    this._stateSrc.next(this._state);
  }

  get isValid(): boolean {
    return this._state.isValid;
  }

  set loadingIsLimits(result: boolean) {
    this._state.loadingIsLimits = result;
    this.onChange('loadingIsLimits');
  }

  get loadingIsLimits(): boolean {
    return this._state.loadingIsLimits;
  }

  set limitsResult(result: LimitsResult) {
    this._state.limitsResult = result;
    this.onChange('limitsResult');
  }

  get limitsResult(): LimitsResult {
    return this._state.limitsResult;
  }

  set accounts(value: BankAccount[]) {
    this._state.accounts = value;
    this.onChange('accounts');
  }

  get accounts(): BankAccount[] {
    return this._state.accounts;
  }

  set useMfaModal(value: boolean) {
    this._state.useMfaModal = value;
    this.onChange('useMfaModal');
  }

  get useMfaModal(): boolean {
    return this._state.useMfaModal;
  }

  set hasPassedMfa(value: boolean) {
    this._state.hasPassedMfa = value;
    this.onChange('hasPassedMfa');
  }

  get hasPassedMfa(): boolean {
    return this._state.hasPassedMfa;
  }

  set hideMaskedAccount(value: boolean) {
    this._state.hideMaskedAccount = value;
    this.onChange('hideMaskedAccount');
  }

  get hideMaskedAccount(): boolean {
    return this._state.hideMaskedAccount;
  }

  set isNew(value: boolean) {
    this._state.isNew = value;
    this.onChange('isNew');
  }

  get isNew(): boolean {
    return this._state.isNew;
  }

  get passMfa(): boolean {
    return this._state.passMfa;
  }

  get passSecret(): boolean {
    return this._state.passSecret;
  }

  get secretWasSet(): boolean {
    return this._state.secretWasSet;
  }

  setSecretWord(secret: string) {
    this._state.template.secret = secret;
    this._state.secretWasSet = true;
    this.onChange('secret word');
  }

  set template(template: Template) {
    this._state.secretWasSet = false;
    this._state.template = template;
    this.onChange('template');
  }

  get template(): Template {
    return this._state.template;
  }

  set payment(payment: Payment) {
    this._state.payment = payment;
    const nextAmount = payment && payment.amount ? payment.amount : null;
    this.amountSubject.next(nextAmount);
    this.onChange('payment');
  }

  get payment(): Payment {
    return this._state.payment;
  }

  set bankAccount(bankAccount: BankAccount) {
    this._state.bankAccount = bankAccount;
    this.onChange('bankAccount');
  }

  get bankAccount(): BankAccount {
    return this._state.bankAccount;
  }

  set multiFactorModel(multiFactorModel: MultiFactorModel) {
    this._state.multiFactorModel = multiFactorModel;
    this.onChange('multiFactorModel');
  }

  get multiFactorModel(): MultiFactorModel {
    return this._state.multiFactorModel;
  }

  set acknowledgeDuplicate(value: boolean) {
    this._state.acknowledgedDuplicate = value;
    this.onChange('userHasAcknowledgeDuplicatePayment');
  }

  get acknowledgeDuplicate(): boolean {
    return this._state.acknowledgedDuplicate;
  }

  set duplicateInfo(value: DuplicateInfo) {
    this._state.duplicateInfo = value;
    this.onChange('duplicateInfo');
  }

  get duplicateInfo(): DuplicateInfo {
    return this._state.duplicateInfo;
  }

  set selectedMethod(selectedMethod: SelectedMultiFactorMethod) {
    this._state.selectedMethod = selectedMethod;
    this.onChange('selectedMethod');
  }

  get selectedMethod(): SelectedMultiFactorMethod {
    return this._state.selectedMethod;
  }

  getCombinedInfo(): CombinedRequest {
    return new CombinedRequest(this.payment, this.template);
  }

  clear() {
    this.template = null;
    this.payment = null;
    const mfaModel = this._state.multiFactorModel;
    this._state = new AppStateModel(this._session);
    this._state.multiFactorModel = mfaModel;
    this.onChange('cleared all (except mfa model)!');
  }

  setContactInfo(input: string) {
    this.trim(input);
    const isPhone = input.indexOf('@') === -1;
    if (this.template) {
      if (isPhone) {
        this.template.p2PPayToEmailAddress = null;
        this.template.defaultContactMethod = PHONE;
        this.template.p2PPayToPhoneNumber = input;
        this.onChange('setContactInfo');
        return;
      }
      this.template.p2PPayToPhoneNumber = null;
      this.template.defaultContactMethod = EMAIL;
      this.template.p2PPayToEmailAddress = input;
      this.onChange('setContactInfo');
    }
  }

  trim(input: string) {
    return input.replace(/\s/g, '');
  }

  getContactInfo(): string {
    if (!this.template) {
      return null;
    }
    switch (this.template.defaultContactMethod) {
      case EMAIL:
        return this.template.p2PPayToEmailAddress;
      case PHONE:
        return this.template.p2PPayToPhoneNumber;
    }
  }

  getDefaultAccount(): Observable<BankAccount> {
    return this.state$.pipe(
      first((state) => !!state && !!state.accounts),
      map((state) => {
        if (
          state.template &&
          state.template.payFromBankAccount &&
          (!state.template.customerMessages || state.template.customerMessages.length === 0)
        ) {
          return state.template.payFromBankAccount;
        }
        if (!state.accounts) {
          return null;
        }
        return state.accounts.reverse().reduce((p, c) => c, null);
      })
    );
  }
}

results matching ""

    No results matching ""