-
-
Save tejasbubane/aa7477ca98c4c667a49d3eaf692dc717 to your computer and use it in GitHub Desktop.
Curried Reducers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { combineReducers } from "redux"; | |
import listReducer from "./listReducer"; | |
const rootReducer = combineReducers({ | |
products: listReducer("products"), | |
orders: listReducer("orders"), | |
warehouse_locations: listReducer("warehouse_locations") | |
}); | |
export default rootReducer; // later configured in store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Paginated List Reducer | |
import { combineReducers } from "redux"; | |
const listReducer = listType => { | |
const type = listType.toUpperCase(); | |
const status = (state = null, action) => { | |
switch (action.type) { | |
case `REQUEST_${type}`: | |
return "inprogress"; | |
case `FETCH_${type}_SUCCESS`: | |
return "success"; | |
case `FETCH_${type}_FAILURE`: | |
return "failure"; | |
default: | |
return state; | |
} | |
}; | |
const items = (state = [], action) => { | |
switch (action.type) { | |
case `FETCH_${type}_SUCCESS`: | |
return action.items; | |
case `FETCH_${type}_FAILURE`: | |
return []; // reset existing data | |
default: | |
return state; | |
} | |
}; | |
// Total number of items in the list (in all pages) | |
const totalCount = (state = 0, action) => { | |
switch (action.type) { | |
case `FETCH_${type}_SUCCESS`: | |
return action.totalCount; | |
case `FETCH_${type}_FAILURE`: | |
return 0; // reset existing data | |
default: | |
return state; | |
} | |
}; | |
const totalPages = (state = 0, action) => { | |
switch (action.type) { | |
case `FETCH_${type}_SUCCESS`: | |
return action.totalPages; | |
case `FETCH_${type}_FAILURE`: | |
return 0; // reset existing data | |
default: | |
return state; | |
} | |
}; | |
return combineReducers({ | |
status, | |
items, | |
totalCount, | |
totalPages | |
}); | |
}; | |
export default listReducer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment