Map Plugin
#
Use casesThis plugin modifies the state shape into a dictionary shape. This is extremely useful when you have to work with omogeneous indexed data belonging to a collection. For example, any master-detail based API is a suitable candidate for the usage of this plugin
The map plugin works by changing the state shape, and adjusting selectors, actions and reducer accordingly.
The base state shape is
This shape is replicated for each key to be stored, and the indexed replicas are used as the new state
In order to get this working, you need to configure a keyMakerFunction, that is, a function able to associate any dispatched action (among those regarding the mapped rocketjump) to a key in the store.
The default keyMakerFunction is the following
The map plugin provides you ad-hoc actions and selectors to interact with the state shape described beforehand
#
ConfigurationThis plugin supports some configuration options:
- key: The keyMaker function described in the previous paragraph
- dataTransform: Custom transformation to be applied to data before storing them in the map. If set, it must be a function that is passed the output of the async task as a parameter and that must return the content to store in the state, under the
data
key - keepCompleted: Boolean indicating whether to keep in the state entries that correspond to completed tasks or to delete them on completion. This can be useful for instance do deal with situations where we need to track in-flight tasks, but we don't care about their output, just about their completion.
#
Usage#
ActionsThis plugin injects in the actions
bag the following action creators:
- runKey: performs standard
run
action on an item, given its key. The given id is expected to be the first parameter of the call and is then passed down to the apiparams
array and copied into themeta
object under theid
key. Hence, the signature of this function is(id, ...otherParams) => void
- cleanKey: performs standard
clean
action on an item, given its key
#
SelectorsThis plugin injects in the selectors
bag the following selectors:
- getMapData: retrieves data key from any item, and returns them indexed by key
- getMapLoadings: retrieves loading state from any item, and returns them indexed by key
- getMapFailures: retrieves error key from any item, and returns them indexed by key
Basically, provided selectors slice the state vertically: