refactored requestCacheReducer

This commit is contained in:
Art Lowel
2017-04-12 18:03:02 +02:00
parent 6212f5d114
commit 8e0d2bac9b
47 changed files with 1341 additions and 1309 deletions

View File

@@ -0,0 +1,43 @@
import {
HrefIndexAction, HrefIndexActionTypes, AddToHrefIndexAction,
RemoveUUIDFromHrefIndexAction
} from "./href-index.actions";
export interface HrefIndexState {
[href: string]: string
}
// Object.create(null) ensures the object has no default js properties (e.g. `__proto__`)
const initialState: HrefIndexState = Object.create(null);
export const hrefIndexReducer = (state = initialState, action: HrefIndexAction): HrefIndexState => {
switch (action.type) {
case HrefIndexActionTypes.ADD: {
return addToHrefIndex(state, <AddToHrefIndexAction>action);
}
case HrefIndexActionTypes.REMOVE_UUID: {
return removeUUIDFromHrefIndex(state, <RemoveUUIDFromHrefIndexAction>action)
}
default: {
return state;
}
}
};
function addToHrefIndex(state: HrefIndexState, action: AddToHrefIndexAction): HrefIndexState {
return Object.assign({}, state, {
[action.payload.href]: action.payload.uuid
});
}
function removeUUIDFromHrefIndex(state: HrefIndexState, action: RemoveUUIDFromHrefIndexAction): HrefIndexState {
let newState = Object.create(null);
for (let href in state) {
if (state[href] !== action.payload) {
newState[href] = state[href];
}
}
return newState;
}