Permalink
Browse files

Add generic entity creation and deletion handling

  • Loading branch information...
markerikson committed Jan 22, 2017
1 parent 42693a3 commit 75cc30ab85b22b3af7f0a70d9a23a2d646833855
@@ -1,4 +1,8 @@
import {ENTITY_UPDATE} from "./entityConstants";
import {
ENTITY_UPDATE,
ENTITY_DELETE,
ENTITY_CREATE,
} from "./entityConstants";
export function updateEntity(itemType, itemID, newItemAttributes) {
return {
@@ -9,4 +13,21 @@ export function updateEntity(itemType, itemID, newItemAttributes) {
newItemAttributes,
},
};
}
export function deleteEntity(itemType, itemID) {
return {
type : ENTITY_DELETE,
payload : {itemType, itemID}
};
}
export function createEntity(itemType, newItemAttributes) {
return {
type : ENTITY_CREATE,
payload : {
itemType,
newItemAttributes,
},
};
}
@@ -1 +1,3 @@
export const ENTITY_UPDATE = "ENTITY_UPDATE";
export const ENTITY_UPDATE = "ENTITY_UPDATE";
export const ENTITY_CREATE = "ENTITY_CREATE";
export const ENTITY_DELETE = "ENTITY_DELETE";
@@ -1,4 +1,8 @@
import {ENTITY_UPDATE} from "./entityConstants";
import {
ENTITY_UPDATE,
ENTITY_DELETE,
ENTITY_CREATE,
} from "./entityConstants";
import {createConditionalSliceReducer} from "common/utils/reducerUtils";
@@ -23,8 +27,44 @@ export function updateEntity(state, payload) {
return newState;
}
export function deleteEntity(state, payload) {
const {itemID, itemType} = payload;
const session = schema.from(state);
const ModelClass = session[itemType];
let newState = state;
if(ModelClass.hasId(itemID)) {
const modelInstance = ModelClass.withId(itemID);
modelInstance.delete();
// Immutably apply updates and return the new entities structure
newState = session.reduce();
}
return newState;
}
export function createEntity(state, payload) {
const {itemType, newItemAttributes} = payload;
const session = schema.from(state);
const ModelClass = session[itemType];
ModelClass.parse(newItemAttributes);
const newState = session.reduce();
return newState;
}
const entityHandlers = {
[ENTITY_UPDATE] : updateEntity,
[ENTITY_CREATE] : createEntity,
[ENTITY_DELETE] : deleteEntity,
};
const entityCrudFeatureReducer = createConditionalSliceReducer("entities", entityHandlers);

0 comments on commit 75cc30a

Please sign in to comment.