otejiri
otejiri

Reputation: 1017

How can I call a function if response is empty

How can I call openSnackbar function if response is empty but return LoadUserSuccess if response is not empty. I can't seem to find a solution that works.

  @Effect()
  validateOtp$: Observable<Action> = this.actions$.pipe(
    ofType<otpActions.Otp>(otpActions.OTP),
    switchMap((action) => {
      return this.otpService.validateOtp(action.payload).pipe(
        map(
          (response) => new userActions.LoadUserSucess(response)

          // this.openSnackBar()
        )
      );
    })
  );

  openSnackBar() {
    this.snackBar.open('Code invalid / expired', 'Close', {
      duration: 3500,
    });
  }

Upvotes: 0

Views: 65

Answers (1)

Rafi Henig
Rafi Henig

Reputation: 6422

Simply follow the example below:

validateOtp$: Observable <Action> = this.actions$.pipe(
    ofType<otpActions.Otp>(otpActions.OTP),
    switchMap((action) => {
        return this.otpService.validateOtp(action.payload).pipe(
            tap(x => !x && this.openSnackBar()),
            filter(x => !!x),
            map(x => new userActions.LoadUserSucess(x))
        );
    })
);

Upvotes: 1

Related Questions