Home Reference Source

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

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

class ContentSearchService extends BaseService {

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

const service = new ContentSearchService;

/**
 * Action types
 */
export const CONTENT_SEARCH_REQUEST = 'CONTENT_SEARCH_REQUEST';
export const CONTENT_SEARCH_RECEIVE = 'CONTENT_SEARCH_RECEIVE';
export const CONTENT_SEARCH_INVALIDATE = 'CONTENT_SEARCH_INVALIDATE';
export const CONTENT_SEARCH_RESET = 'CONTENT_SEARCH_RESET';
export const CONTENT_SEARCH_PAGINATION = 'CONTENT_SEARCH_PAGINATION';

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

export function contentSearchReceive(items) {

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

export function contentSearchReceivePagination(pagination) {
    return {
        type: CONTENT_SEARCH_PAGINATION,
        payload: {
            pagination: pagination ? pagination : {}
        }
    }
}


export function contentSearchInvalidate() {
    return {
        type: CONTENT_SEARCH_INVALIDATE,
        payload: {
        }
    };
}

export function contentSearchClearItems() {
    return {
        type: CONTENT_SEARCH_RESET,
        payload: {
        }
    };
}

export function contentSearchReset() {
    return function (dispatch, getState) {
        return Promise.all([dispatch(contentSearchClearItems())]);
    }
}

export function contentSearchFetch(params = {}) {
    return function(dispatch, getState) {
        dispatch(contentSearchRequest(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(contentSearchReceivePagination(response));
                return dispatch(contentSearchReceive(response.get('items')));
            });
    };
}