application/redux/actions/workflow-actions.js
import BaseService from './../../services/base-service';
class WorkflowService extends BaseService {
fetch(opts) {
return this.get('/v3/workflow', opts);
}
fetchOne(id) {
return this.get('/v3/workflow/' + id);
}
create(data) {
return this.post('/v3/workflow', data);
}
update(id, data) {
return this.put('/v3/workflow/' + id, data);
}
remove(id) {
return this.delete('/v3/workflow/' + id);
}
priority(uuids) {
return this.put('/v3/workflow/priority', uuids);
}
}
const service = new WorkflowService;
/**
* Action types
*/
export const WORKFLOWS_REQUEST = 'WORKFLOWS_REQUEST';
export const WORKFLOWS_RECEIVE = 'WORKFLOWS_RECEIVE';
export const WORKFLOW_RECEIVE = 'WORKFLOW_RECEIVE';
export const WORKFLOW_SELECTED = 'WORKFLOW_SELECTED';
export const WORKFLOWS_INVALIDATE = 'WORKFLOWS_INVALIDATE';
export const WORKFLOWS_FILTER_WEBONLY = 'WORKFLOWS_FILTER_WEBONLY';
export const WORKFLOWS_FILTER_PRINTONLY = 'WORKFLOWS_FILTER_PRINTONLY';
export const WORKFLOWS_FILTER_CONTENTONLY = 'WORKFLOWS_FILTER_CONTENTONLY';
export const WORKFLOWS_FILTER_ASSIGNMENTONLY = 'WORKFLOWS_FILTER_ASSIGNMENTONLY';
/**
* Action creators
*/
export function workflowsRequest(params = {}) {
return {
type: WORKFLOWS_REQUEST,
payload: {
params: params
}
};
}
export function workflowsReceive(items) {
return {
type: WORKFLOWS_RECEIVE,
payload: {
workflows: items ? items : []
}
};
}
export function workflowReceive(item) {
return {
type: WORKFLOW_RECEIVE,
payload: {
workflow: item ? item : {}
}
};
}
export function workflowSetSelected(workflow) {
return {
type: WORKFLOW_SELECTED,
payload: {
selected: workflow ? workflow : false
}
};
}
export function workflowsInvalidate() {
return {
type: WORKFLOWS_INVALIDATE,
payload: {
}
};
}
export function workflowsFilterContentOnly() {
return {
type: WORKFLOWS_FILTER_CONTENTONLY,
payload: {
}
}
}
export function workflowsFilterAssignmentOnly() {
return {
type: WORKFLOWS_FILTER_ASSIGNMENTONLY,
payload: {
}
}
}
export function workflowsFilterWebOnly() {
return {
type: WORKFLOWS_FILTER_WEBONLY,
payload: {
}
}
}
export function workflowsFilterPrintOnly() {
return {
type: WORKFLOWS_FILTER_PRINTONLY,
payload: {
}
}
}
export function workflowsShouldFetch(state) {
const workflows = state.workflows;
if (!workflows.items.length) {
return true;
} else if (workflows.isFetching) {
return false;
}
return workflows.didInvalidate;
}
export function workflowsMaybeFetch(params = {}) {
return function(dispatch, getState) {
const workflows = getState().workflows;
if (!workflows.items || !workflows.items.size) {
return dispatch(workflowsFetch(params));
}
return Promise.resolve();
}
};
export function workflowsFetch(params = {}) {
return function(dispatch, getState) {
dispatch(workflowsRequest(params));
params.per_page = params.per_page ? params.per_page : 50;
params.order = params.order ? params.order : "name";
params.dir = params.dir ? params.dir : "asc";
return service.fetch(params)
.then(response => {
return dispatch(workflowsReceive(response.get('items')));
});
};
}
export function workflowsFetchOne(id) {
return function (dispatch, getState) {
dispatch(workflowsRequest({}));
return service.fetchOne(id)
.then(response => {
return dispatch(workflowReceive(response.first()));
});
};
}
export function workflowCreate(workflow = {}) {
return function(dispatch, getState) {
return service.create(workflow)
.then(response => {
return dispatch(workflowReceive(response.first()));
});
}
}
export function workflowUpdate(id, workflow = {}) {
return function(dispatch, getState) {
return service.update(id, workflow)
.then(response => {
return dispatch(workflowReceive(response.first()));
});
}
}
export function workflowsRemove(uuids) {
return function(dispatch, getState) {
dispatch(workflowsInvalidate());
return service.remove(uuids);
}
}
export function workflowsSetPriority(uuids) {
return function(dispatch, getState) {
dispatch(workflowsInvalidate());
return service.priority(uuids)
.then(response => {
return dispatch(workflowsReceive(response.get('items')));
})
}
}