Advanced

Override in Rule

This example will show an example of overriding in specific rule.

Code

// Imports
import { Rule, Generator } from 'redux-autoreducers';
const testRule = new Rule('TEST');
const openRule = new Rule('OPEN');
openRule
    .overrideInitialState(() => ({ isOpen: false }))
    .overrideReducer(() => ({
        OPEN: state => ({
            ...state,
            isOpen: true,
        }),
    }));
const closeRule = new Rule('CLOSE');
closeRule
    .overrideInitialState(() => ({}))
    .overrideReducer(() => ({
        CLOSE: state => ({
            ...state,
            isOpen: false,
        }),
    }));

const generator = new Generator([testRule, openRule, closeRule]);
const reducer = generator.generate();

export default reducer;

Result

The code before is equivalent to:

// Initial State
const initialState = {
    isTestPending: false,
    testData: null,
    testError: null,
    isOpen: false,
};

// Define reducer
function reducer(state = initialState, action) {
    switch (action.type) {
        case 'TEST_PENDING':
            return {
                ...state,
                isTestPending: true,
                testData: null,
                testError: null,
            };
        case 'TEST_REJECTED':
            return {
                ...state,
                isTestPending: false,
                testError: action.error,
            };
        case 'TEST_FULFILLED':
            return {
                ...state,
                isTestPending: false,
                testData: action.data,
            };
        case 'OPEN':
            return {
                ...state,
                isOpen: true,
            };
        case 'CLOSE':
            return {
                ...state,
                isOpen: false,
            };
        default:
            return state;
    }
}

// Exports
export default reducer;

Override in defaults

This example will show an example of overriding defaults values.

Code

import { Rule, Generator, overrideActionTransformer } from 'redux-autoreducers';

overrideActionTransformer((action) => {
    if (!action.payload) {
        return action;
    }

    const { type, payload: { error, data } } = action;
    return {
        type,
        error,
        data,
    };
})

const rule = new Rule('GET_ALL');
const generator = new Generator([rule]);
const reducer = generator.generate();
export default reducer;

Result

The code before is equivalent to:

const initialState = {
    isGetAllPending: true,
    getAllData: null,
    getAllError: null,
};

function reducer(state = initialState, action) {
    switch (action.type) {
        case 'GET_ALL_PENDING':
            return {
                ...state,
                isGetAllPending: true,
                getAllData: null,
                getAllError: null,
            };
        case 'GET_ALL_REJECTED':
            return {
                ...state,
                isGetAllPending: false,
                getAllError: action.payload.error,
            };
        case 'GET_ALL_FULFILLED':
            return {
                ...state,
                isGetAllPending: false,
                getAllData: action.payload.data,
            };
        default:
            return state;
    }
}

export default reducer;

results matching ""

    No results matching ""