ampache_react/app/reducers/paginate.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-07-07 23:23:18 +02:00
import { createReducer } from "../utils";
2016-07-26 15:59:18 +02:00
export const DEFAULT_LIMIT = 30; /** Default max number of elements to retrieve. */
2016-07-07 23:23:18 +02:00
const initialState = {
isFetching: false,
items: [],
total: 0,
error: ""
};
// Creates a reducer managing pagination, given the action types to handle,
// and a function telling how to extract the key from an action.
export default function paginate(types) {
if (!Array.isArray(types) || types.length !== 3) {
throw new Error("Expected types to be an array of three elements.");
}
if (!types.every(t => typeof t === "string")) {
throw new Error("Expected types to be strings.");
}
const [ requestType, successType, failureType ] = types;
return createReducer(initialState, {
[requestType]: (state) => {
return Object.assign({}, state, {
isFetching: true,
error: "",
});
},
[successType]: (state, payload) => {
return Object.assign({}, state, {
isFetching: false,
items: payload.items,
total: payload.total,
error: ""
});
},
[failureType]: (state, payload) => {
return Object.assign({}, state, {
isFetching: false,
error: payload.error
});
}
});
}