mbeauv/urbanoe-communications

View on GitHub
src/urbanoe/city/actions/city_statistics_actions.js

Summary

Maintainability
A
0 mins
Test Coverage
// @flow

import { Map } from 'immutable';
import { communicator, url } from '../../../common';
import type { UrbanoeThunkAction } from '../../types';

/**
 * Returns an asynchronous action to retrieves a statistics object for
 * given city id.
 */
export function getCityStatistics(cityId: number, statsType: string): UrbanoeThunkAction {
  return async (dispatch) => {
    dispatch({ type: 'CITY_STATISTICS_REQUEST', cityId, statsType });

    try {
      const statsUrl = url(`/cities/${cityId}/statistics.json`, Map({
        type: statsType,
      }));
      const response = await communicator().get(statsUrl);
      dispatch({ type: 'CITY_STATISTICS_RESPONSE_OK', cityId, statsType, chart: response.data });
    } catch (error) {
      dispatch({ type: 'CITY_STATISTICS_RESPONSE_ERROR', cityId, statsType, error });
    }
  };
}