0.0.4 • Published 7 years ago

redux-utilities v0.0.4

Weekly downloads
10
License
MIT
Repository
github
Last release
7 years ago

Redux Utilities

This is a small library with some helper functions useful for applications build with Redux.

Build Status NPM Status NPM Status NPM Status NPM Status

Create Action Types

Usage:
// model-action-types.js

import { createActionTypes } from 'redux-utilities'

export default createActionTypes(
  [
    {
      FETCH: ['START', 'SUCCESS', 'FAIL'],
      SAVE: ['START', 'SUCCESS', 'FAIL']
    },
    ['SET_DATA']
  ],
  'MODEL'
)

Will export an object:

{
  FETCH: {
    START: 'MODEL.FETCH.START',
    SUCCESS: 'MODEL.FETCH.SUCCESS',
    FAIL: 'MODEL.FETCH.FAIL'
  },
  SAVE: {
    START: 'MODEL.SAVE.START',
    SUCCESS: 'MODEL.SAVE.SUCCESS',
    FAIL: 'MODEL.SAVE.FAIL'
  },
  SET_DATA: 'MODEL.SET_DATA'
}

Create Reducer

Usage:
// model-reducer.js

import { createReducer } from 'redux-utilities'

import MODEL from './model-action-types'

const initState = {
  isLoading: false,
  isSaving: false,
  data: [],
  error: null
}

export default createReducer({
  [MODEL.FETCH.START]: state => (
    { ...state, isLoading: true }
  )
  [MODEL.FETCH.SUCCESS]: (state, { data }) => (
    { ...state, isLoading: false, data }
  )
  [MODEL.FETCH.FAIL]: state => (
    { ...state, isLoading: false, error: { message: 'Could not fetch...' } }
  )

  [MODEL.SET_DATA]: (state, { field, value }) => (
    { ...state, data: { ...state.data, [field]: value } }
  )
    
  [MODEL.SAVE.START]: state => (
    { ...state, isSaving: true }
  )
  [MODEL.SAVE.SUCCESS]: (state, { data }) => (
    { ...state, isSaving: false }
  )
  [MODEL.SAVE.FAIL]: state => (
    { ...state, isSaving: false, error: { message: 'Could not save...' } }
  )
    
  ...
    
}, initState)

Will export a reducer that can be used with Redux's combineReducer function:

import modelReducer from './model-reducer.js'

...
combineReducers({
  modelReducer,
  ...
})
...

Create Module Factory

Docs: WIP

Contributing

If you want to contribute or share your ideas, please feel free to contact me.

License

Copyright (c) 2017 Matko Bulic
Licensed under the MIT License

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago

0.0.0

7 years ago