3.0.1 • Published 6 years ago

redux-odyssey v3.0.1

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

Redux Odyssey

Build Status

Installation

NPM

npm install --save redux-odyssey

Yarn

yarn add redux-odyssey

Usage

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 }): Action

Adds or updates all the resources in the given object by their keys, which represents their ids.


function setResource (resourceId: string, resource: T): Action

Adds or updates the given resource according to its id.


function unsetResources (resourceIds: string[]): Action

Removes the resources with the given ids from the state.


function unsetResource (resourceId: string): Action

Removes 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 | null

Returns a resource by its id. If the resource does not exist, null is returned.


function hasResource (id: string): boolean

Indicates if the resource with the given id exists.

3.0.1

6 years ago

3.0.0

6 years ago

2.3.0

6 years ago

2.2.0

6 years ago

1.2.0

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago