<commit_before>import {createAction} from 'redux-actions'
import snakeCaseKeys from 'snakecase-keys'

import {secureFetch} from '../../common/actions'
import {fetchGTFSEntities} from '../../manager/actions/versions'
import {savedGtfsEntity, updateEditSetting} from './active'
import {entityIsNew} from '../util/objects'
import {getEditorNamespace} from '../util/gtfs'

export const undoActiveTripPatternEdits = createAction('UNDO_TRIP_PATTERN_EDITS')
export const setActiveStop = createAction('SET_ACTIVE_PATTERN_STOP')
export const togglingPatternEditing = createAction('TOGGLE_PATTERN_EDITING')
export const setActivePatternSegment = createAction('SET_ACTIVE_PATTERN_SEGMENT')
export const resnapStops = createAction('RESNAP_STOPS')

export function togglePatternEditing () {
  return function (dispatch, getState) {
    const {editGeometry} = getState().editor.editSettings.present
    dispatch(updateEditSetting('editGeometry', !editGeometry))
    dispatch(togglingPatternEditing())
  }
}

/**
 * Fetch all trip patterns for the feed. Used to display pattern shapes in map
 * layer.
 */
export function fetchTripPatterns (feedId) {
  return function (dispatch, getState) {
    const namespace = getEditorNamespace(feedId, getState())
    return dispatch(fetchGTFSEntities({namespace, type: 'pattern', editor: true}))
  }
}

export function saveTripPattern (feedId, tripPattern) {
  return function (dispatch, getState) {
    const patternIsNew = entityIsNew(tripPattern)
    const {sessionId} = getState().editor.data.lock
    const method = patternIsNew ? 'post' : 'put'
    // Route ID needed for re-fetch is the ID field of the active entity (route)
    // NOTE: The pattern being saved is the active **sub**-entity.
    const routeId = getState().editor.data.active.entity.id
    const data = snakeCaseKeys(tripPattern)
    // Convert control points and pattern segments into shape points
    // FIXME: ready shape_points for insertion into shapes table
    data.shapes = data.shape_points
    delete data.shape_points
    const url = patternIsNew
      ? `/api/editor/secure/pattern?feedId=${feedId}&sessionId=${sessionId}`
      : `/api/editor/secure/pattern/${tripPattern.id}?feedId=${feedId}&sessionId=${sessionId}`
    data.id = patternIsNew ? null : tripPattern.id
    return dispatch(secureFetch(url, method, data))
      .then(res => res.json())
      .then(newTripPattern => {
        dispatch(savedGtfsEntity(newTripPattern))
        const namespace = getEditorNamespace(feedId, getState())
        // // Refetch entity and replace in store
        console.log(`setting active pattern to ${newTripPattern.id}`)
        return dispatch(fetchGTFSEntities({
          namespace,
          id: routeId,
          type: 'route',
          editor: true,
          replaceNew: patternIsNew,
          patternId: newTripPattern.id
        }))
      })
  }
}

/**
 * Deletes all trips for a given pattern ID. This is used to clear the trips for
 * a pattern if/when a pattern is changed from frequency-based to timetable-baed.
 */
export function deleteAllTripsForPattern (feedId, patternId) {
  return function (dispatch, getState) {
    const {sessionId} = getState().editor.data.lock<commit_msg>fix(editor): reset trip pattern edits after error on save<commit_after>import {createAction} from 'redux-actions'
import snakeCaseKeys from 'snakecase-keys'

import {secureFetch} from '../../common/actions'
import {fetchGTFSEntities} from '../../manager/actions/versions'
import {resetActiveGtfsEntity, savedGtfsEntity, updateEditSetting} from './active'
import {entityIsNew} from '../util/objects'
import {getEditorNamespace} from '../util/gtfs'

export const undoActiveTripPatternEdits = createAction('UNDO_TRIP_PATTERN_EDITS')
export const setActiveStop = createAction('SET_ACTIVE_PATTERN_STOP')
export const togglingPatternEditing = createAction('TOGGLE_PATTERN_EDITING')
export const setActivePatternSegment = createAction('SET_ACTIVE_PATTERN_SEGMENT')
export const resnapStops = createAction('RESNAP_STOPS')

export function togglePatternEditing () {
  return function (dispatch, getState) {
    const {editGeometry} = getState().editor.editSettings.present
    dispatch(updateEditSetting('editGeometry', !editGeometry))
    dispatch(togglingPatternEditing())
  }
}

/**
 * Fetch all trip patterns for the feed. Used to display pattern shapes in map
 * layer.
 */
export function fetchTripPatterns (feedId) {
  return function (dispatch, getState) {
    const namespace = getEditorNamespace(feedId, getState())
    return dispatch(fetchGTFSEntities({namespace, type: 'pattern', editor: true}))
  }
}

export function saveTripPattern (feedId, tripPattern) {
  return function (dispatch, getState) {
    const patternIsNew = entityIsNew(tripPattern)
    const {sessionId} = getState().editor.data.lock
    const method = patternIsNew ? 'post' : 'put'
    // Route ID needed for re-fetch is the ID field of the active entity (route)
    // NOTE: The pattern being saved is the active **sub**-entity.
    const routeId = getState().editor.data.active.entity.id
    const data = snakeCaseKeys(tripPattern)
    // Convert control points and pattern segments into shape points
    // FIXME: ready shape_points for insertion into shapes table
    data.shapes = data.shape_points
    delete data.shape_points
    const url = patternIsNew
      ? `/api/editor/secure/pattern?feedId=${feedId}&sessionId=${sessionId}`
      : `/api/editor/secure/pattern/${tripPattern.id}?feedId=${feedId}&sessionId=${sessionId}`
    data.id = patternIsNew ? null : tripPattern.id
    return dispatch(secureFetch(url, method, data))
      .then(res => res.json())
      .then(newTripPattern => {
        dispatch(savedGtfsEntity(newTripPattern))
        const namespace = getEditorNamespace(feedId, getState())
        // // Refetch entity and replace in store
        console.log(`setting active pattern to ${newTripPattern.id}`)
        return dispatch(fetchGTFSEntities({
          namespace,
          id: routeId,
          type: 'route',
          editor: true,
          replaceNew: patternIsNew,
          patternId: newTripPattern.id
        }))
      })
      .catch(err => {
        console.log(err)
        dispatch(resetActiveGtfsEntity(tripPattern, 'trippattern'))
      })
  }
}

/**
 * Deletes all trips for a given pattern ID. This is used to clear the trips for
 * a pattern if/when a pattern is changed from frequency-based to timetable-baed.
 */
export function deleteAllTripsForPattern (feedId, patternId) {
  return function (dispatch, getState) {
    const {sessionId} = getState().editor.data.lock
{'fraction_non_alphanumeric': 0.10764572293716881, 'fraction_numerical': 0.0, 'mean_word_length': 4.000757002271007, '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': '648558', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1873, 'n_words': 510}