MontyGoldy
MontyGoldy

Reputation: 749

Redux Saga - How to call multiple actions after logging in

What I want to do is after logging into the app, I want to dispatch a couple of actions in my sagas to get the total no. of feedbacks and employees. Could someone help me understanding how I can do that :-

Login Saga

function* loginSaga(action) {
  try {
    const userData = yield call(api.auth.login, action.payload);
    yield put(actions.loginUser(userData));

    //Save to localstorage
    localStorage.setItem("jwtToken", userData.token);
    //Set token to auth header
    setAuthToken(userData.token);
    //Decode token to get userData
    const decoded = jwt_decode(userData.token);
    //Set Current User
    yield put(actions.setCurrentUser(decoded));
    history.push("/review");
  } catch (err) {
    yield put(actions.loginUserErrors(err.response.data));
  }
}

Here is mY action

//Complete login user
export const loginUserRequest = userData => ({
  type: actionTypes.LOGIN_USER_REQUEST,
  payload: userData
});

export const loginUser = userData => ({
  type: actionTypes.LOGIN_USER,
  payload: userData
});

export const loginUserErrors = errors => ({
  type: actionTypes.GET_ERRORS,
  payload: errors
});

So I want to more actions into login saga for example

//Get Feedbacks
export const getFeedbacksRequest = () => ({
  type: actionTypes.GET_FEEDBACKS_REQUEST
});

export const getFeedbacks = feedbacks => ({
  type: actionTypes.GET_FEEDBACKS,
  payload: feedbacks
});

export const getFeedbacksErrors = errors => ({
  type: actionTypes.GET_ERRORS,
  payload: errors
});

How can I add more in login saga so that when the user logs in these actions will be dispatched and get the lists.

I tried by adding this yield call(getFeedbacksRequest()) but it is throwing an error. I want to add 3-4 actions like that.

Upvotes: 1

Views: 13463

Answers (2)

Brandon Durham
Brandon Durham

Reputation: 7727

You can bundle them in an array, like so:

yield all([
    put(actionOne()),
    put(actionTwo()),
]);

Upvotes: 20

Aliaksandr Sushkevich
Aliaksandr Sushkevich

Reputation: 12384

To dispatch more actions you need to use put yield put(getFeedbacksRequest()). But if you need to do more requests to the server in you login saga you need to create similar sagas with api calls and then you can call them using call or all inside your loginSaga.

Upvotes: 1

Related Questions