Home Reference Source

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

  1. import BaseService from './../../services/base-service';
  2. import {parseSrn} from './../../util/strings';
  3. import Immutable from 'immutable';
  4.  
  5. class ContentSearchService extends BaseService {
  6.  
  7. fetch(opts) {
  8. return this.get('/v3/search', opts);
  9. }
  10. }
  11.  
  12. const service = new ContentSearchService;
  13.  
  14. /**
  15. * Action types
  16. */
  17. export const CONTENT_SEARCH_REQUEST = 'CONTENT_SEARCH_REQUEST';
  18. export const CONTENT_SEARCH_RECEIVE = 'CONTENT_SEARCH_RECEIVE';
  19. export const CONTENT_SEARCH_INVALIDATE = 'CONTENT_SEARCH_INVALIDATE';
  20. export const CONTENT_SEARCH_RESET = 'CONTENT_SEARCH_RESET';
  21. export const CONTENT_SEARCH_PAGINATION = 'CONTENT_SEARCH_PAGINATION';
  22.  
  23. /**
  24. * Action creators
  25. */
  26. export function contentSearchRequest(params = {}) {
  27. return {
  28. type: CONTENT_SEARCH_REQUEST,
  29. payload: {
  30. params: params
  31. }
  32. };
  33. }
  34.  
  35. export function contentSearchReceive(items) {
  36.  
  37. return {
  38. type: CONTENT_SEARCH_RECEIVE,
  39. payload: {
  40. content: items ? items : []
  41. }
  42. };
  43. }
  44.  
  45. export function contentSearchReceivePagination(pagination) {
  46. return {
  47. type: CONTENT_SEARCH_PAGINATION,
  48. payload: {
  49. pagination: pagination ? pagination : {}
  50. }
  51. }
  52. }
  53.  
  54.  
  55. export function contentSearchInvalidate() {
  56. return {
  57. type: CONTENT_SEARCH_INVALIDATE,
  58. payload: {
  59. }
  60. };
  61. }
  62.  
  63. export function contentSearchClearItems() {
  64. return {
  65. type: CONTENT_SEARCH_RESET,
  66. payload: {
  67. }
  68. };
  69. }
  70.  
  71. export function contentSearchReset() {
  72. return function (dispatch, getState) {
  73. return Promise.all([dispatch(contentSearchClearItems())]);
  74. }
  75. }
  76.  
  77. export function contentSearchFetch(params = {}) {
  78. return function(dispatch, getState) {
  79. dispatch(contentSearchRequest(params));
  80.  
  81. params.per_page = params.per_page ? params.per_page : 20;
  82. params.order = params.order ? params.order : "modified_at";
  83. params.dir = params.dir ? params.dir : "desc";
  84.  
  85. return service.fetch(params)
  86. .then(response => {
  87. dispatch(contentSearchReceivePagination(response));
  88. return dispatch(contentSearchReceive(response.get('items')));
  89. });
  90. };
  91. }