angularngrxngrx-storengrx-effectsngrx-selectors

i am trying to get selector value, store it in a variable array and pass this array to a success action in account.effect.ts file using ngrx


account.effect.ts File

fetchUserInfo$ = createEffecct(() =>
  this.actions$.pipe(
    ofType(AccountActions._fetchAccountInfo),
    switchMap(({accountNumber}) =>
      this.accServvice.getAccInfo(accountNum).pipe(
        map((accountInfo: AccType) =>
          AccountActions.loadUserInfoSuccess({data: accountInfo}),
        ),
        catchError((error) =>
          of(AccountActions.userDataFails({error}))
        )
      )
    )
  )
)

In the above file i have imported selector and you can access it by this.store.select(FromAcc.getSelectedAccInfo). i want to access selector value FromAcc.getSelectedAccInfo and add one more value which i will receive from service and then send it to action AccountActions.loadUserInfoSuccess. I am very new to ngrx. Please tell me how can i do that.


Solution

  • you can try below solution

    fetchUserInfo$ = createEffecct(() =>
      this.actions$.pipe(
        ofType(AccountActions._fetchAccountInfo),
        // below 2 lines updated
        withLatestFrom(this.store.select(FromAcc.getSelectedAccInfo)), 
        switchMap(([accountNumber, selectedAccInfo]) =>
          this.accServvice.getAccInfo(accountNum).pipe(
            map(([accountInfo: AccType, ) =>
              AccountActions.loadUserInfoSuccess({data: accountInfo}),
            ),
            catchError((error) =>
              of(AccountActions.userDataFails({error}))
            )
          )
        )
      )
    )