Home Reference Source

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

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

class GlobalSearchService extends BaseService {

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

const service = new GlobalSearchService;

/**
 * Action types
 */
export const GLOBAL_SEARCH_REQUEST = 'GLOBAL_SEARCH_REQUEST';
export const GLOBAL_SEARCH_RECEIVE = 'GLOBAL_SEARCH_RECEIVE';
export const GLOBAL_SEARCH_INVALIDATE = 'GLOBAL_SEARCH_INVALIDATE';
export const GLOBAL_SEARCH_REMEMBER = 'GLOBAL_SEARCH_REMEMBER';

export const GLOBAL_SEARCH_RECEIVE_PAGINATION = 'GLOBAL_SEARCH_RECEIVE_PAGINATION';

/**
 * Action creators
 */
export function globalSearchReceivePagination(pagination) {
    return {
        type: GLOBAL_SEARCH_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 globalSearchRequest(params = {}) {
    return {
        type: GLOBAL_SEARCH_REQUEST,
        payload: {
            params: params
        }
    };
}

export function globalSearchReceive(items) {

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

export function globalSearchInvalidate() {
    return {
        type: GLOBAL_SEARCH_INVALIDATE,
        payload: {
        }
    };
}

export function globalSearchRemember(query) {
    console.log(query);
    return {
        type: GLOBAL_SEARCH_REMEMBER,
        payload: {query: query}
    };
}

export function globalSearchFetch(params = {}) {
    return function(dispatch, getState) {
        dispatch(globalSearchRequest(params));

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