Permalink
Browse files

Add logic to apply item edits to the "current data" slice

  • Loading branch information...
markerikson committed Jan 22, 2017
1 parent df88219 commit 4bd028d8584713b470120f302d16ab1ff14a42cc
@@ -1,7 +1,8 @@
import {
EDIT_ITEM_EXISTING,
EDIT_ITEM_UPDATE,
EDIT_ITEM_STOP
EDIT_ITEM_APPLY,
EDIT_ITEM_STOP,
} from "./editingConstants";
@@ -26,6 +27,16 @@ export function editItemAttributes(itemType, itemID, newItemAttributes) {
};
}
export function applyItemEdits(itemType, itemID) {
return {
type : EDIT_ITEM_APPLY,
payload : {
itemType,
itemID
},
};
}
export function stopEditingItem(itemType, itemID) {
return {
type : EDIT_ITEM_STOP,
@@ -1,3 +1,4 @@
export const EDIT_ITEM_EXISTING = "EDIT_ITEM_EXISTING";
export const EDIT_ITEM_UPDATE = "EDIT_ITEM_UPDATE";
export const EDIT_ITEM_APPLY = "EDIT_ITEM_APPLY";
export const EDIT_ITEM_STOP = "EDIT_ITEM_STOP";
@@ -1,5 +1,7 @@
import {createReducer} from "common/utils/reducerUtils";
import schema from "app/schema";
import {
createEntity,
updateEntity,
@@ -9,14 +11,17 @@ import {
import {
EDIT_ITEM_EXISTING,
EDIT_ITEM_UPDATE,
EDIT_ITEM_STOP
EDIT_ITEM_APPLY,
EDIT_ITEM_STOP,
} from "./editingConstants";
import {getModelByType} from "common/utils/modelUtils";
import {selectEntities} from "features/entities/entitySelectors";
import {selectEditingEntities} from "./editingSelectors";
import {
readEntityData,
updateEntitiesState,
updateEditingEntitiesState,
} from "./editingUtils";
@@ -34,6 +39,41 @@ export function copyEntity(sourceEntities, destinationEntities, payload) {
export function updateEditedEntity(sourceEntities, destinationEntities, payload) {
// Start by reading our "work-in-progress" data
const readSession = schema.from(sourceEntities);
const {itemType, itemID} = payload;
// Look up the model instance for the requested item
const model = getModelByType(readSession, itemType, itemID);
// We of course will be updating our "current" relational data
let writeSession = schema.from(destinationEntities);
const ModelClass = writeSession[itemType];
if(ModelClass.hasId(itemID)) {
// Look up the original Model instance for the top item
const existingItem = ModelClass.withId(itemID);
if(existingItem.updateFrom) {
// Each model class should know how to properly update itself and its
// relations from another model of the same type. Ask the original model to
// update itself based on the "work-in-progress" model, which queues up a
// series of immutable add/update/delete actions internally
existingItem.updateFrom(model);
}
}
// Immutably apply the changes and generate our new "current" relational data
const updatedEntities = writeSession.reduce();
return updatedEntities;
}
export function editItemExisting(state, payload) {
const entities = selectEntities(state);
@@ -59,12 +99,21 @@ export function editItemStop(state, payload) {
}
export function editItemApply(state, payload) {
const entities = selectEntities(state);
const editingEntities = selectEditingEntities(state);
const updatedEntities = updateEditedEntity(editingEntities, entities, payload);
return updateEntitiesState(state, updatedEntities);
}
const editingFeatureReducer = createReducer({}, {
[EDIT_ITEM_EXISTING] : editItemExisting,
[EDIT_ITEM_UPDATE] : editItemUpdate,
[EDIT_ITEM_APPLY] : editItemApply,
[EDIT_ITEM_STOP] : editItemStop,
});
export default editingFeatureReducer;
export default editingFeatureReducer;

0 comments on commit 4bd028d

Please sign in to comment.