Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140101

How to run redux-saga middleware on the any dispatch action?

$
0
0

I have defined following redux saga middleware function in 'usersaga.js'

import {call, put, takeEvery, takeLatest} from "redux-saga/effects";
import {REQUEST_API_DATA, receiveApiData} from '../store/actions';

import userServiceObject from '../services/user.service';


// Worker Saga function will be fired on USER_FETCH_ACTIONS

export function* getApiData(action){
    console.log(action)
    try{
        // Do Api Call
        const data = yield call(userServiceObject.getUsers);
        console.log(data.data);
        yield put(receiveApiData(data.data));
    }
    catch(error){
        console.log("this is error part and executing");
        console.log(error);
    }
}

In the 'index.js' file I use "run" method to run above function getApiData

import React from 'react';
import ReactDOM from 'react-dom';
import {createStore, combineReducers, applyMiddleware } from 'redux';
import {Provider} from 'react-redux';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

import contactReducer from './store/reducers/userlist';

// For Creating Saga and connecting store to saga middleware
import createSagaMiddleware from 'redux-saga';
import {getApiData} from './sagas/usersaga';

const sagaMiddleware = createSagaMiddleware();

const rootReducer = combineReducers({
    res: contactReducer
})

const store = createStore(rootReducer, applyMiddleware(sagaMiddleware));

sagaMiddleware.run(getApiData);

ReactDOM.render(<Provider store={store}> <App /> </Provider>, document.getElementById('root'));



serviceWorker.unregister();

It successfully getting the data from the api and I successfully handle the data in acttion generated by 'receiveApiData' action generator function.
I want to call the getApiData function on a action which is genrated by function 'requestApiData' which is dispatched by. The action name is '{type: REQUEST_API_DATA}'
How to run Our getApiData on a action which is dispatched. My userlist reducer file look like below

import * as actionTypes from "../actions";

const initialState = {
    value: '',
    results: []
};

const reducer = (state = initialState, action) => {
    switch(action.type){
        case actionTypes.STORE_CONTACTS:   // This is for storing all contacts in the state here
            console.log(action.values)
            return {
                ...state,
                results: action.values
            }

        case actionTypes.RECEIVE_API_DATA:  
            console.log(action);
            return{
                ...state,
                results: action.contacts
            }
        case actionTypes.DELETE_CONTACT:
            return {
                ...state,
                results: action.value
            }
        case actionTypes.ADD_CONTACT:
            // perform add action to the database, update state and return All new data
            return {
                ...state,

            }
        case actionTypes.EDIT_CONTACT:
            return {
                ...state,
            }
        default:
            return state;

    }
};

export default reducer;

Viewing all articles
Browse latest Browse all 140101

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>