{"size": 1113, "ext": "js", "max_stars_repo_path": "client/src/actions/expenseActions.js", "max_stars_repo_name": "briavers/mobdev-II-briavers-coinsaver-web", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "client/src/actions/expenseActions.js", "max_issues_repo_name": "briavers/mobdev-II-briavers-coinsaver-web", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "client/src/actions/expenseActions.js", "max_forks_repo_name": "briavers/mobdev-II-briavers-coinsaver-web", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.5, "max_line_length": 106, "alphanum_fraction": 0.5633423181}
import { EXPENSE_CREATION_ERROR, EXPENSE_CREATED } from '../constants';

export function createExpense({ title, description, amount, billingAccount,subCategory, user }, history) {
  return async (dispatch) => {
    try {
      const expenseData = new Blob([JSON.stringify({
        title: title,
        description: description,
        amount: amount,
        _billingAccount: billingAccount,
        _subCategory: subCategory,
        user: user

      }, null, 2)], {
        type: 'application/json'
      });
      const options = {
          method: 'POST',
          body: expenseData,
          mode: 'cors',
          cache: 'default'
      };
      const response = await fetch('/api/v1/expenses', options);
      const responseJson = await response.json();

      dispatch({ 
        type: EXPENSE_CREATED,
        payload: responseJson,
        
      });
      window.location.href = '/expenses';
    } catch(error) {
      dispatch({
        type: EXPENSE_CREATION_ERROR,
        payload: {
          message: 'Invalid email or password',
          exception: error
        }
      });
    }
  };
}