Home Reference Source

application/redux/actions/content-version-actions.js

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

class ContentVersionService extends BaseService {

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

    fetchOne(content, version, opts = {}) {
        return this.get('/v3/version/' + content + '/' + version, opts);
    }

    restore(uuid, opts = {}) {
        return this.put('/v3/version/' + uuid, opts);
    }
}

const service = new ContentVersionService;

/**
 * Action types
 */
export const VERSION_REQUEST = 'VERSION_REQUEST';
export const VERSION_RECEIVE = 'VERSION_RECEIVE';
export const VERSION_RECEIVE_ONE = 'VERSION_RECEIVE_ONE';
export const VERSION_SELECTED = 'VERSION_SELECTED';
export const VERSION_INVALIDATE = 'VERSION_INVALIDATE';

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

export function contentVersionReceive(items) {

    return {
        type: VERSION_RECEIVE,
        payload: {
            versions: items ? items : []
        }
    };
}

export function contentVersionReceiveOne(item) {
    return {
        type: VERSION_RECEIVE_ONE,
        payload: {
            version: item ? item : {}
        }
    };
}

export function contentVersionSetSelected(item) {
    return {
        type: VERSION_SELECTED,
        payload: {
            selected: item ? item : false
        }
    };
}

export function contentVersionInvalidate() {
    return {
        type: VERSION_INVALIDATE,
        payload: {
        }
    };
}

export function contentVersionsFetch(srn, params = {}) {
    return function(dispatch, getState) {
        dispatch(contentVersionRequest());

        return service.fetch(srn, params)
            .then(response => {
                return dispatch(contentVersionReceive(response.get('items')));
            });
    };
}

export function contentFetchOne(content, version, params = {}) {
    return function (dispatch, getState) {
        dispatch(contentVersionRequest({}));

        return service.fetchOne(content, version, params)
            .then(response => {
                return dispatch(contentVersionReceiveOne(response.first()));
            });
    };
}

export function contentVersionRestore(uuid) {
    return function(dispatch, getState) {
        return service.restore(uuid)
            .then(response => response.first());
;
    }
}