Project import generated by Copybara.
GitOrigin-RevId: d9e9e3fb4e31372ec1fb43b178994ca78fa8fe70
diff --git a/static_src/reducers/redux-helpers.js b/static_src/reducers/redux-helpers.js
new file mode 100644
index 0000000..ce80d60
--- /dev/null
+++ b/static_src/reducers/redux-helpers.js
@@ -0,0 +1,64 @@
+// 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,
+ }),
+ });
+};