Home Reference Source

application/redux/reducers/container-reducers.js

import _ from 'lodash';
import Immutable from 'immutable';
import {
    CONTAINERS_REQUEST,
    CONTAINERS_RECEIVE,
    CONTAINER_RECEIVE,
    CONTAINERS_INVALIDATE,
    CONTAINER_SELECTED,
    CONTAINERS_RECEIVE_PAGINATION
} from './../actions/container-actions';

const PER_PAGE = 20;

export function containers(state = null, action) {
    if (state === null) {
        state = {
            isFetching: false,
            didInvalidate: false,
            items: Immutable.fromJS({}),
            open: Immutable.fromJS({}),
            selected: false,
            order: "title",
            dir: "asc",
            page: 0,
            pagination: {
                before: 0,
                current: 0,
                first: 0,
                last: 0,
                limit: 0,
                next: 0,
                total_items: 0,
                total_pages: 0
            },
        };
    }

    switch (action.type) {
        case CONTAINERS_RECEIVE_PAGINATION:
            return Object.assign({}, state, {
                pagination: {
                    'before': action.payload.before,
                    'current': action.payload.current,
                    'first': action.payload.first,
                    'last': action.payload.last,
                    'limit': action.payload.limit,
                    'next': action.payload.next,
                    'total_items': action.payload.total_items,
                    'total_pages': action.payload.total_pages,
                }
            });
        case CONTAINERS_INVALIDATE:
            return Object.assign({}, state, {
                didInvalidate: true,
            });
        case CONTAINERS_REQUEST:
            return Object.assign({}, state, {
                isFetching: true,
                didInvalidate: false
            });
        case CONTAINER_SELECTED: {
            return Object.assign({}, state, {
                selected: action.payload.selected
            });
        }
        case CONTAINER_RECEIVE:
            const container = action.payload.container;
            let found = false;
            let containers = state.items.map((item, i) => {
                if (item.get('uuid') == container.get('uuid')) {
                    found = true;
                    return container;
                }
                return item;
            });

            if (!found) {
                let col = containers.toJS();
                if (col && col.length) {
                    col.push(container);
                } else {
                    col = [container];
                }
                containers = Immutable.fromJS(col);
            }

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

    return state;
};