A
A
Alexander2021-08-26 19:30:32
redux
Alexander, 2021-08-26 19:30:32

How to pass data to redux saga via redux slice?

Can you please tell me how to pass data through action to redux toolkit so that redux saga will pick it up with the right action type?

const onSubmit: SubmitHandler<ILoginInputs> = async (data) => {
    dispatch(fetchSignIn(data))
  }

const usersSlice = createSlice({
  name: 'users',
  initialState: {
    data: undefined,
    isAuth: false,
  },
  reducers: {
    fetchSignIn: (data: ILoginInputs) => {
      return data
    },
    setUserData: (state, action) => {
      state.data = action.payload
    },

    setAuth: (state, action) => {
      state.isAuth = action.payload
    },
  },
})

export default usersSlice.reducer
export const { fetchSignIn, setUserData, setAuth } = usersSlice.actions


export function* fetchLoginWorker(payload: any): Generator<any> {
  try {
    const response: any = yield call(AuthApi.login, payload)
    localStorage.setItem('token', response.data.accessToken)
    yield put(setAuth(true))
    yield put(setUserData(response.data.user))
  } catch (e) {
    console.log(e.response?.data?.message)
  }
}

export function* usersSaga() {
  yield takeEvery(fetchSignIn.type, fetchLoginWorker)
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question