@cgnal/redux/reducers

Utilities for Redux reducers.

Source:

Methods

(static) combine(functions) → {function}

Source:
Since:
  • 0.0.20

Accepts a list of function meant to act on the same portion of a state, and combines them to produce a new state.

Example
const state = {
    counter: 0,
    values: [1, 2]
};
const incrementCounter = state => ({ ...state, counter: state.counter + 1 });
const addValue = (state, action) => ({ ...state, values: state.values.concat(action.payload) });
const action = { payload: 3, type: "ADD_VALUE" };
const combinedUpdate = combine([incrementCounter, addValue]);

combinedUpdate(state, action) // => { counter: 1, values: [1, 2, 3] }
Parameters:
Name Type Description
functions Array.<function()>
Returns:

(state:Object, action:Action) => newState:Object

Type
function

(static) handleHttpFailure(titleopt, messageopt, errorPathopt, isLoadingPathopt) → {function}

Source:
Since:
  • 0.0.20

Utility function to handle a common case of change in the store when a HTTP requests completes successfully. Resets loading path to false and sets a data path with the HTTP result, optionally using a transformer function.
Path strings are paths supplied to Lamb's setPathIn, implying that a dot is used as a separator (e.g. "view.isLoading").

Example
const state = {
    data: [],
    error: null,
    isLoading: true
};
const action = {
    error: true,
    payload: new Error("some error"),
    meta: {
        originalAction: { type: "DATA_GET" }
    },
    type: "DATA_GET_KO"
};
const handler = handleHttpFailure(); // using default values

handler(state, action) // =>
// {
//     data: [],
//     error: {
//         message: "Error during the HTTP request.",
//         originalAction: { type: "DATA_GET" },
//         originalError: Error, // the above action's payload
//         title: "HTTP Error"
//     },
//     isLoading: false
// }
Parameters:
Name Type Attributes Default Description
title String <optional>
"HTTP Error"

User friendly title.

message String <optional>
"Error during the HTTP request."

User friendly message.

errorPath String <optional>
"error"
isLoadingPath String <optional>
"isLoading"
Returns:

(state:Object, action:Action) => newState:Object

Type
function

(static) handleHttpStart(errorPathopt, isLoadingPathopt) → {function}

Source:
Since:
  • 0.0.20

Utility function to handle a common case of change in the store when a HTTP requests starts. Resets an error path and set a loading path to true.
Path strings are paths supplied to Lamb's setPathIn, implying that a dot is used as a separator (e.g. "view.isLoading").

Example
const state = {
    data: [],
    error: {
        message: "Error during the HTTP request.",
        originalAction: { type: "DATA_GET" },
        originalError: new Error("some message")
        title: "HTTP Error"
    },
    isLoading: false
};
const action = { type: "DATA_GET" };
const handler = handleHttpStart(); // using default values

// handler(state, action) // =>
// {
//     data: [],
//     error: null,
//     isLoading: true
// }
Parameters:
Name Type Attributes Default Description
errorPath String <optional>
"error"
isLoadingPath String <optional>
"isLoading"
Returns:

(state:Object, action:Action) => newState:Object

Type
function

(static) handleHttpSuccess(dataPathopt, isLoadingPathopt, transformeropt) → {function}

Source:
Since:
  • 0.0.20

Utility function to handle a common case of change in the store when a HTTP requests completes successfully. Resets loading path to false and sets a data path with the HTTP result, optionally using a transformer function.
Path strings are paths supplied to Lamb's setPathIn, implying that a dot is used as a separator (e.g. "view.isLoading").

Example
const state = {
    data: [],
    error: null,
    isLoading: true
};
const action = {
    payload: [1, 2, 3],
    type: "DATA_GET_OK"
};
const handler = handleHttpSuccess();  // using default values

handler(state, action) // =>
// {
//     data: [1, 2, 3],
//     error: null,
//     isLoading: false
// }
Parameters:
Name Type Attributes Default Description
dataPath String <optional>
"data"
isLoadingPath String <optional>
"isLoading"
transformer function <optional>
v => v

(v:Any, prevValue:Any) => newValue:Any

Returns:

(state:Object, action:Action) => newState:Object

Type
function

(static) putPayloadInKey(key) → {function}

Source:
Since:
  • 0.0.6

Builds a function that will put a flux standard action payload in the given key of the redux state.

Example
const state = {
    data: []
};
const action = { payload: [1, 2, 3], type: "SET_DATA" };
const handler = putPayloadInKey("data");

handler(state, action) // => { data: [1, 2, 3] }
Parameters:
Name Type Description
key String
Returns:

(state:Object, action:Action) => newState:Object

Type
function

(static) putPayloadInPath(path, separatoropt) → {function}

Source:
Since:
  • 0.0.6

Builds a function that will put a flux standard action payload in the given path of the redux state.
Path strings are paths supplied to Lamb's setPathIn.

Example
const state = {
    view: {
        data: []
    }
};
const action = { payload: [1, 2, 3], type: "SET_DATA" };
const handler = putPayloadInPath("view.data");

handler(state, action) // => { view: { data: [1, 2, 3] } }
Parameters:
Name Type Attributes Default Description
path String
separator String <optional>
"."
Returns:

(state:Object, action:Action) => newState:Object

Type
function