I am using Redux-Thunk for Reactjs project. but I am not sure how I have to make a redux store. making the redux store for each web page will be good? now I made the stores for each object (maybe it is related to each database table) such as sites, rooms, sensors...
import { siteConstants } from '../_constants';
function sites(state = {}, action) {
switch (action.type) {
case siteConstants.GETALL_REQUEST:
return {
...state,
get : {
status : 'loading',
data : [],
}
};
case siteConstants.GETALL_SUCCESS:
return {
...state,
get : {
status : 'success',
data : action.sites,
}
};
case siteConstants.GETALL_FAILURE:
return {
...state,
get : {
status : 'failure',
data : [],
error: action.error
}
};
case siteConstants.GET_REQUEST:
return {
...state,
selected_site : {
status : 'loading',
data : [],
}
};
case siteConstants.GET_SUCCESS:
return {
...state,
selected_site : {
status : 'success',
data : action.site,
}
};
case siteConstants.GET_FAILURE:
return {
...state,
selected_site : {
status : 'failure',
data : [],
error: action.error
}
};
case siteConstants.ADDNEW_REQUEST:
return {
...state,
add : {
status : 'adding',
}
};
case siteConstants.ADDNEW_SUCCESS:
return {
...state,
add : {
status : 'success',
},
get : {
...state.get,
data : [
...state.get.data,
{
_id : action.site._id,
name : action.site.name,
description : action.site.description,
vectorMapUrl : action.site.vectorMapUrl,
}
]
}
};
case siteConstants.ADDNEW_FAILURE:
return {
...state,
add : {
status : 'failure',
error : action.error,
}
};
case siteConstants.DELETE_REQUEST:
return {
...state,
delete : {
status : 'deleting',
}
};
case siteConstants.DELETE_SUCCESS:
return {
...state,
delete : {
status : 'success',
},
get : {
...state.get,
data : [
...state.get.data.slice(0, action.index),
...state.get.data.slice(action.index + 1)
]
}
};
case siteConstants.DELETE_FAILURE:
return {
...state,
delete : {
status : 'success',
error : action.error,
}
};
case siteConstants.UPDATE_REQUEST:
return {
...state,
update : {
status : 'updating',
}
};
case siteConstants.UPDATE_SUCCESS:
return {
...state,
update : {
status : 'success',
},
get : {
...state.get,
data : [
...state.get.data.slice(0, action.i),
{
_id : action.site_id,
name : action.name,
description : action.description,
vectorMapUrl : action.vectorMapUrl,
},
...state.get.data.slice(action.i + 1)
]
}
};
case siteConstants.UPDATE_FAILURE:
return {
...state,
update : {
status : 'failure',
error : action.error,
}
};
default:
return state;
}
}
export default sites;
as you see, current there is only one store for sites CRUD on my web application project. but there are relations between data tables. for example
- sites table
- rooms table will have site id
- sensors table will have site id and room id
for this relation, what is the best store structure for React Redux