mbeauv/urbanoe-communications

View on GitHub
src/urbanoe/user/actions/user_profile_actions.js

Summary

Maintainability
B
5 hrs
Test Coverage
// @flow

import { communicator } from '../../../common';
import type { UrbanoeThunkAction } from '../../types';

/**
 * Returns an asynchronous action to retrieve a given user's profile.
 */
export function getSelectedUserProfile(userId: number): UrbanoeThunkAction {
  return async (dispatch) => {
    dispatch({ type: 'SELECTED_USER_PROFILE_REQUEST', userId });

    try {
      const userUrl = `end_users/${userId}.json`;
      const response = await communicator().get(userUrl);
      dispatch({ type: 'SELECTED_USER_PROFILE_RESPONSE_OK', userProfile: response.data });
    } catch (error) {
      dispatch({ type: 'SELECTED_USER_PROFILE_RESPONSE_ERROR', userId, error });
    }
  };
}