Permalink
Browse files

Add generic reducer logic for editing an entity

  • Loading branch information...
markerikson committed Jan 25, 2017
1 parent 6376ebd commit 018c55214a03d25a8da8592390567dbd5878c411
@@ -0,0 +1,37 @@
+import {
+ EDIT_ITEM_EXISTING,
+ EDIT_ITEM_UPDATE,
+ EDIT_ITEM_STOP
+} from "./editingConstants";
+
+
+export function editExistingItem(itemType, itemID) {
+ return {
+ type : EDIT_ITEM_EXISTING,
+ payload : {
+ itemType,
+ itemID
+ },
+ };
+}
+
+export function editItemAttributes(itemType, itemID, newItemAttributes) {
+ return {
+ type : EDIT_ITEM_UPDATE,
+ payload : {
+ itemType,
+ itemID,
+ newItemAttributes,
+ },
+ };
+}
+
+export function stopEditingItem(itemType, itemID) {
+ return {
+ type : EDIT_ITEM_STOP,
+ payload : {
+ itemType,
+ itemID
+ },
+ };
+}
@@ -0,0 +1,3 @@
+export const EDIT_ITEM_EXISTING = "EDIT_ITEM_EXISTING";
+export const EDIT_ITEM_UPDATE = "EDIT_ITEM_UPDATE";
+export const EDIT_ITEM_STOP = "EDIT_ITEM_STOP";
@@ -1,7 +1,70 @@
import {createReducer} from "common/utils/reducerUtils";
+import {
+ createEntity,
+ updateEntity,
+ deleteEntity
+} from "features/entities/entityReducer";
+
+import {
+ EDIT_ITEM_EXISTING,
+ EDIT_ITEM_UPDATE,
+ EDIT_ITEM_STOP
+} from "./editingConstants";
+
+
+import {selectEntities} from "features/entities/entitySelectors";
+import {selectEditingEntities} from "./editingSelectors";
+import {
+ readEntityData,
+ updateEditingEntitiesState,
+} from "./editingUtils";
+
+
+
+export function copyEntity(sourceEntities, destinationEntities, payload) {
+ const {itemID, itemType} = payload;
+
+ const newItemAttributes = readEntityData(sourceEntities, itemType, itemID);
+ const creationPayload = {itemType, itemID, newItemAttributes}
+
+ const updatedEntities = createEntity(destinationEntities, creationPayload);
+ return updatedEntities;
+}
+
+
+
+
+export function editItemExisting(state, payload) {
+ const entities = selectEntities(state);
+ const editingEntities = selectEditingEntities(state);
+
+ const updatedEditingEntities = copyEntity(entities, editingEntities, payload);
+
+ return updateEditingEntitiesState(state, updatedEditingEntities);
+}
+
+export function editItemUpdate(state, payload) {
+ const editingEntities = selectEditingEntities(state);
+
+ const updatedEditingEntities = updateEntity(editingEntities, payload);
+ return updateEditingEntitiesState(state, updatedEditingEntities);
+}
+
+export function editItemStop(state, payload) {
+ const editingEntities = selectEditingEntities(state);
+
+ const updatedEditingEntities = deleteEntity(editingEntities, payload);
+ return updateEditingEntitiesState(state, updatedEditingEntities);
+}
+
+
+
const editingFeatureReducer = createReducer({}, {
+ [EDIT_ITEM_EXISTING] : editItemExisting,
+ [EDIT_ITEM_UPDATE] : editItemUpdate,
+ [EDIT_ITEM_STOP] : editItemStop,
});
export default editingFeatureReducer;
@@ -0,0 +1 @@
+export const selectEditingEntities = state => state.editingEntities;

0 comments on commit 018c552

Please sign in to comment.