mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-16 06:23:03 +00:00
refactored requestCacheReducer
This commit is contained in:
43
src/app/core/index/href-index.reducer.ts
Normal file
43
src/app/core/index/href-index.reducer.ts
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user