<commit_before>import inatjs from "inaturalistjs";
import _ from "lodash";

import {
  UPDATE_CURRENT_OBSERVATION
} from "../actions/current_observation_actions";

const RESET = "observations-identify/suggestions/RESET";
const START_LOADING = "observations-identify/suggestions/START_LOADING";
const STOP_LOADING = "observations-identify/suggestions/STOP_LOADING";
const SET_QUERY = "observations-identify/suggestions/SET_QUERY";
const SET_SUGGESTIONS = "observations-identify/suggestions/SET_SUGGESTIONS";
const SET_DETAIL_TAXON = "observations-identify/suggestions/SET_DETAIL_TAXON";
const UPDATE_WITH_OBSERVATION = "observations-identify/suggestions/UPDATE_WITH_OBSERVATION";

export default function reducer(
  state = {
    query: {
      source: "observations"
    },
    loading: false,
    response: {
      results: []
    },
    responseQuery: null,
    detailTaxon: null,
    detailPhotoIndex: 0,
    observation: null // optional observation from which to derive default values for query
  },
  action
) {
  let newState = Object.assign( {}, state );
  switch ( action.type ) {
    case RESET:
      newState = {};
      break;
    case START_LOADING:
      newState.loading = true;
      newState.response.results = [];
      newState.detailTaxon = null;
      break;
    case STOP_LOADING:
      newState.loading = false;
      break;
    case SET_QUERY:
      newState.query = action.query || {};
      break;
    case SET_SUGGESTIONS:
      newState.response = action.suggestions;
      newState.responseQuery = state.query;
      newState.detailTaxon = null;
      break;
    case SET_DETAIL_TAXON:
      newState.detailTaxon = action.taxon;
      if ( action.options ) {
        newState.detailPhotoIndex = action.options.detailPhotoIndex;
      }
      break;
    case UPDATE_WITH_OBSERVATION: {
      newState.query = {
        source: state.query.source,
        order_by: state.query.order_by
      };
      const observation = action.observation;
      if ( observation.taxon ) {
        let indexOfTaxonInAncestors = observation.taxon.ancestor_ids.indexOf( observation.taxon.id );
        if ( indexOfTaxonInAncestors < 0 ) {
          indexOfTaxonInAncestors = observation.taxon.ancestor_ids.length;
        }
        if ( observation.taxon.rank_level === 10 ) {
          newState.query.taxon_id =
            observation.taxon.ancestor_ids[Math.max( indexOfTaxonInAncestors - 1, 0 )];
        } else if ( observation.taxon.rank_level < 10 ) {
          newState.query.taxon_id =
            observation.taxon.ancestor_ids[Math.max( indexOfTaxonInAncestors - 2, 0 )];
        } else {
          newState.query.taxon_id = observation.taxon.id;
        }
      }
      let placeIDs;
      if ( observation.private_place_ids && observation.private_place_ids.length > 0 ) {
        placeIDs = observation.private_place_ids;
      } else {
        placeIDs = observation.place_ids;
      }
      if ( observation.places ) {
        const place = _
          .sortBy( observation.places, p => p.bbox_area )
          .find( p => p.admin_level !== null && p.admin_level < 3 );
        newState.query.place_id = place.id;
        newState.query.place = place;
        newState.query.defaultPlace = place;
      } else if ( placeIDs && placeIDs.length > 0 ) {
        newState.query.place_id = placeIDs[placeIDs.length - 1];
      }
      newState.detailTaxon = null;
      newState.detailPhotoIndex = 0;
      newState.observation = observation;
      // Don't use the current observation when making suggestions based on nearby
      // observations
      newState.query.featured_observation_id = observation.id;
      break;
    }
    case UPDATE_CURRENT_OBSERVATION: {
      if ( action.updates.places ) {
        const place = _
          .sortBy( action.updates.places, p => p.bbox_area )
          .find( p => p.admin_level !== null && p.admin_level < 3 );
        newState.query.place_id = place.id;
        newState.query.place = place;
        newState.query.defaultPlace = place;<commit_msg>Suggestions modal wasn't working if an obs had no places.<commit_after>import inatjs from "inaturalistjs";
import _ from "lodash";

import {
  UPDATE_CURRENT_OBSERVATION
} from "../actions/current_observation_actions";

const RESET = "observations-identify/suggestions/RESET";
const START_LOADING = "observations-identify/suggestions/START_LOADING";
const STOP_LOADING = "observations-identify/suggestions/STOP_LOADING";
const SET_QUERY = "observations-identify/suggestions/SET_QUERY";
const SET_SUGGESTIONS = "observations-identify/suggestions/SET_SUGGESTIONS";
const SET_DETAIL_TAXON = "observations-identify/suggestions/SET_DETAIL_TAXON";
const UPDATE_WITH_OBSERVATION = "observations-identify/suggestions/UPDATE_WITH_OBSERVATION";

export default function reducer(
  state = {
    query: {
      source: "observations"
    },
    loading: false,
    response: {
      results: []
    },
    responseQuery: null,
    detailTaxon: null,
    detailPhotoIndex: 0,
    observation: null // optional observation from which to derive default values for query
  },
  action
) {
  let newState = Object.assign( {}, state );
  switch ( action.type ) {
    case RESET:
      newState = {};
      break;
    case START_LOADING:
      newState.loading = true;
      newState.response.results = [];
      newState.detailTaxon = null;
      break;
    case STOP_LOADING:
      newState.loading = false;
      break;
    case SET_QUERY:
      newState.query = action.query || {};
      break;
    case SET_SUGGESTIONS:
      newState.response = action.suggestions;
      newState.responseQuery = state.query;
      newState.detailTaxon = null;
      break;
    case SET_DETAIL_TAXON:
      newState.detailTaxon = action.taxon;
      if ( action.options ) {
        newState.detailPhotoIndex = action.options.detailPhotoIndex;
      }
      break;
    case UPDATE_WITH_OBSERVATION: {
      newState.query = {
        source: state.query.source,
        order_by: state.query.order_by
      };
      const observation = action.observation;
      if ( observation.taxon ) {
        let indexOfTaxonInAncestors = observation.taxon.ancestor_ids.indexOf( observation.taxon.id );
        if ( indexOfTaxonInAncestors < 0 ) {
          indexOfTaxonInAncestors = observation.taxon.ancestor_ids.length;
        }
        if ( observation.taxon.rank_level === 10 ) {
          newState.query.taxon_id =
            observation.taxon.ancestor_ids[Math.max( indexOfTaxonInAncestors - 1, 0 )];
        } else if ( observation.taxon.rank_level < 10 ) {
          newState.query.taxon_id =
            observation.taxon.ancestor_ids[Math.max( indexOfTaxonInAncestors - 2, 0 )];
        } else {
          newState.query.taxon_id = observation.taxon.id;
        }
      }
      let placeIDs;
      if ( observation.private_place_ids && observation.private_place_ids.length > 0 ) {
        placeIDs = observation.private_place_ids;
      } else {
        placeIDs = observation.place_ids;
      }
      if ( observation.places ) {
        const place = _
          .sortBy( observation.places, p => p.bbox_area )
          .find( p => p.admin_level !== null && p.admin_level < 3 );
        if ( place ) {
          newState.query.place_id = place.id;
          newState.query.place = place;
          newState.query.defaultPlace = place;
        }
      } else if ( placeIDs && placeIDs.length > 0 ) {
        newState.query.place_id = placeIDs[placeIDs.length - 1];
      }
      newState.detailTaxon = null;
      newState.detailPhotoIndex = 0;
      newState.observation = observation;
      // Don't use the current observation when making suggestions based on nearby
      // observations
      newState.query.featured_observation_id = observation.id;
      break;
    }
    case UPDATE_CURRENT_OBSERVATION: {
      if ( action.updates.places ) {
        const place = _
          .sortBy( action.updates.places, p => p.bbox_area )
          .find( p => p.admin_level !== null && p.admin_level < 3 );
        newState.query.place_id = place.id;
        newState.query.place = place;
        newState.query.defaultPlace = place;
{'fraction_non_alphanumeric': 0.10097278660263515, 'fraction_numerical': 0.0039404014283955175, 'mean_word_length': 2.6242748772869255, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '3745473', 'n_tokens_mistral': 2608, 'n_tokens_neox': 2508, 'n_words': 474}