0.18.1 • Published 7 years ago

redux-blueflag v0.18.1

Weekly downloads
65
License
ISC
Repository
github
Last release
7 years ago

redux-blueflag

Redux Blue Flag is a complete Redux setup for asynchronous data calls that uses Normalizr schemas to handle all client side data management.

Benefits

export default entitiyQuery(props => `
    query {
        characters {
            name
            age
            race
        }
    }
`)(({characters}) => {
    return <ul>{characters.map(({name, age, race}, key) => {
        return <li key={key}>
            <div>{name}</div>
            <div>{age}</div>
            <div>{race}</div>
        </li>;
    })}</ul>
})

Install

npm install --save redux-blueflag

Index

  • Creators
    • createEntityReducer
    • createEntityProvider????
    • createEntityQuery
    • createRequestAction
    • createRequestActionSet
    • createSchema ??
  • Selectors
    • selectEntity
    • selectEntityByPath
    • selectRequestState
  • Hocks (Higher Order Components)
    • PropChangeHock
    • LocalStateHock
  • Misc
    • connectWithQuery
    • logRequestActionNames

Modules

export default combineReducers({ entity: createEntityReducer({ GRAPHQL_RECEIVE: EntitySchema, MY_CUSTOM_ACTION_RECEIVE: EntitySchema.myCustomActionSceham }), });

Functions

createEntityReducer

Returns a reducer that normalizes data based on the normalizr schemas provided. When an action is fired, if the type matches one provied in schemaMap the payload is normalized based off the given schema. Takes a map of schemas where each key is an action name and value is a schema. must have at least one key called mainSchema returns a reducer that holds the main entity state.

import {createEntityReducer} from 'redux-blueflag';
import EntitySchema from 'myapp/EntitySchema';

export default combineReducers({
    entity: createEntityReducer({
        GRAPHQL_RECEIVE: EntitySchema,
        MY_CUSTOM_ACTION_RECEIVE: EntitySchema.myCustomActionSceham
    }),
});
ParamTypeDescription
schemaMapobjectMap of schema action names.
constructorfunctionconstructor function to edit payload data before it is normalized.

LocalStateHock ⇒ function

LocalStateHock(reducer: function(state, action)) => function(component: Component) Wraps a component with a tiny implementation of the redux concept. Takes a reducer and returns a function ready to call with a component. The hock gives the component props.localDispatchwhich triggers the reducer. the return state of the reducer is then destructured on to the components as props.

Returns: function - componentCreator function to pass react component

ParamTypeDescription
reducerfunctiona function that acts as a local reducer

PropChangeHock ⇒ function

PropChangeHock(propKeys: [String], sideEffect: function) => (component: Component) => Component

The prop change hock takes a side effect and an array of prop keys paths. The component then listens for component mount and receive props. If any of the provided props change the side effect is triggered.

Returns: function - componentCreator function to pass react component

ParamTypeDescription
propKeysArraylist of strings of prop keys
outputFunctionfunction

RequestStateReducer : reducer

Keeps fetching and error state in a global redux property "async", which is an immutable.js Map It tracks state on actions ending with _FETCH, _ERROR or _RECEIVE Variables are uppercase snakes and match the consts for fetch and error XXX_FETCH is a boolean indicating if that action is currently requesting info (or undefined before any actions have been dispatched) XXX_ERROR is either { status, message } if an error has occured, or is null otherwise ^ really only useful for ensuring that a complete list of objects has been received when using ordered maps for collections. You won't know whether your list is complete or partial without this e.g. state.async.LEARNING_PLAN_FETCH

This listens to all actions and tracks the fetching and error states of all in a generic way. Async state data is kept under the async key in redux. Fetching state is kept in state.async.<FETCH_ACTION> and will either be true if the action is currently fetching or a falsey value otherwise. <FETCH_ACTION> refers to the name (string) of the fetch action, such as USER_GET_FETCH. Error state is kept in state.async.<ERROR_ACTION> and will either be an error like {status: <status code>, message: <message>}, or null otherwise. <ERROR_ACTION> refers to the name (string) of the error action, such as USER_GET_ERROR. Actions follow a strict naming convention, each ending in either _FETCH, _RECEIVE or _ERROR. This allows the AsyncStateReducer to listen to the various actions and keep track of async state.

entityQuery(sideEffect) ⇒ function

Takes an action creator and gives it a resultKey. wraps it in PropChangeHock, entitySelect and requestStateSelect

Kind: global function
Returns: function - action creator

ParamType
sideEffectfunction

logRequestActionNames(actionMap, prefix)

Given the return value of creatRequestActionSet it will log the names of the created action types and creators

Kind: global function

ParamTypeDescription
actionMapobjectmap of actions
prefixstringString to prefix actions types with

createRequestActionSet(actionMap) ⇒ array

returns a redux-thunk action creator that will dispatch the three states of our request action. dispatch fetchAction call sideEffect then dispatch recieveAction catch dispatch errorAction

Kind: global function
Returns: array - list of action creators and action types

ParamTypeDescription
actionMapobjectdeep object representation of api functions

selectEntity(state, resultKey, schemaKey) ⇒ object

The primary means of accessing entity state. Given a requestKey it will return the denormalized state object.

Kind: global function
Returns: object - entity map

ParamTypeDefault
stateobject
resultKeystring
schemaKeystring"mainSchema"

selectEntityByPath(state, path, schemaKey) ⇒ object

Given a path to entity state it will return the denormalized state. This function is only used when you are certain you need the exact id in entity state. Most often the request key is more appropriate.

Kind: global function
Returns: object - entity map

ParamTypeDefault
stateobject
patharray
schemaKeystring"mainSchema"

selectRequestState(state, actions) ⇒ object

Returns the state of a current request. Either fetching, error or not yet requested.

Kind: global function
Returns: object - the curerent request state

ParamTypeDescription
stateobjectthe current state
actionsstring | arrayeither one or many partial action types
0.18.1

7 years ago

0.18.0

7 years ago

0.17.0

7 years ago

0.16.0

7 years ago

0.15.2

7 years ago

0.15.1

7 years ago

0.15.0

7 years ago

0.14.0

7 years ago

0.13.3

7 years ago

0.13.2

7 years ago

0.13.1

7 years ago

0.13.0

7 years ago

0.12.0

7 years ago

0.11.3

7 years ago

0.11.2

7 years ago

0.11.1

7 years ago

0.11.0

7 years ago

0.10.1

8 years ago

0.10.0

8 years ago

0.9.1

8 years ago

0.9.0

8 years ago

0.8.1

8 years ago

0.8.0

8 years ago

0.7.6

8 years ago

0.7.5

8 years ago

0.7.4

8 years ago

0.7.3

8 years ago

0.7.2

8 years ago

0.7.1

8 years ago

0.7.0

8 years ago

0.6.0

8 years ago

0.5.2

8 years ago

0.5.1

8 years ago

0.5.0

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago