[lore-hook-connect] simplify blueprints and mapping #168
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR simplifies the interface for extending and overriding the blueprints and mapping behavior used by
lore-hook-connect
. It also adds two new blueprints;all
andbyCid
.The
all
blueprint is extremely helpful for finishing the Next Steps section of the Quickstart, as it provides a simple way to retrieving the new tweets.The
all
BlueprintThe
all
blueprint is intended to provide a way to filter and sort reducer data when the intent is to NOT make a server call. For example, in the Quickstart we fetch a page of tweets, and sometimes create new tweets. In this scenario, we know the new tweets are in the store, and we want to display them on top of the paginated tweets.Before that, accomplishing that was not straight forward, as it required you having to access the Redux Store directly, getting all the data from the
byCid
reducer, and then filtering and sorting it to get what you want.Now you can skip that process, and provide the
filter
andsortBy
criteria directly to thegetState
call like this:The
getState
call above will return all tweets created after the timestamp or that don't have anid
(implying that were optimistically created, and should be displayed). ThesortBy
function then orders the tweets by theircreatedAt
date.The
byCid
BlueprintNot sure how useful this blueprint is in practice, but occasionally you do need to find data by the
cid
value, when you're waiting or looking for confirmation of when data is confirmed to be created by the server.If it makes sense to use a
lore.connect
call to accomplish that, now you can.The above
getState
call will look through thetweet.byCid
reducer and return the tweet with thecid
ofc2
. If no tweet exists with the matchingcid
, the call will returnundefined
.The Interface for Extending/Overriding Blueprints and Mapping
To illustrate the interface, let's take a look at how you could add the
all
blueprint yourself.First, you need to define the blueprint in the
config/connect.js
file like this:This blueprint defines two default parameters,
where
andsortBy
, has no action, and iterates through the providedreducerState
, filtering and sorting the results, which are then returned in a data structure with the state always set toRESOLVED
(since it makes no server calls, the state is never transient).Next, you need to tell lore how to use it. To do that, we need to define a
reducerActionMap
, and we can use the new*
syntax to say "this mapping applies to all models".That's it! With those two changes, you can easily extend and override the behavior of the
lore.connect
function and associatedgetState
calls.