Home Reference Source

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

import BaseService from './../../services/base-service';

class WorkflowSectionService extends BaseService {

    fetch(opts) {
        return this.get('/v3/workflow-section', opts);
    }

    fetchOne(id) {
        return this.get('/v3/workflow-section/' + id);
    }

    create(data) {
        return this.post('/v3/workflow-section', data);
    }

    update(id, data) {
        return this.put('/v3/workflow-section/' + id, data);
    }

    remove(id) {
        return this.delete('/v3/workflow-section/' + id);
    }

    priority(uuids) {
        return this.put('/v3/workflow-section/priority', uuids);
    }
}

const service = new WorkflowSectionService;

/**
 * Action types
 */
export const WORKFLOW_SECTIONS_REQUEST = 'WORKFLOW_SECTIONS_REQUEST';
export const WORKFLOW_SECTIONS_RECEIVE = 'WORKFLOW_SECTIONS_RECEIVE';
export const WORKFLOW_SECTION_RECEIVE = 'WORKFLOW_SECTION_RECEIVE';
export const WORKFLOW_SECTION_SELECTED = 'WORKFLOW_SECTION_SELECTED';
export const WORKFLOW_SECTIONS_INVALIDATE = 'WORKFLOW_SECTIONS_INVALIDATE';
export const WORKFLOW_SECTIONS_FILTER_WEBONLY = 'WORKFLOW_SECTIONS_FILTER_WEBONLY';
export const WORKFLOW_SECTIONS_FILTER_PRINTONLY = 'WORKFLOW_SECTIONS_FILTER_PRINTONLY';

/**
 * Action creators
 */
export function workflowSectionsRequest(params = {}) {
    return {
        type: WORKFLOW_SECTIONS_REQUEST,
        payload: {
            params: params
        }
    };
}

export function workflowSectionsReceive(items) {

    return {
        type: WORKFLOW_SECTIONS_RECEIVE,
        payload: {
            workflowSections: items ? items : []
        }
    };
}

export function workflowSectionReceive(item) {
    return {
        type: WORKFLOW_SECTION_RECEIVE,
        payload: {
            workflowSection: item ? item : {}
        }
    };
}

export function workflowSectionSetSelected(workflowSection) {
    return {
        type: WORKFLOW_SECTION_SELECTED,
        payload: {
            selected: workflowSection ? workflowSection : false
        }
    };
}

export function workflowSectionsInvalidate() {
    return {
        type: WORKFLOW_SECTIONS_INVALIDATE,
        payload: {
        }
    };
}

export function workflowSectionsFilterWebOnly() {
    return {
        type: WORKFLOW_SECTIONS_FILTER_WEBONLY,
        payload: {
        }
    }
}
export function workflowSectionsFilterPrintOnly() {
    return {
        type: WORKFLOW_SECTIONS_FILTER_PRINTONLY,
        payload: {
        }
    }
}

export function workflowSectionsShouldFetch(state) {
    const workflowSections = state.workflowSections;
    if (!workflowSections.items.length) {
        return true;
    } else if (workflowSections.isFetching) {
        return false;
    }

    return workflowSections.didInvalidate;
}

export function workflowSectionsMaybeFetch(params = {}) {
    return function(dispatch, getState) {
        const workflowSections = getState().workflowSections;

        if (!workflowSections.items || !workflowSections.items.size) {
            return dispatch(workflowSectionsFetch(params));
        }

        return Promise.resolve();
    }
};

export function workflowSectionsFetch(params = {}) {
    return function(dispatch, getState) {
        dispatch(workflowSectionsRequest(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(workflowSectionsReceive(response.get('items')));
            });
    };
}

export function workflowSectionsFetchOne(id) {
    return function (dispatch, getState) {
        dispatch(workflowSectionsRequest({}));

        return service.fetchOne(id)
            .then(response => {
                return dispatch(workflowSectionReceive(response.first()));
            });
    };
}

export function workflowSectionCreate(workflowSection = {}) {
    return function(dispatch, getState) {
        return service.create(workflowSection)
            .then(response => {
                return dispatch(workflowSectionReceive(response.first()));
            });
    }
}

export function workflowSectionUpdate(id, workflowSection = {}) {
    return function(dispatch, getState) {
        return service.update(id, workflowSection)
            .then(response => {
                return dispatch(workflowSectionReceive(response.first()));
            });
    }
}

export function workflowSectionsRemove(uuids) {
    return function(dispatch, getState) {
        dispatch(workflowSectionsInvalidate());

        return service.remove(uuids);
    }
}

export function workflowSectionsSetPriority(uuids) {
    return function(dispatch, getState) {
        dispatch(workflowSectionsInvalidate());

        return service.priority(uuids)
            .then(response => {
                return dispatch(workflowSectionsReceive(response.get('items')));
            })
    }
}