mbeauv/urbanoe-communications

View on GitHub
src/urbanoe/city/reducers/city_statistics_reducer.js

Summary

Maintainability
A
0 mins
Test Coverage
// @flow

import type { PieChartData } from '../../models';
import type { UrbanoeAction } from '../../types';

export type StatReportState = {
  loading: boolean,
  error: ?Object,
  chart: ?PieChartData,
};

export type StatisticsState = {
  [ key: string] : StatReportState,
};

/**
 * Reducer function to process city statistics actions. At the present time, only the
 * getCityStatistics action is handled.
 */
export function cityStatisticsReducer(state: StatisticsState = {}, action: UrbanoeAction)
: StatisticsState {
  const report = {};
  switch (action.type) {
    case 'CITY_STATISTICS_REQUEST':
      report[action.statsType] = { loading: true, error: null, chart: null };
      return { ...state, ...report };
    case 'CITY_STATISTICS_RESPONSE_OK':
      report[action.statsType] = { loading: false, chart: action.chart };
      return { ...state, ...report };
    case 'CITY_STATISTICS_RESPONSE_ERROR':
      report[action.statsType] = { loading: false, chart: action.error };
      return { ...state, ...report };
    default:
      return state;
  }
}