0.1.6 • Published 7 years ago

magic-redux-generator v0.1.6

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

magic-redux-generator

Simple component for generate redux actions/reducer

Component in development. I accept suggestions.

Installation

For install: npm install magic-redux-generator --save

Instance methods

fetchItem

Prop nameTypeDescriptionExample
urlExtensionStringUrl sufix"/id"
queryParamsObjectQuery url params{limit:15}
responseTypeStringWS response sufix"users"
preDispatchFunctionFunction to execute before do dispatch() => {}
postDispatchFunctionFunction to execute after do dispatch() => {}

fetch

Prop nameTypeDescriptionExample
urlExtensionStringUrl sufix"/5"
queryParamsObjectQuery url params{limit:15}
responseTypeStringWS response sufix"users"
responseTotalStringWS response total sufix"total"
preDispatchFunctionFunction to execute before do dispatch() => {}
postDispatchFunctionFunction to execute after do dispatch() => {}

create

Prop nameTypeDescriptionExample
dataObjectObject with create data{username:example, password:1234}
urlExtensionStringUrl sufix"/users"
queryParamsObjectQuery url params{limit:15}
postDispatchFunctionFunction to execute after do dispatch() => {}

update

Prop nameTypeDescriptionExample
dataObjectObject with update data{id:3, username:example, password:1234}
urlExtensionStringUrl sufix"/3"
queryParamsObjectQuery url params{limit:15}
postDispatchFunctionFunction to execute after do dispatch() => {}

delete

Prop nameTypeDescriptionExample
urlExtensionStringUrl sufix"/3"
queryParamsObjectQuery url params{limit:15}
postDispatchFunctionFunction to execute after do dispatch() => {}

deleteRows

Prop nameTypeDescriptionExample
urlExtensionStringUrl sufix"/3"
queryParamsObjectQuery url params{limit:15}
postDispatchItemFunctionFunction to execute after delete each item() => {}
postDispatchAllFunctionFunction to execute after delete all items() => {}

More instance methods

Instance methodDescriptionExample
setListSet the reducer list valuedispatch(actions. setList([]))
setItemSet the reducer item valuedispatch(actions.setItem({}))
setTotalSet the reducer total valuedispatch(actions.setTotal(32))
setFetchingSet the reducer fetching valuedispatch(actions.setFetching(true))

Project Example

You can check this component with this project example for react-native.

git clone https://github.com/luisfuertes/testMagicReduxGenerator
cd projectDir && npm install
react-native run-ios

Basic usage

Import: import reduxGenerator from 'magic-redux-generator'

Important: Set Authorization token (only on init): reduxGenerator.webservices.configureToken(yourToken)

Actions.js basic example:

import reduxGenerator from 'magic-redux-generator'

let baseUri = mainUrl + '/users'
let actions = reduxGenerator.createActions(baseUri, 'users')
let types = reduxGenerator.createTypes('users')

//Add custom actions
export default {
  ...actions,

  types,

  yourCustomAction: () => {
    return (dispatch, getState) => {
      ...
      let item = extraData
      dispatch(actions.setItem(item))
    }
  },
}

Reducer.js example:

import reduxGenerator from 'magic-redux-generator'

let userReducer = createReducer('users')
export default userReducer.reducer

Usage example:

Dispatch action:

import actions from './Actions.js'

dispatch(actions.fetch({offset: 0, limit: 10}, 'users'))

And in mapStateToProps:

list: state.users.list,

Error reducer example:

import reduxGenerator from 'magic-redux-generator'
const users = reduxGenerator.createTypes('users')//One per action/reducer generate with reduxGenerator

const initialState = {
  type: null,
  error: null,
};

export default function errorReducer(state = initialState, action = {}) {
  switch (action.type) {

    case users.ACTIONS_ERROR:
      return {
        ...state,
        type: users.ACTIONS_ERROR,
        error: action.error,
      };

    case 'REMOVE_ERROR':
      return {
        ...state,
        error: null,
        type: null,
      };

    default:
      return state;
  }
}
0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.0

7 years ago