Home Reference Source

application/redux/reducers/acl-reducers.js

import _ from 'lodash';
import Immutable from 'immutable';
import {
    ACLS_REQUEST,
    ACLS_RECEIVE,
    ACL_RECEIVE,
    ACLS_RESOURCES_RECEIVE,
    ACLS_INVALIDATE,
    ACL_SELECTED,
    ACLS_SET_PAGE
} from './../actions/acl-actions';

const PER_PAGE = 20;

export function acls(state = null, action) {
    if (state === null) {
        state = {
            isFetching: false,
            didInvalidate: false,
            items: Immutable.fromJS({}),
            selected: false,
            page: 0,
            order: "name",
            dir: "asc",
            filter: false,
            resources: {}
        };
    }

    let newState;

    switch (action.type) {
        case ACLS_INVALIDATE:
            return Object.assign({}, state, {
                didInvalidate: true,
            });
        case ACLS_REQUEST:
            return Object.assign({}, state, {
                isFetching: true,
                didInvalidate: false
            });
        case ACL_SELECTED: {
            return Object.assign({}, state, {
                selected: action.payload.selected
            });
        }
        case ACLS_RESOURCES_RECEIVE:
            return Object.assign({}, state, {
                resources: action.payload.resources
            });
        case ACL_RECEIVE:
            const acl = action.payload.acl;
            const acls = state.items.map((item, i) => {
                if (item.get('name') == acl.get('name')) {
                    return acl;
                }
                return item;
            });

            action.payload.acls = Immutable.fromJS(acls);
        case ACLS_RECEIVE:
            return Object.assign({}, state, {
                isFetching: false,
                didInvalidate: false,
                items: action.payload.acls
            });
    }

    return state;
};