<commit_before>import axios from 'axios';
import { normalize } from 'normalizr';
import { questionSchema } from '../schema';
import routes from '../routes';
import { deleteObject } from './action_helpers';
import { getCSRFToken } from './index';
import {
  ADD_QUESTION,
  REMOVE_QUESTION,
  DELETE_QUESTION,
  REORDER_QUESTION,
  SAVE_QUESTION,
  SAVE_DRAFT_QUESTION,
  PUBLISH_QUESTION,
  FETCH_QUESTION_USAGE,
  ADD_ENTITIES
} from './types';

export function addQuestion(form, question) {
  return {
    type: ADD_QUESTION,
    payload: {form, question}
  };
}

export function removeQuestion(form, index) {
  return {
    type: REMOVE_QUESTION,
    payload: {form, index}
  };
}

export function deleteQuestion(id, callback=null) {
  return {
    type: DELETE_QUESTION,
    payload: deleteObject(routes.question_path(id), callback)
  };
}

export function reorderQuestion(form, index, direction) {
  return {
    type: REORDER_QUESTION,
    payload: {form, index, direction}
  };
}

export function fetchQuestion(id) {
  return {
    type: ADD_ENTITIES,
    payload: axios.get(routes.questionPath(id), {
      headers: {'Accept': 'application/json', 'X-Key-Inflection': 'camel'}
    }).then((response) => {
      const normalizedData = normalize(response.data, questionSchema);
      return normalizedData.entities;
    })
  };
}

export function fetchQuestionUsage(id) {
  return {
    type: FETCH_QUESTION_USAGE,
    payload: axios.get(routes.usageQuestionPath(id), {
      headers: {'Accept': 'application/json', 'X-Key-Inflection': 'camel'}
    })
  };
}

export function saveQuestion(question, successHandler=null, failureHandler=null) {
  const authenticityToken  = getCSRFToken();
  const linkedResponseSets = question.linkedResponseSets ? question.linkedResponseSets.map((rs) => rs.id) : [];
  delete question.linkedResponseSets;
  const postPromise = axios.post(routes.questionsPath(),
                      {question, authenticityToken, linkedResponseSets},
                      {headers: {'X-Key-Inflection': 'camel', 'Accept': 'application/json'}});
  if (failureHandler) {
    postPromise.catch(failureHandler);
  }
  if (successHandler) {
    postPromise.then(successHandler);
  }
  return {
    type: SAVE_QUESTION,
    payload: postPromise
  };
}

export function saveDraftQuestion(id, question, callback=null) {
  const authenticityToken  = getCSRFToken();
  const linkedResponseSets = question.linkedResponseSets;
  delete question.linkedResponseSets;
  const putPromise = axios.put(routes.questions_path()+'/'+id,
                      {question, authenticityToken, linkedResponseSets},
                      {headers: {'X-Key-Inflection': 'camel', 'Accept': 'application/json'}});
  if (callback) {
    putPromise.then(callback);
  }
  return {
    type: SAVE_DRAFT_QUESTION,
    payload: putPromise
  };
}

export function publishQuestion(id, callback=null) {
  const authenticityToken  = getCSRFToken();
  const putPromise = axios.put(routes.publish_question_path(id),
    {authenticityToken},
    {headers: {'X-Key-Inflection': 'camel', 'Accept': 'application/json'}
    });
  if (callback) {
    putPromise.then(callback);<commit_msg>Fixing bug with question edit<commit_after>import axios from 'axios';
import { normalize } from 'normalizr';
import { questionSchema } from '../schema';
import routes from '../routes';
import { deleteObject } from './action_helpers';
import { getCSRFToken } from './index';
import {
  ADD_QUESTION,
  REMOVE_QUESTION,
  DELETE_QUESTION,
  REORDER_QUESTION,
  SAVE_QUESTION,
  SAVE_DRAFT_QUESTION,
  PUBLISH_QUESTION,
  FETCH_QUESTION_USAGE,
  ADD_ENTITIES
} from './types';

export function addQuestion(form, question) {
  return {
    type: ADD_QUESTION,
    payload: {form, question}
  };
}

export function removeQuestion(form, index) {
  return {
    type: REMOVE_QUESTION,
    payload: {form, index}
  };
}

export function deleteQuestion(id, callback=null) {
  return {
    type: DELETE_QUESTION,
    payload: deleteObject(routes.question_path(id), callback)
  };
}

export function reorderQuestion(form, index, direction) {
  return {
    type: REORDER_QUESTION,
    payload: {form, index, direction}
  };
}

export function fetchQuestion(id) {
  return {
    type: ADD_ENTITIES,
    payload: axios.get(routes.questionPath(id), {
      headers: {'Accept': 'application/json', 'X-Key-Inflection': 'camel'}
    }).then((response) => {
      const normalizedData = normalize(response.data, questionSchema);
      return normalizedData.entities;
    })
  };
}

export function fetchQuestionUsage(id) {
  return {
    type: FETCH_QUESTION_USAGE,
    payload: axios.get(routes.usageQuestionPath(id), {
      headers: {'Accept': 'application/json', 'X-Key-Inflection': 'camel'}
    })
  };
}

export function saveQuestion(question, successHandler=null, failureHandler=null) {
  const authenticityToken  = getCSRFToken();
  const linkedResponseSets = question.linkedResponseSets ? question.linkedResponseSets.map((rs) => rs.id) : [];
  delete question.linkedResponseSets;
  const postPromise = axios.post(routes.questionsPath(),
                      {question, authenticityToken, linkedResponseSets},
                      {headers: {'X-Key-Inflection': 'camel', 'Accept': 'application/json'}});
  if (failureHandler) {
    postPromise.catch(failureHandler);
  }
  if (successHandler) {
    postPromise.then(successHandler);
  }
  return {
    type: SAVE_QUESTION,
    payload: postPromise
  };
}

export function saveDraftQuestion(id, question, callback=null) {
  const authenticityToken  = getCSRFToken();
  const linkedResponseSets = question.linkedResponseSets ? question.linkedResponseSets.map((rs) => rs.id) : [];
  delete question.linkedResponseSets;
  const putPromise = axios.put(routes.questions_path()+'/'+id,
                      {question, authenticityToken, linkedResponseSets},
                      {headers: {'X-Key-Inflection': 'camel', 'Accept': 'application/json'}});
  if (callback) {
    putPromise.then(callback);
  }
  return {
    type: SAVE_DRAFT_QUESTION,
    payload: putPromise
  };
}

export function publishQuestion(id, callback=null) {
  const authenticityToken  = getCSRFToken();
  const putPromise = axios.put(routes.publish_question_path(id),
    {authenticityToken},
    {headers: {'X-Key-Inflection': 'camel', 'Accept': 'application/json'}
    });
  if (callback) {
    putPromise.then(callback);
{'fraction_non_alphanumeric': 0.128633948108784, 'fraction_numerical': 0.0, 'mean_word_length': 3.99921875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5200031', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1974, 'n_words': 403}