Stefan B
Stefan B

Reputation: 229

IF in Redux Observable epic

I have an epic that catch each dispatch of getting status (just item from state, like state.process:{ status: fail, success, inWork}, not an request status like 200, 500 etc). When status == success (by getting status from state) i need to dispatch another action like SET_STATUS_SUCCESS

const getStatus = (action, state) =>
    action.pipe(
        ofType(GET_STATUS),
        withLatestFrom(state),
        mergeMap(([action, state]) => {
            const { status } = state.api.process; //here is what i need, there is no problem with status.
            if (status === "success") {
              return mapTo(SET_STATUS_SUCCESS) //got nothing and error.
}
        })
    );

Now i receive error:

Uncaught TypeError: You provided 'function (source) { return source.lift(new MapToOperator(value)); }' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. at subscribeTo (subscribeTo.js:41)

What should i do? I tried just return setStatusSuccess action but it doesn't work too.

Upvotes: 2

Views: 1455

Answers (1)

mpontus
mpontus

Reputation: 2203

You need to return an observable from the function you pass to mergeMap. Try this:

const getStatus = (action, state) =>
  action.pipe(
    ofType(GET_STATUS),
    withLatestFrom(state),
    mergeMap(([action, state]) => {
      const { status } = state.api.process;

      if (status === 'success') {
        return of({ type: SET_STATUS_SUCCESS });
      } else {
        return EMPTY;
      }
    }),
  );

of and EMPTY are imported from rxjs.

Upvotes: 5

Related Questions