Home Reference Source

application/redux/reducers/workflow-section-reducers.js

import _ from 'lodash';
import Immutable from 'immutable';
import {
    WORKFLOW_SECTIONS_REQUEST,
    WORKFLOW_SECTIONS_RECEIVE,
    WORKFLOW_SECTION_RECEIVE,
    WORKFLOW_SECTIONS_INVALIDATE,
    WORKFLOW_SECTION_SELECTED,
    WORKFLOW_SECTIONS_FILTER_WEBONLY,
    WORKFLOW_SECTIONS_FILTER_PRINTONLY
} from './../actions/workflow-section-actions';

export function workflowSections(state = null, action) {
    if (state === null) {
        state = {
            isFetching: false,
            didInvalidate: false,
            items: Immutable.fromJS({}),
            selected: false,
            order: "name",
            dir: "asc",
            filters: {
                webOnly: Immutable.fromJS({}),
                printOnly: Immutable.fromJS({})
            }
        };
    }

    let newState;

    switch (action.type) {
        case WORKFLOW_SECTIONS_FILTER_WEBONLY:
            // little expanation, filter -> sort -> map:
            let _wo_filtered = state.items.filter((item) => {
                // we only want web workflowSections
                return parseInt(item.get('is_web')) === 1;
            }).sort((a, b) => {
                // and we sort by priority
                if (parseInt(a.get('priority')) > parseInt(b.get('priority'))) {
                    return 1;
                } else if (parseInt(a.get('priority')) < parseInt(b.get('priority'))) {
                    return -1;
                }
                return 0;
            }).map((item) => {
                /*
                    then only return the UUID
                    we'll use that to fetch the workflow
                    from the workflows.items iterable
                    check content-item.js:533 (line starts with {this.props.workflows.filters.webOnly.map((id, i) => {)
                    for info on how this works in the real world
                */
                return item.get('uuid');
            });

            return Object.assign({}, state, {
               filters: Object.assign({}, state.filters, {
                   webOnly: _wo_filtered
               })
            });
        case WORKFLOW_SECTIONS_FILTER_PRINTONLY:
            let _po_filtered = state.items.filter((item) => {
                return parseInt(item.get('is_print')) === 1;
            }).sort((a, b) => {
                if (parseInt(a.get('priority')) > parseInt(b.get('priority'))) {
                    return 1;
                } else if (parseInt(a.get('priority')) < parseInt(b.get('priority'))) {
                    return -1;
                }
                return 0;
            }).map((item) => {
                return item.get('uuid');
            });

            return Object.assign({}, state, {
               filters: Object.assign({}, state.filters, {
                   printOnly: _po_filtered
               })
            });
        case WORKFLOW_SECTIONS_INVALIDATE:
            return Object.assign({}, state, {
                didInvalidate: true,
            });
        case WORKFLOW_SECTIONS_REQUEST:
            return Object.assign({}, state, {
                isFetching: true,
                didInvalidate: false
            });
        case WORKFLOW_SECTION_SELECTED: {
            return Object.assign({}, state, {
                selected: action.payload.selected
            });
        }
        case WORKFLOW_SECTION_RECEIVE:
            const workflowSection = action.payload.workflowSection;
            const workflowSections = state.items.map((item, i) => {
                if (item.get('uuid') == workflowSection.get('uuid')) {
                    return workflowSection;
                }
                return item;
            });

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

    return state;
};