Permalink
Browse files

Add a "entity feature reducer" and a generic "entity update reducer"

  • Loading branch information...
markerikson committed Jan 13, 2017
1 parent 3b5b2bf commit 1d5ccae612a97c59782df42680cc3282df31c861
@@ -8,6 +8,8 @@ import unitInfoReducer from "features/unitInfo/unitInfoReducer";
import pilotsReducer from "features/pilots/pilotsReducer";
import mechsReducer from "features/mechs/mechsReducer";
import entityCrudReducer from "features/entities/entityReducer";
const combinedReducer = combineReducers({
entities : entitiesReducer,
@@ -20,6 +22,7 @@ const combinedReducer = combineReducers({
const rootReducer = reduceReducers(
combinedReducer,
entityCrudReducer,
);
export default rootReducer;
@@ -0,0 +1,12 @@
import {ENTITY_UPDATE} from "./entityConstants";
export function updateEntity(itemType, itemID, newItemAttributes) {
return {
type : ENTITY_UPDATE,
payload : {
itemType,
itemID,
newItemAttributes,
},
};
}
@@ -0,0 +1 @@
export const ENTITY_UPDATE = "ENTITY_UPDATE";
@@ -0,0 +1,32 @@
import {ENTITY_UPDATE} from "./entityConstants";
import {createConditionalSliceReducer} from "common/utils/reducerUtils";
import schema from "app/schema";
export function updateEntity(state, payload) {
const {itemType, itemID, newItemAttributes} = payload;
const session = schema.from(state);
const ModelClass = session[itemType];
let newState = state;
if(ModelClass.hasId(itemID)) {
const modelInstance = ModelClass.withId(itemID);
modelInstance.update(newItemAttributes);
newState = session.reduce();
}
return newState;
}
const entityHandlers = {
[ENTITY_UPDATE] : updateEntity,
};
const entityCrudFeatureReducer = createConditionalSliceReducer("entities", entityHandlers);
export default entityCrudFeatureReducer;

0 comments on commit 1d5ccae

Please sign in to comment.