2.1.11 • Published 1 year ago

@budarin/redux-business-logic-middleare v2.1.11

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

redux-business-logic-middleare

Middleware for processing business login in redux application.

Instllation

Using npm

npm install --save-dev @budarin/redux-business-logic-middleare

Using yarn

yarn add -D @budarin/redux-business-logic-middleare

Using:

Let's implement a simple business rule for Todo: “when creating a todo, send the todo to the server and to the redux store”.

Let's describe the essence of Todo — its constants, actions, business rules and a redeser in accordance with the concept of ducks.

./ducks/todo.js

import { sendTodo } from 'src/services/api'
import { onAction } from'@budarin/redux-business-logic-middleare';

const SET_ERROR = 'TODO/SET_ERROR';
export const ADD_TODO = 'TODO/ADD_TODO';

export const setError = (error) => ({
    type: SET_ERROR,
    payload: error
})

export const addTodo = ( todo ) => ({
    type: ADD_TODO,
    payload: todo 
});

// our business rule
export const addTodoMiddleware = async (store, next, action) => {
    // call the API method to send todo to the server
    try {
        await sendTodo(action.payload);
    } catch(error) {
        return store.dispatch(setError(error));
    }

     // otherwise, we pass the action to the next middleware
    return next(action);
}

// let's add the business rule
onAction(ADD_TODO, addTodoMiddleware)


export default const reducer = (state = initialState, action) => {
    switch (action.type) {
        case ADD_TODO: {
            ...
        }
        case SET_ERROR {
            ...
        }
        default:
            return state;
    }
};

Add midleware to stores middlewares

import todoReducer from '../ducks/todo.js'
import { createStore, applyMiddleware, combineReducers } from 'redux'
import { bussinesLogicMiddleware } from '@budarin/redux-business-logic-middleare';

const reducers = combineReducers(
    ...
    todoReducer,
    ...
);

const store = createStore(
    reducers, 
    initialState, 
    applyMiddleware(bussinesLogicMiddleware)
);

To remove bussines-rule from processing

import { ADD_TODO } from '../ducks/todo.js'
import { offAction } from '@budarin/redux-business-logic-middleare';

offAction(ADD_TODO);

To remove all bussines-rules from processing

import { removeAllBusinessRules } from '@budarin/redux-business-logic-middleare';

removeAllBusinessRules();
1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

2.1.2

1 year ago

2.0.3

1 year ago

2.1.1

1 year ago

2.0.2

1 year ago

2.0.5

1 year ago

2.1.3

1 year ago

2.0.4

1 year ago

2.1.6

1 year ago

2.1.5

1 year ago

2.1.8

1 year ago

2.1.7

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

2.1.0

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

2.1.9

1 year ago

2.1.11

1 year ago

1.0.15

1 year ago

1.0.13

1 year ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.9

3 years ago

1.0.10

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago