import * as ActionTypes from '../constants/actionTypes';
import { getConfigurationUrl, putConfigurationUrl } from '../constants/api';

import { addNotification } from './notifications';

import { get, put } from '../utils/fetch';
import { toJSON } from '../utils/helpers';

export function fetchConfig() {
  return dispatch => {
    dispatch({ type: ActionTypes.FETCH_CONFIG_REQUEST});
    return get(
      getConfigurationUrl(),
      { type: ActionTypes.FETCH_CONFIG_SUCCESS, name: "config"},
      { type: ActionTypes.FETCH_CONFIG_FAILURE, name: "error"},
      dispatch
    );
  };
}

export function putConfig(config) {
  return (dispatch) => {
    let json;
    try {
      json = toJSON(config);
    } catch (e) {
      return dispatch(addNotification('Parse Error', e.message, 'error'));
    }
    return put(
      putConfigurationUrl(),
      JSON.stringify(config),
      { type: ActionTypes.PUT_CONFIG_SUCCESS, name: "config"},
      { type: ActionTypes.PUT_CONFIG_FAILURE, name: "error"},
      dispatch
    );
  };
}

export function onEditorChange() {
  return {
    type: ActionTypes.CONFIG_EDITOR_CHANGED
  };
}

@@ -862,22 +862,20 @@
 ringify(
-config
+json
 ),%0A     

fix put config