All files / posts-table reducer.js

100% Statements 19/19
100% Branches 11/11
100% Functions 1/1
100% Lines 19/19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118          3x               3x                           14x   1x                 3x             1x           1x         1x         1x       1x         2x           1x           2x       3x   3x           3x         3x           3x           4x            
import { actionTypes as asyncDataFetchActionTypes } from '@bufferapp/async-data-fetch';
import { actionTypes as profileActionTypes } from '@bufferapp/analyze-profile-selector';
 
import keyWrapper from '@bufferapp/keywrapper';
 
export const actionTypes = keyWrapper('POSTS_TABLE', {
  SELECT_TOP_POSTS_METRIC: 'SELECT_TOP_POSTS_METRIC',
  TOGGLE_TOP_POSTS_DROPDOWN: 'TOGGLE_TOP_POSTS_DROPDOWN',
  SELECT_TOP_POSTS_COUNT: 'SELECT_TOP_POSTS_COUNT',
  SELECT_TOP_POSTS_ORDER: 'SELECT_TOP_POSTS_ORDER',
  SEARCH: 'SEARCH',
});
 
const initialState = {
  posts: [],
  loading: true,
  hasError: false,
  searching: false,
  metrics: [],
  isDropdownOpen: false,
  isDescendingSelected: true,
  selectedMetric: {},
  activePostsCount: 5,
  searchTerms: [],
};
 
export default (state = initialState, action) => {
  switch (action.type) {
    case `posts_${asyncDataFetchActionTypes.FETCH_START}`:
      return {
        ...initialState,
        posts: state.posts,
        activePostsCount: parseInt(state.activePostsCount, 10),
        selectedMetric: state.selectedMetric,
        isDescendingSelected: state.isDescendingSelected,
        searchTerms: state.searchTerms,
      };
    case `posts_${asyncDataFetchActionTypes.FETCH_SUCCESS}`:
      return {
        ...state,
        loading: false,
        searching: false,
        posts: action.result,
      };
    case actionTypes.SELECT_TOP_POSTS_METRIC:
      return {
        ...state,
        isDropdownOpen: false,
        selectedMetric: action.metric,
      };
    case actionTypes.SELECT_TOP_POSTS_COUNT:
      return {
        ...state,
        activePostsCount: parseInt(action.postsCount, 10),
      };
    case actionTypes.TOGGLE_TOP_POSTS_DROPDOWN:
      return {
        ...state,
        isDropdownOpen: !state.isDropdownOpen,
      };
    case profileActionTypes.SELECT_PROFILE:
      return {
        ...initialState,
      };
    case actionTypes.SELECT_TOP_POSTS_ORDER:
      return {
        ...state,
        isDescendingSelected: action.isDescendingSelected,
      };
    case actionTypes.SEARCH:
      return {
        ...state,
        searching: true,
        searchTerms: action.tags,
      };
    case `posts_${asyncDataFetchActionTypes.FETCH_FAIL}`:
      return {
        ...initialState,
        loading: false,
        hasError: true,
      };
    default:
      return state;
  }
};
 
export const actions = {
  selectMetric(metric) {
    return {
      type: actionTypes.SELECT_TOP_POSTS_METRIC,
      metric,
    };
  },
  toggleDropdown() {
    return {
      type: actionTypes.TOGGLE_TOP_POSTS_DROPDOWN,
    };
  },
  handlePostsCountClick(postsCount) {
    return {
      type: actionTypes.SELECT_TOP_POSTS_COUNT,
      postsCount,
    };
  },
  handlePostsSortClick(isDescendingSelected) {
    return {
      type: actionTypes.SELECT_TOP_POSTS_ORDER,
      isDescendingSelected,
    };
  },
  search(tags) {
    return {
      type: actionTypes.SEARCH,
      tags,
    };
  },
};