All files / report reducer.js

100% Statements 55/55
100% Branches 34/34
100% Functions 19/19
100% Lines 53/53
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211        3x                         3x                           3x 2x   7x   3x 7x 7x 7x 7x 7x 7x 7x     3x 7x 7x 11x 11x   7x     3x 6x 3x       58x   1x             6x                   2x           2x                 5x         1x   2x     1x         2x               1x               1x       4x               1x         1x         1x         2x         2x             25x       3x 3x     3x     10x       3x       3x       3x       2x     3x       3x     2x       1x        
import { actionTypes as asyncDataFetchActionTypes } from '@bufferapp/async-data-fetch';
import keyWrapper from '@bufferapp/keywrapper';
import { DIRECTION_UP, DIRECTION_DOWN } from './middleware';
 
export const actionTypes = keyWrapper('REPORT', {
  FETCH: 'FETCH',
  EDIT_NAME: 'EDIT_NAME',
  SAVE_CHANGES: 'SAVE_CHANGES',
  MOVE_CHART_UP: 'MOVE_CHART_UP',
  MOVE_CHART_DOWN: 'MOVE_CHART_DOWN',
  DELETE_CHART: 'DELETE_CHART',
  PARSE_PAGE_BREAKS: 'PARSE_PAGE_BREAKS',
  UPLOAD_LOGO: 'UPLOAD_LOGO',
  DELETE_LOGO: 'DELETE_LOGO',
  EDIT_DESCRIPTION: 'EDIT_DESCRIPTION',
});
 
const initialState = {
  loading: true,
  charts: [],
  name: '',
  logoUrl: '',
  edit: false,
  isLogoUploading: false,
  isLogoDropzoneDisabled: false,
  isLogoUploadErrored: false,
  isDescriptionEditing: false,
  description: '',
  hasError: false,
};
 
const getOppositeDirection = direction =>
  (direction === DIRECTION_UP ? DIRECTION_DOWN : DIRECTION_UP);
 
const getNewPosition = (position, direction) => position + (direction === DIRECTION_UP ? -1 : 1);
 
const moveChartInPosition = (position, direction, charts) => {
  const newPosition = getNewPosition(position, direction);
  const chartToSwitchPlacesWith = charts[newPosition];
  const chartToMove = charts[position];
  const reorderedCharts = [...charts];
  reorderedCharts[position] = chartToSwitchPlacesWith;
  reorderedCharts[newPosition] = chartToMove;
  return reorderedCharts;
};
 
const moveChart = (chartId, direction, charts) => {
  let position = 0;
  charts.find((chart, index) => {
    position = index;
    return chart._id === chartId;
  });
  return moveChartInPosition(position, direction, charts);
};
 
const getLogoUrl = (report) => {
  if (report.logo === undefined) return '';
  return report.logo.url ? report.logo.url : '';
};
 
export default (state = initialState, action) => {
  switch (action.type) {
    case `get_report_${asyncDataFetchActionTypes.FETCH_START}`:
      return {
        ...initialState,
        name: action.args.name,
        description: action.args.description,
        id: action.args._id,
      };
    case `get_report_${asyncDataFetchActionTypes.FETCH_SUCCESS}`:
      return {
        ...state,
        name: action.result.name,
        description: action.result.description,
        charts: action.result.charts,
        dateRange: action.result.date_range,
        logoUrl: getLogoUrl(action.result),
        loading: false,
      };
    case `get_report_${asyncDataFetchActionTypes.FETCH_FAIL}`:
      return {
        ...state,
        loading: false,
        hasError: true,
      };
    case `move_chart_${asyncDataFetchActionTypes.FETCH_FAIL}`:
      return {
        ...state,
        charts: moveChart(
          action.args.chartId,
          getOppositeDirection(action.args.direction),
          state.charts,
        ),
      };
    case `move_chart_${asyncDataFetchActionTypes.FETCH_START}`:
      return {
        ...state,
        charts: moveChart(action.args.chartId, action.args.direction, state.charts),
      };
    case `delete_chart_${asyncDataFetchActionTypes.FETCH_SUCCESS}`:
      return {
        ...state,
        charts: state.charts.filter(chart => chart._id !== action.args.chartId),
      };
    case `upload_report_logo_${asyncDataFetchActionTypes.FETCH_START}`:
      return {
        ...state,
        isLogoUploading: true,
      };
    case `upload_report_logo_${asyncDataFetchActionTypes.FETCH_SUCCESS}`:
      return {
        ...state,
        isLogoUploading: false,
        isLogoDropzoneDisabled: true,
        isLogoUploadErrored: false,
        logoUrl: action.result.logo.url,
      };
    case `upload_report_logo_${asyncDataFetchActionTypes.FETCH_FAIL}`:
      return {
        ...state,
        isLogoUploading: false,
        isLogoDropzoneDisabled: false,
        isLogoUploadErrored: true,
        logoUrl: '',
      };
    case `delete_report_logo_${asyncDataFetchActionTypes.FETCH_SUCCESS}`:
      return {
        ...state,
      };
    case actionTypes.SAVE_CHANGES:
      return {
        ...state,
        edit: false,
        isDescriptionEditing: false,
        name: action.name ? action.name : state.name,
        description: action.description || action.description === '' ? action.description : state.description,
      };
    case `update_report_${asyncDataFetchActionTypes.FETCH_START}`:
      return {
        ...state,
        loading: !Object.is(state.dateRange, action.args.dateRange),
      };
    case actionTypes.EDIT_NAME:
      return {
        ...state,
        edit: true,
      };
    case actionTypes.EDIT_DESCRIPTION:
      return {
        ...state,
        isDescriptionEditing: true,
      };
    case actionTypes.UPLOAD_LOGO:
      return {
        ...state,
        logo: action.logo ? action.logo : state.logo,
      };
    case actionTypes.DELETE_LOGO:
      return {
        ...state,
        isLogoDropzoneDisabled: false,
        isLogoUploadErrored: false,
        logoUrl: '',
      };
    default:
      return state;
  }
};
 
export const actions = {
  editName: () => ({
    type: actionTypes.EDIT_NAME,
  }),
  editDescription: () => ({
    type: actionTypes.EDIT_DESCRIPTION,
  }),
  saveChanges: report => ({
    type: actionTypes.SAVE_CHANGES,
    ...report,
  }),
  moveUp: chartId => ({
    type: actionTypes.MOVE_CHART_UP,
    chartId,
  }),
  moveDown: chartId => ({
    type: actionTypes.MOVE_CHART_DOWN,
    chartId,
  }),
  deleteChart: chartId => ({
    type: actionTypes.DELETE_CHART,
    chartId,
  }),
  parsePageBreaks: () => ({
    type: actionTypes.PARSE_PAGE_BREAKS,
  }),
  uploadLogo: logo => ({
    type: actionTypes.UPLOAD_LOGO,
    ...logo,
  }),
  deleteLogo: () => ({
    type: actionTypes.DELETE_LOGO,
  }),
  saveDescription: report => ({
    type: actionTypes.SAVE_CHANGES,
    ...report,
  }),
  fetch: () => ({
    type: actionTypes.FETCH,
  }),
};