application/redux/reducers/workflow-reducers.js
import _ from 'lodash';
import Immutable from 'immutable';
import {
WORKFLOWS_REQUEST,
WORKFLOWS_RECEIVE,
WORKFLOW_RECEIVE,
WORKFLOWS_INVALIDATE,
WORKFLOW_SELECTED,
WORKFLOWS_FILTER_WEBONLY,
WORKFLOWS_FILTER_PRINTONLY,
WORKFLOWS_FILTER_CONTENTONLY,
WORKFLOWS_FILTER_ASSIGNMENTONLY
} from './../actions/workflow-actions';
export function workflows(state = null, action) {
if (state === null) {
state = {
isFetching: false,
didInvalidate: false,
items: Immutable.fromJS({}),
selected: false,
order: "name",
dir: "asc",
filters: {
contentOnly: Immutable.fromJS({}),
assignmentOnly: Immutable.fromJS({}),
webOnly: Immutable.fromJS({}),
printOnly: Immutable.fromJS({})
}
};
}
let newState;
switch (action.type) {
case WORKFLOWS_FILTER_CONTENTONLY:
// little expanation, filter -> sort -> map:
let _co_filtered = state.items.filter((item) => {
// we only want web workflows
return (parseInt(item.get('is_content')) === 1 && 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, {
contentOnly: _co_filtered
})
});
case WORKFLOWS_FILTER_ASSIGNMENTONLY:
// little expanation, filter -> sort -> map:
let _ao_filtered = state.items.filter((item) => {
// we only want web workflows
return (parseInt(item.get('is_assignment')) === 1 && 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, {
assignmentOnly: _ao_filtered
})
});
case WORKFLOWS_FILTER_WEBONLY:
// little expanation, filter -> sort -> map:
let _wo_filtered = state.items.filter((item) => {
// we only want web workflows
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 WORKFLOWS_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 WORKFLOWS_INVALIDATE:
return Object.assign({}, state, {
didInvalidate: true,
});
case WORKFLOWS_REQUEST:
return Object.assign({}, state, {
isFetching: true,
didInvalidate: false
});
case WORKFLOW_SELECTED: {
return Object.assign({}, state, {
selected: action.payload.selected
});
}
case WORKFLOW_RECEIVE:
const workflow = action.payload.workflow;
const workflows = state.items.map((item, i) => {
if (item.get('uuid') == workflow.get('uuid')) {
return workflow;
}
return item;
});
action.payload.workflows = Immutable.fromJS(workflows);
case WORKFLOWS_RECEIVE:
return Object.assign({}, state, {
isFetching: false,
didInvalidate: false,
items: action.payload.workflows
});
}
return state;
};