I have app, which loads the data from my server. I dispatch getData() action from my index file. I have couple reducers, joined with combineReducers into rootReducer.
I do get the data via getData. But how to dispatch the SET_DATA action, or simply change the state with the new data?
I can't call store.dispatch from the reducer and calling the action the way as I have it currently changes the data in my state, but doesn't redraw the page.
import { SYSTEM_FILTER, SET_DATA, GET_DATA, getData, setData } from '../actions/systemlist'
import rootReducer from './index'
const initialState = {
systemFilters: true,
systems: []
};
export default function systemlist(state=initialState, action) {
switch (action.type) {
case SYSTEM_FILTER:
var enabled=document.getElementById("filterSystems").checked;
return {systemFilters: enabled, systems: state.systems}
case GET_DATA:
console.log("Getting data");
$.getJSON("../data/systems.json", function(data) {
data=data.sort(function(a, b) {
var textA = a.name.toUpperCase();
var textB = b.name.toUpperCase();
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});
systemlist(state, {type: "SET_DATA", data: data});
});
return state
case SET_DATA:
console.log("Setting data");
$("#systemsMenuCounter").html(action.data.length);
return {systemFilters: state.systemFilters, systems: action.data}
default:
return state
}
}