1.1.1 • Published 7 years ago

redux-enjoy-helpers v1.1.1

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

redux-enjoy-helpers

Helpers for ejoying Redux

Install

npm i --save-dev redux-enjoy-helpers

Use

Types

import { createTypes } from 'redux-enjoy-helpers/type';
const PREFIX = '@@user/current/';
export const TYPES = createTypes(PREFIX, [
  'SET', 'RESET', 'SAVE', 'TOOGLE_INVISIBLE'
]);
export const actions = {
  set(state, reset = false) {
    return { type: TYPES.SET, state, reset }
  },
  reset() {
    return { type: TYPES.RESET }
  },
  save() {
    return { type: TYPES.SAVE }
  },
  toogleInvisible() {
    return { type: TYPES.TOOGLE_INVISIBLE }
  }
}

Reducers

import { createReducer } from 'redux-enjoy-helpers/reducer';
import { TYPES } from '../actions/user/current';

const initialState = {
  firstName: '',
  lastName: '',
  login: '',
  invisible: false
}

export default createReducer({
  [TYPES.TOOGLE_INVISIBLE](state, action) {
    return {
      ...state,
      invisible: !state.invisible
    }
  }
}, initialState, null, { set: TYPES.SET, reset: TYPES.RESET });
// set automatic add set-reducer
// ...
// (state, action) => {
//   if (action.reset) {
//      return { ...initialState, ...action.state }
//    }
//    return { ...state, ...action.state }
// }
// reset — add reset reducer, (state, action) => initialState

in action:

import {actions as userActions} from 'actions/user';
// ...
dispatch(userActions.set({ firstName: 'Kostya' }));
//set firstName at user.current to Kostya
dispatch(userActions.toogleInvisible()));
//toogle invisible flag at user.current

More docs at docs page

MIT License