redux-odyssey v3.0.1
Redux Odyssey
Installation
NPM
npm install --save redux-odysseyYarn
yarn add redux-odysseyUsage
Include the following in your call to combineReducers/1:
import { reducers as odysseyReducers } from 'redux-odyssey'
const reducer = combineReducers({
// snip
...odysseyReducers
})You can then use the Resource.createResource/1 function to create action creators
and selectors:
import { createEntityHelpers } from 'redux-odyssey'
export const User = Symbol('User')
export type User = {
id: string,
email: string,
firstName: string,
lastName: string
}
export const {
// Action creators
setEntities: setUsers,
setEntity: setUser,
unsetEntities: unsetUsers,
unsetEntity: unsetUser
// Selectors
getEntities: getUsers,
getEntitiesSafe: getUsersSafe,
getEntity: getUser,
hasEntity: hasUser
} = Resource.createResource<User>(User)The exported function will now allow one to manage resources.
API
function createResource<T = {}> (type: string | symbol): {
setEntities: (entities: { [id: string]: TEntity }): Action,
setEntity: (id: string, entity: TEntity): Action,
unsetEntities: (ids: string[]): Action,
unsetEntity: (id: string): Action
getEntities: (ids: string[]): TEntity[],
getEntitiesSafe: (ids: string[]): TEntity[],
getEntity: (id: string): TEntity | null,
hasEntity: (id: string): boolean
}The createResource/1 function creates partially applied action creators and selectors to read and modify the resources stored in the state.
Helpers
Actions
function setResources (resources: { [id: string]: T }): ActionAdds or updates all the resources in the given object by their keys, which represents their ids.
function setResource (resourceId: string, resource: T): ActionAdds or updates the given resource according to its id.
function unsetResources (resourceIds: string[]): ActionRemoves the resources with the given ids from the state.
function unsetResource (resourceId: string): ActionRemoves the resource with the given id.
Selectors
function getResources (resourceIds: string[]): T[]Returns all the resources with the given ids. If an resource does not exist, this function will throw an error.
function getResourcesSafe(resourceIds: string[]): T[]Returns all the resources with the given ids. If a resource does not exist, it will be excluded from the resulting array.
function getResource (resourceId: string): T | nullReturns a resource by its id. If the resource does not exist, null is returned.
function hasResource (id: string): booleanIndicates if the resource with the given id exists.