All files reducer.js

91.67% Statements 11/12
85.71% Branches 6/7
100% Functions 3/3
91.67% Lines 11/12
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      1x         1x                   11x   2x         1x             4x               1x           3x       1x 2x     1x          
import { actionTypes as asyncDataFetchActionTypes } from '@bufferapp/async-data-fetch';
import keyWrapper from '@bufferapp/keywrapper';
 
export const actionTypes = keyWrapper('HOURLY_CHART', {
  HOURLY_TOGGLE_DROPDOWN: 'HOURLY_TOGGLE_DROPDOWN',
  SELECT_METRIC: 'SELECT_METRIC',
});
 
const initialState = {
  loading: true,
  hasError: false,
  postsCount: [],
  metrics: [],
  dropdownOpen: false,
  selectedMetric: null,
};
 
export default (state = initialState, action) => {
  switch (action.type) {
    case actionTypes.HOURLY_TOGGLE_DROPDOWN:
      return {
        ...state,
        dropdownOpen: !state.dropdownOpen,
      };
    case actionTypes.SELECT_METRIC:
      return {
        ...state,
        selectedMetric: action.metric,
      };
    case `hourly_${asyncDataFetchActionTypes.FETCH_START}`:
      return initialState;
    case `hourly_${asyncDataFetchActionTypes.FETCH_SUCCESS}`:
      return {
        ...state,
        loading: false,
        metrics: action.result.metrics,
        selectedMetric: action.result.metrics[0].label,
        postsCount: action.result.postsCount,
      };
    case `hourly_${asyncDataFetchActionTypes.FETCH_FAIL}`:
      return {
        ...initialState,
        loading: false,
        hasError: true,
      };
    default:
      return state;
  }
};
 
export const actions = {
  toggleDropdown: () => ({
    type: actionTypes.HOURLY_TOGGLE_DROPDOWN,
  }),
  selectMetric: metric => ({
    type: actionTypes.SELECT_METRIC,
    metric,
  }),
};