Sawtaytoes
10/16/2019 - 7:35 AM

fetchUserEpic - Using Dispatch

const fetchUserEpic = (
  action$,
  state$,
  { dispatch },
) => (
  action$
  .pipe(
    ofType(FETCH_USER),
    tap(() => {
      dispatch(
        setLoading(
          'login',
        )
      )
    }),
    switchMap(({
      password,
      username,
    }) => (
      ajax
      .getJSON(
        'https://api.example.com/login'
      )
      .pipe(
        catchError((
          error,
        ) => {
          dispatch(
            fetchUserFailed(
              error,
            )
          )

          return EMPTY
        }),
      )
    )),
    tap(({
      authToken,
    }) => {
      dispatch(
        fetchUserSucceeded(
          authToken,
        )
      )
      
      dispatch(
        setLoaded(
          'login',
        )
      )
    }),
    ignoreElements(),
  )
)