@lore/utils
v1.0.0-beta.1
Published
Functions and files used across multiple Lore packages
Downloads
10
Readme
@lore/utils
A catch-all folder for any functions used in multiple places across the Lore ecosystem.
- ActionTypes: Factory that generates ActionTypes based on Lore's naming conventions
- PayloadStates: Default PayloadStates used by the action blueprints
- Hook: Defines the interface implemented by all hooks
- payload: A function to convert a Model into a payload for an action
- payloadCollection: A function to convert a Collection into a payload for an action
Payload
Converts a model into a payload for an action. Also defines the structure of the data passed around the application.
function payload(model, state, error) {
return {
id: model.id,
cid: model.cid,
state: state,
error: error || {},
data: model.toJSON()
};
}
PayloadCollection
Converts a collection into a payload for an action. Also defines the structure of the data passed around the application.
function payloadCollection(collection, state, error, query) {
return {
state: state,
error: error || {},
data: collection.models.map(function( model ) {
return payload(model, state, error);
}),
query: query,
meta: collection.meta
};
}
PayloadStates
Stores the PayloadStates used by action and reducer blueprints. Default states are:
module.exports = {
INITIAL_STATE: 'INITIAL_STATE',
RESOLVED: 'RESOLVED',
NOT_FOUND: 'NOT_FOUND',
CREATING: 'CREATING',
UPDATING: 'UPDATING',
DELETING: 'DELETING',
FETCHING: 'FETCHING',
ERROR_CREATING: 'ERROR_CREATING',
ERROR_UPDATING: 'ERROR_UPDATING',
ERROR_DELETING: 'ERROR_DELETING',
ERROR_FETCHING: 'ERROR_FETCHING'
};
Needed improvements
Something's weird about this. The true set of PayloadStates (from Lore's perspective) is this file plus whatever
custom Payload States the user specifies in src/constants/PayloadStates.js
, a file which starts off empty.
This file was created because Lore needs to emit PayloadStates in the action blueprints. But it's obnoxious and opaque
that there's no easy way for a user to learn what they are. One idea is created a CLI command like lore payloadStates
that lists the full set of payload states for the application. That would at least give the user something to
copy/paste when they need to check them. Another option is to copy them into src/constants/PayloadStates
as part of
the '@lore/generate-new' project generator (the thing responsible for generating the new project structure).
Additionally, PayloadStates are usable in any component, and trying to keep track of the relative path is obnoxious. It doesn't take a very large app before you have require statements in components like:
var PayloadStates = require('../../../../constants/PayloadStates');
We should solve this by either:
Aliasing
src/constants
inwebpack.config.js
asconstants
, so you can require PayloadStates asrequire('constants/PayloadStates')
Attaching PayloadStates to lore as lore.PayloadStates.XYZ. The downside with that is auto-complete will break, as PayloadStates becomes a dynamic object, but I believe aliasing will also cause that issue.