<commit_before>import { reset } from 'redux-form';
import axios from 'axios';
import * as types from '../constants/ActionTypes';
import { tacticSchema, tacticDetailSchema } from '../constants/Schemas';
import { handleError } from './index';

export const selectTactic = id => ({ type: types.SELECT_TACTIC, id });
export const openCreateTacticDialog = () => ({ type: types.OPEN_CREATE_TACTIC_DIALOG });
export const closeCreateTacticDialog = () => ({ type: types.CLOSE_CREATE_TACTIC_DIALOG });

const createTactic = data => dispatch =>
  dispatch({
    type: types.CREATE_TACTIC,
    payload: axios.post('/tactics', data),
    meta: { data },
  }).catch(error => dispatch(handleError(error)));

export const fetchTactic = id => dispatch =>
  dispatch({
    type: types.FETCH_TACTIC,
    payload: axios.get(`/tactics/${id}`),
    meta: {
      schema: tacticDetailSchema,
      data: { id },
    },
  }).catch(error => dispatch(handleError(error)));

export const fetchTactics = () => dispatch =>
  dispatch({
    type: types.FETCH_TACTICS,
    payload: axios.get('/tactics'),
    meta: {
      schema: [tacticSchema],
    },
  }).catch(error => dispatch(handleError(error)));

const shouldFetchTactic = ({ entities }, id) => {
  const { tacticDetails } = entities;
  const { status, byId } = tacticDetails;
  const isFetching = status.fetching.indexOf(id) !== -1;
  const tacticDetail = byId[id];
  const tacticDetailExists = !!tacticDetail;
  return !tacticDetailExists && !isFetching;
};

export const fetchTacticIfNeeded = id => (dispatch, getState) => {
  if (shouldFetchTactic(getState(), id)) {
    return dispatch(fetchTactic(id));
  }
  return Promise.resolve();
};

export const createAndSelectTactic = data => dispatch =>
  dispatch(createTactic(data)).then((response) => {
    dispatch(selectTactic(response.value.data.id));
    dispatch(closeCreateTacticDialog());
    dispatch(reset('createTacticForm'));
  });<commit_msg>Redirect to tactic url after creating a new tactic<commit_after>import { reset } from 'redux-form';
import axios from 'axios';
import * as types from '../constants/ActionTypes';
import { tacticSchema, tacticDetailSchema } from '../constants/Schemas';
import { handleError } from './index';
import history from '../history';

export const openCreateTacticDialog = () => ({ type: types.OPEN_CREATE_TACTIC_DIALOG });
export const closeCreateTacticDialog = () => ({ type: types.CLOSE_CREATE_TACTIC_DIALOG });

export const selectTactic = (id, redirect = false) => {
  if (redirect) history.push(`/tactics/${id}`);
  return { type: types.SELECT_TACTIC, id };
};

const createTactic = data => dispatch =>
  dispatch({
    type: types.CREATE_TACTIC,
    payload: axios.post('/tactics', data),
    meta: { data },
  }).catch(error => dispatch(handleError(error)));

export const fetchTactic = id => dispatch =>
  dispatch({
    type: types.FETCH_TACTIC,
    payload: axios.get(`/tactics/${id}`),
    meta: {
      schema: tacticDetailSchema,
      data: { id },
    },
  }).catch(error => dispatch(handleError(error)));

export const fetchTactics = () => dispatch =>
  dispatch({
    type: types.FETCH_TACTICS,
    payload: axios.get('/tactics'),
    meta: {
      schema: [tacticSchema],
    },
  }).catch(error => dispatch(handleError(error)));

const shouldFetchTactic = ({ entities }, id) => {
  const { tacticDetails } = entities;
  const { status, byId } = tacticDetails;
  const isFetching = status.fetching.indexOf(id) !== -1;
  const tacticDetail = byId[id];
  const tacticDetailExists = !!tacticDetail;
  return !tacticDetailExists && !isFetching;
};

export const fetchTacticIfNeeded = id => (dispatch, getState) => {
  if (shouldFetchTactic(getState(), id)) {
    return dispatch(fetchTactic(id));
  }
  return Promise.resolve();
};

export const createAndSelectTactic = data => dispatch =>
  dispatch(createTactic(data)).then((response) => {
    const id = response.value.data.id;
    dispatch(selectTactic(id, true));
    dispatch(closeCreateTacticDialog());
    dispatch(reset('createTacticForm'));
  });
{'fraction_non_alphanumeric': 0.15599505562422744, 'fraction_numerical': 0.0004944375772558715, 'mean_word_length': 4.38031914893617, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, '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': '4083555', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1255, 'n_words': 282}