blob: ce80d60603c86ab844858ca9fa3b03f3f95a475f [file] [log] [blame]
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
export const createReducer = (initialState, handlers) => {
return function reducer(state = initialState, action) {
if (handlers.hasOwnProperty(action.type)) {
return handlers[action.type](state, action);
} else {
return state;
}
};
};
const DEFAULT_REQUEST_KEY = '*';
export const createKeyedRequestReducer = (start, success, failure) => {
return createReducer({}, {
[start]: (state, {requestKey = DEFAULT_REQUEST_KEY}) => {
return {
...state,
[requestKey]: {
requesting: true,
error: null,
},
};
},
[success]: (state, {requestKey = DEFAULT_REQUEST_KEY}) =>{
return {
...state,
[requestKey]: {
requesting: false,
error: null,
},
};
},
[failure]: (state, {requestKey = DEFAULT_REQUEST_KEY, error}) => {
return {
...state,
[requestKey]: {
requesting: false,
error,
},
};
},
});
};
export const createRequestReducer = (start, success, failure) => {
return createReducer({requesting: false, error: null}, {
[start]: (_state, _action) => ({
requesting: true,
error: null,
}),
[success]: (_state, _action) =>({
requesting: false,
error: null,
}),
[failure]: (_state, {error}) => ({
requesting: false,
error,
}),
});
};