/** * This file provides utility functions for creating reducers that manage async data (in other * words, data fetched via requests to the server). * * Data is stored using the following schema: * * { * isLoading {boolean} - True when an async fetch operation is in progress for the data. Allows * us to avoid duplicate simultaneous fetch operations and to provide feedback to the user * during load operations (loading animations) * error {*} - If the most recent async fetch operation failed, this field will hold that error * lastUpdated {Date} - The last time the data was updated. Allows us to detect data that has yet * to be fetched as well as stale data * data {*} - The actual data * } * * Lists of data are also supported. * * Note that the generated reducers all expects actions to be in the Flux Standard Action (FSA) format: * https://github.com/acdlite/flux-standard-action#example. */ import * as _ from "lodash"; import type { DataAction, ErrorAction, ItemMap, ReduxAction, SubstateItemData, SubstateListData, } from "./types"; /** * An identity function that is the default reduce function for the generic reducers in this module. * Given the state and an action, returns the action's payload as the new item state for this * substate of the store. * * @template [S=any] The type representation of the Redux store state. Can be a full or partial * typing; if the state type represents a partial slice of the store state, it is recommended to * declare that type as an interface. * @template [P=any] The payload type for the given action * @param state {S} The Redux store state * @param action {ReduxAction
} The action being reduced
* @returns {P} The action payload
*/
const identityFn = (_: S, action: ReduxAction
): P => action.payload; /** * Returns a reducer that manages an async data item. * * @template [I=any] The type for an item returned by these selectors * @template [E=any] The error type for an item's failed load * @template [P=I] The action payload type * @param {function(I, ReduxAction
): I} [reduceFn] A function that produces a * new data entry, given the data entry from the current state and the action object. By default, * it is the identity function, which is only appropriate in cases where P = I; if you define an * explicit P type parameter, then you must also define this function parameter. * @return {function(SubstateItemData, ReduxAction
): SubstateItemData} The reducer */ export const newAsyncDataReducer = ( // @ts-expect-error identityFn is the default behavior where P = I reduceFn: (state: I, action: DataAction
) => I = identityFn, ) => ( state: SubstateItemData, action: DataAction
| ErrorAction ),
};
};
/**
* Returns a reducer that stores/replaces a single item in an async data list.
*
* @template [I=any] The type for an item returned by these selectors
* @template [E=any] The error type for an item's failed load
* @param {function(P | E): string} keyFn A function that returns a unique key for the list item
* @param {function(I, ReduxAction ): I} [itemReduceFn] A function that produces a
* new data entry, given the data entry from the current state and the action object. By default,
* it is the identity function, which is only appropriate in cases where P = I; if you define an
* explicit P type parameter, then you must also define this function parameter.
* @return {function(SubstateListData, ReduxAction ): SubstateListData} The reducer
*/
export const newAsyncListItemReducer = (
keyFn: (payload: P | E) => string,
// @ts-expect-error identityFn is the default behavior where P = I
reduceFn: (state: I, action: ReduxAction ) => I = identityFn,
) => (
state: SubstateListData,
action: DataAction | ErrorAction )} - The reducer
*/
export const newAsyncListItemDeleteReducer = (
keyFn: (payload: P) => string,
) => (state: SubstateListData, action: ReduxAction ): SubstateListData => {
const itemKey = keyFn(action.payload);
// Create a copy of the state and then delete the specified item to ensure that the state is a
// new reference
const newState = { ...state, data: { ...state.data } } as SubstateListData;
if (newState.data && newState.data[itemKey]) {
delete newState.data[itemKey];
}
return newState;
};
/**
* Returns a reducer that takes a list payload and stores it as an object, where each value is of
* the format described at the top of this file.
*
* @template [I=any] The type for an item returned by these selectors
* @template [E=any] The error type for an item's failed load
* @template [LE=E] The error type for the list's failed load. May or may not be the same as `E`.
* @param {function(I): string} keyFn - A function that returns a unique key for every
* payload list item
* @param {function(SubstateItemData, ReduxAction): I} [itemReduceFn] - A function that
* produces a new data entry for a single item in the list, given the data entry from the current
* item state and an action containing the item as its payload
* @return {function(SubstateListData, ReduxAction ): SubstateListData} - The reducer
*/
export const newAsyncListReducer = (
keyFn: (payload: P) => string,
// @ts-expect-error identityFn is the default behavior where P = I
itemReduceFn: (state: SubstateItemData, action: ReduxAction ) => I = identityFn,
) => (
state: SubstateListData,
action: DataAction | ErrorAction );
newState.data[itemKey] = newItemState;
}
return newState;
};