<commit_before>import clone from 'clone'
import update from 'immutability-helper'

import queryParams from '../common/query-params'

const defaultConfig = {
  autoPlan: true,
  debouncePlanTimeMs: 0
}

// construct the initial/default query
const defaultQuery = { routingType: 'ITINERARY' }
queryParams.filter(qp => 'default' in qp).forEach(qp => {
  defaultQuery[qp.name] = qp.default
})

// TODO: parse and merge URL query params w/ default query

// TODO: fire planTrip action if default query is complete/error-free

function createOtpReducer (config, initialQuery) {
  defaultQuery.mode = defaultQuery.mode || config.modes[0] || 'TRANSIT,WALK'
  // populate query by merging any provided query params w/ the default params
  const currentQuery = Object.assign(defaultQuery, initialQuery)
  const initialState = {
    config: Object.assign(defaultConfig, config),
    currentQuery,
    location: {
      currentPosition: null,
      sessionSearches: [],
      nearbyStops: []
    },
    searches: {},
    transitIndex: {
      stops: {}
    },
    activeSearchId: 0,
    overlay: {
      bikeRental: {
        stations: []
      }
    }
  }

  return (state = initialState, action) => {
    const searchId = action.payload && action.payload.searchId
    switch (action.type) {
      case 'ROUTING_REQUEST':
        return update(state, {
          searches: { [searchId]: { $set: {
            activeItinerary: 0,
            activeLeg: null,
            activeStep: null,
            pending: true,
            query: clone(state.currentQuery),
            response: null,
            type: action.payload.routingType
          }}}
        })
      case 'ROUTING_ERROR':
        return update(state, {
          searches: {[searchId]: {
            response: {
              $set: {
                error: action.payload.error
              }
            },
            pending: {$set: false}
          }},
          activeSearchId: { $set: searchId }
        })
      case 'ROUTING_RESPONSE':
        return update(state, {
          searches: {[searchId]: {
            response: {$set: action.payload.response},
            pending: {$set: false}
          }},
          activeSearchId: { $set: searchId }
        })
      case 'BIKE_RENTAL_REQUEST':
        return update(state, {
          overlay: {
            bikeRental: {
              pending: {$set: true},
              error: {$set: null}
            }
          }
        })
      case 'BIKE_RENTAL_ERROR':
        return update(state, {
          overlay: {
            bikeRental: {
              pending: {$set: false},
              error: {$set: action.payload}
            }
          }
        })
      case 'BIKE_RENTAL_RESPONSE':
        return update(state, {<commit_msg>fix(api): Store the active searchId in state at beginning of each request<commit_after>import clone from 'clone'
import update from 'immutability-helper'

import queryParams from '../common/query-params'

const defaultConfig = {
  autoPlan: true,
  debouncePlanTimeMs: 0
}

// construct the initial/default query
const defaultQuery = { routingType: 'ITINERARY' }
queryParams.filter(qp => 'default' in qp).forEach(qp => {
  defaultQuery[qp.name] = qp.default
})

// TODO: parse and merge URL query params w/ default query

// TODO: fire planTrip action if default query is complete/error-free

function createOtpReducer (config, initialQuery) {
  defaultQuery.mode = defaultQuery.mode || config.modes[0] || 'TRANSIT,WALK'
  // populate query by merging any provided query params w/ the default params
  const currentQuery = Object.assign(defaultQuery, initialQuery)
  const initialState = {
    config: Object.assign(defaultConfig, config),
    currentQuery,
    location: {
      currentPosition: null,
      sessionSearches: [],
      nearbyStops: []
    },
    searches: {},
    transitIndex: {
      stops: {}
    },
    activeSearchId: 0,
    overlay: {
      bikeRental: {
        stations: []
      }
    }
  }

  return (state = initialState, action) => {
    const searchId = action.payload && action.payload.searchId
    switch (action.type) {
      case 'ROUTING_REQUEST':
        return update(state, {
          searches: { [searchId]: { $set: {
            activeItinerary: 0,
            activeLeg: null,
            activeStep: null,
            pending: true,
            query: clone(state.currentQuery),
            response: null,
            type: action.payload.routingType
          }}},
          activeSearchId: { $set: searchId }
        })
      case 'ROUTING_ERROR':
        return update(state, {
          searches: {[searchId]: {
            response: {
              $set: {
                error: action.payload.error
              }
            },
            pending: {$set: false}
          }}
        })
      case 'ROUTING_RESPONSE':
        return update(state, {
          searches: {[searchId]: {
            response: {$set: action.payload.response},
            pending: {$set: false}
          }}
        })
      case 'BIKE_RENTAL_REQUEST':
        return update(state, {
          overlay: {
            bikeRental: {
              pending: {$set: true},
              error: {$set: null}
            }
          }
        })
      case 'BIKE_RENTAL_ERROR':
        return update(state, {
          overlay: {
            bikeRental: {
              pending: {$set: false},
              error: {$set: action.payload}
            }
          }
        })
      case 'BIKE_RENTAL_RESPONSE':
        return update(state, {
{'fraction_non_alphanumeric': 0.11294416243654823, 'fraction_numerical': 0.0014503263234227702, 'mean_word_length': 1.8991066736731477, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '503963', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1572, 'n_words': 376}