myreduxtypes
v2.0.0
Published
get dinamic types and actions for redux
Downloads
5
Maintainers
Readme
myreduxtypes
myreduxtypes is a lightweight package which allows you to autogenerate redux types
Features!
- Generate types for CRUD operation /CREATE/REMOVE/FIND/GET/PATCH and plus SET/LOADING
- Generate customised types for some specific needs
Usage
myreduxtypes requires Node.js v4+ to run.
Install the dependencies and devDependencies and start the server.
$ cd my-app
$ npx create-react-app .
$ npm i redux react-redux --save
$ npm i myreduxtypes --save
$ npm start
In Redux Module
First you need to import package\
For common js
const { autoTypeGen, } = require('myreduxtypes');
For ES6 and higher
import {autoTypeGen, customTypeGen,simpleTypeGen} from 'myreduxtypes'
For autoTypeGen
export const [types, actions] = autoTypeGen('product')
And with only this line of code you are having CRUD operation ready types and actions
Types
{
CREATE_START: 'CREATE_PRODUCT_START',
CREATE_SUCCESS: 'CREATE_PRODUCT_SUCCESS',
CREATE_FAILED: 'CREATE_PRODUCT_FAILED',
LOADING: 'PRODUCT_LOADING',
SET_CURRENT: 'PRODUCT_SET_CURRENT',
PATCH_START: 'PATCH_PRODUCT_START',
PATCH_SUCCESS: 'PATCH_PRODUCT_SUCCESS',
PATCH_FAILED: 'PATCH_PRODUCT_FAILED',
REMOVE_START: 'REMOVE_PRODUCT_START',
REMOVE_SUCCESS: 'REMOVE_PRODUCT_SUCCESS',
REMOVE_FAILED: 'REMOVE_PRODUCT_FAILED',
GET_START: 'GET_PRODUCT_START',
GET_SUCCESS: 'GET_PRODUCT_SUCCESS',
GET_FAILED: 'GET_PRODUCT_FAILED',
FIND_START: 'FIND_PRODUCT_START',
FIND_SUCCESS: 'FIND_PRODUCT_SUCCESS',
FIND_FAILED: 'FIND_PRODUCT_FAILED'
}
Actions
{
create_start: [Function (anonymous)],
create_success: [Function (anonymous)],
create_failed: [Function (anonymous)],
loading: [Function (anonymous)],
set_current: [Function (anonymous)],
patch_start: [Function (anonymous)],
patch_success: [Function (anonymous)],
patch_failed: [Function (anonymous)],
remove_start: [Function (anonymous)],
remove_success: [Function (anonymous)],
remove_failed: [Function (anonymous)],
get_start: [Function (anonymous)],
get_success: [Function (anonymous)],
get_failed: [Function (anonymous)],
find_start: [Function (anonymous)],
find_success: [Function (anonymous)],
find_failed: [Function (anonymous)]
}
Each action function accepts two arguments optionally /data and route
actions.create_start(data,'/')
output====>
{ type: 'CREATE_PRODUCT_START', payload: data, route: '/' }
Custom types with myreduxtypes
import { autoTypeGen} from 'myreduxtypes'
export const [types,actions]=autoTypeGen("user","login","register");
You will get types and actions for each argument provided to autoTypeGen
{
LOGIN_START: 'LOGIN_USER_START',
LOGIN_SUCCESS: 'LOGIN_USER_SUCCESS',
LOGIN_FAILED: 'LOGIN_USER_FAILED',
LOADING: 'USER_LOADING',
REGISTER_START: 'REGISTER_USER_START',
REGISTER_SUCCESS: 'REGISTER_USER_SUCCESS',
REGISTER_FAILED: 'REGISTER_USER_FAILED'
}
More
To have both CRUD and optional types you need to provide boolien argument a the end by default its false
import { autoTypeGen} from 'myreduxtypes'
export const [types,actions]=autoTypeGen("user","login","register",true);
Package will generate
{
CREATE_START: 'CREATE_USER_START',
CREATE_SUCCESS: 'CREATE_USER_SUCCESS',
CREATE_FAILED: 'CREATE_USER_FAILED',
LOADING: 'USER_LOADING',
SET_CURRENT: 'USER_SET_CURRENT',
PATCH_START: 'PATCH_USER_START',
PATCH_SUCCESS: 'PATCH_USER_SUCCESS',
PATCH_FAILED: 'PATCH_USER_FAILED',
REMOVE_START: 'REMOVE_USER_START',
REMOVE_SUCCESS: 'REMOVE_USER_SUCCESS',
REMOVE_FAILED: 'REMOVE_USER_FAILED',
GET_START: 'GET_USER_START',
GET_SUCCESS: 'GET_USER_SUCCESS',
GET_FAILED: 'GET_USER_FAILED',
FIND_START: 'FIND_USER_START',
FIND_SUCCESS: 'FIND_USER_SUCCESS',
FIND_FAILED: 'FIND_USER_FAILED',
LOGIN_START: 'LOGIN_USER_START',
LOGIN_SUCCESS: 'LOGIN_USER_SUCCESS',
LOGIN_FAILED: 'LOGIN_USER_FAILED',
REGISTER_START: 'REGISTER_USER_START',
REGISTER_SUCCESS: 'REGISTER_USER_SUCCESS',
REGISTER_FAILED: 'REGISTER_USER_FAILED'
}
For Custom types
const { customTypeGen } = require('myreduxtypes');
export const [types,actions]=autoTypeGen("modal","open","close",'toggle');
- Types
{
MODAL_OPEN: 'MODAL_OPEN',
MODAL_CLOSE: 'MODAL_CLOSE',
MODAL_TOGGLE: 'MODAL_TOGGLE'
}
- Actions
{
modal_open: [Function (anonymous)],
modal_close: [Function (anonymous)],
modal_toggle: [Function (anonymous)]
}.
- You cann pass arguments optionally
actions.modal_open()
==================================
{ type: 'MODAL_OPEN', payload: undefined, route: undefined }