How can I call a function if response is empty

75 Views Asked by At

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,
    });
  }
1

There are 1 best solutions below

1
On BEST ANSWER

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))
        );
    })
);