Home Reference Source

application/redux/actions/content-actions.js

import BaseService from './../../services/base-service';
import {parseSrn} from './../../util/strings';
import Immutable from 'immutable';

import {
    contentViewNotifyNew
} from './content-view-actions';

class ContentService extends BaseService {

    fetch(opts) {
        return this.get('/v3/content', opts);
    }

    fetchOne(id) {
        return this.get('/v3/content/' + id);
    }

    create(data) {
        return this.post('/v3/content', data);
    }

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

    updateAttachment(id, data) {
        return this.put('/v3/attachment/' + id, data);
    }

    remove(id) {
        return this.delete('/v3/content/' + id);
    }

    checkout(srn) {
        return this.post('/v3/lock-request', {'srn': srn});
    }

    checkin(uuid, lockUuid) {
        return this.delete('/v3/lock-request/' + lockUuid);
    }

    clearLock(userUuid, lockUuid) {
        return this.delete('/v3/lock-request/' + lockUuid, {
            'force_override': userUuid
        });
    }

    sync(uuid) {
        return this.put('/v3/export/push/' + uuid);
    }
}

const service = new ContentService;

/**
 * Action types
 */
export const CONTENT_REQUEST = 'CONTENT_REQUEST';
export const CONTENT_RECEIVE = 'CONTENT_RECEIVE';
export const CONTENT_RECEIVE_ONE = 'CONTENT_RECEIVE_ONE';
export const CONTENT_SELECTED = 'CONTENT_SELECTED';
export const CONTENT_INVALIDATE = 'CONTENT_INVALIDATE';
export const CONTENT_REMOVE_ONE = 'CONTENT_REMOVE_ONE';
export const CONTENT_RECEIVE_PAGINATION = 'CONTENT_RECEIVE_PAGINATION';
export const CONTENT_RECEIVE_ONE_FROM_SOCKET = 'CONTENT_RECEIVE_ONE_FROM_SOCKET';

/**
 * Action creators
 */
export function contentReceivePagination(pagination) {
    return {
        type: CONTENT_RECEIVE_PAGINATION,
        payload: {
            before: pagination.get('before'),
            current: pagination.get('current'),
            first: pagination.get('first'),
            last: pagination.get('last'),
            limit: pagination.get('limit'),
            next: pagination.get('next'),
            total_items: pagination.get('total_items'),
            total_pages: pagination.get('total_pages'),
        }
    };
}
 export function contentRequest(params = {}) {
    return {
        type: CONTENT_REQUEST,
        payload: {
            params: params
        }
    };
}

export function contentReceive(items) {

    return {
        type: CONTENT_RECEIVE,
        payload: {
            content: items ? items : []
        }
    };
}

export function contentReceiveOne(item) {
    return {
        type: CONTENT_RECEIVE_ONE,
        payload: {
            content: item ? item : {}
        }
    };
}

export function contentReceiveOneFromSocket(item) {
    return {
        type: CONTENT_RECEIVE_ONE_FROM_SOCKET,
        payload: {
            content: item ? item : {}
        }
    };
}

export function contentRemoveOne(item) {
    return (dispatch) => {
        return dispatch({
            type: CONTENT_REMOVE_ONE,
            payload: {
                content: item ? item : {}
            }
        });
    }
}

export function contentSetSelected(content) {
    return {
        type: CONTENT_SELECTED,
        payload: {
            selected: content ? content : false
        }
    };
}

export function contentInvalidate() {
    return {
        type: CONTENT_INVALIDATE,
        payload: {
        }
    };
}

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

    return content.didInvalidate;
}

export function contentMaybeFetch(params = {}) {
    return function(dispatch, getState) {
        const content = getState().content;

        if (!content.items || !content.items.size) {
            return dispatch(contentFetch(params));
        }

        return Promise.resolve();
    }
};

export function contentFetch(params = {}) {
    return function(dispatch, getState) {
        dispatch(contentRequest(params));

        params.per_page = params.per_page ? params.per_page : 20;
        params.order = params.order ? params.order : "modified_at";
        params.dir = params.dir ? params.dir : "desc";

        return service.fetch(params)
            .then(response => {
                dispatch(contentReceivePagination(response));
                return dispatch(contentReceive(response.get('items')));
            });
    };
}

export function contentMaybeFetchCache() {
    return function(dispatch, getState) {
        const content = getState().content;
        if (!content.items || !content.items.size) {
            return dispatch(contentFetch({
                'order': 'modified_at',
                'dir': 'desc',
                'limit': 500,
                'per_page': 500,
                'by_type': 1
            }));
        }

        return Promise.resolve();
    }
}

export function contentFetchOne(id) {
    return function (dispatch, getState) {
        dispatch(contentRequest({}));

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

export function contentReceiveWebSocket(content = {}) {
    return function(dispatch, getState) {
        // if (window.Store.getState().contentView.filtersOn) {
            dispatch(contentViewNotifyNew(content.first()));
            return dispatch(contentReceiveOneFromSocket(content.first()));
        // } else {
        //     return dispatch(contentReceiveOne(content.first()));
        // }
    };
}

export function contentDeleteFromWebSocket(content = {}) {
    return function(dispatch, getState) {
        return dispatch(contentRemoveOne(content.first()));
    }
}

export function contentCreate(content = {}) {
    return function(dispatch, getState) {
        return service.create(content)
            .then(response => {
                dispatch(contentSetSelected(response.first().get('uuid')));
                return dispatch(contentReceiveOne(response.first()));
            });
    }
}

export function contentUpdate(id, content = {}) {
    return function(dispatch, getState) {
        return service.update(id, content)
            .then(response => {
                return dispatch(contentReceiveOne(response.first()));
            });
    }
}

export function contentAttachmentUpdate(id, data = {}) {
    return function(dispatch, getState) {
        return service.updateAttachment(id, data)
            .then(response => {
                return dispatch(contentReceiveOne(response.first()));
            });
    }
}

export function contentRemove(uuids) {
    return function(dispatch, getState) {
        dispatch(contentInvalidate());

        return service.remove(uuids);
    }
}

export function contentCheckOut(srn) {
    return function(dispatch, getState) {
        return service.checkout(srn)
            .then((data) => {
                return parseSrn(srn);
            });
    }
}

export function contentCheckIn(uuid, lockUuid) {
    return function(dispatch, getState) {
        return service.checkin(uuid, lockUuid);
    }
}

export function contentSyncPrint(uuid) {
    return function(dispatch, getState) {
        return service.sync(uuid);
    }
}

export function contentClearLock(userUuid, lockUuid) {
    return function (dispatch, getState) {
        return service.clearLock(userUuid, lockUuid);
    }
}