<commit_before>import React from 'react-native';
import NavigationBar from 'react-native-navbar';

import CameraDashboard from '../camera/Camera-Dashboard';
import Main from '../main/Main';

import Directions from './Directions';
import RouteOverlay from './Route-Overlay';
import ArrivalOverlay from './Arrival-Overlay';
import Map from './Map';

import mapbox_api from '../../utils/mapbox-api';
import firebase_api from '../../utils/firebase-api';
import { formatNameString } from '../../utils/helpers';

import { findIconForCategory } from '../../utils/filters-data'

var {
  View,
  StyleSheet
} = React;

class MapDashBoard extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      steps: [],
      stepAnnotations: [],
      stepDirections: [],
      stepIndex: -1,
      lastStepIndex: null,
      isLoading: true,
      timetoAnnotation: null,
      isConfirmed: false,
      hasArrived: false
    };
  }

  // [] gets an array of categories for the chosen plate
  componentWillMount() {
    var userCoords = this.props.route.props.userPosition.coords;

    var userPosition = {
      lat: userCoords.latitude,
      lng: userCoords.longitude
    };

    var timeStart = Date.now();

    this.getAsyncDirections(userPosition, this.props.route.props.image.location, this.props.route.props.image.category)
    .then((res) => {
      var wait = 0;
      var timeEnd = Date.now();
      var timeElapsed = timeEnd - timeStart;

      // we get route so fast, make sure we wait at least 1 sec
      if( timeElapsed < 3000 ) {
        wait = 4000 - timeElapsed;
      }

      setTimeout(() => {
        this.setState({
          steps: res.steps,
          stepDirections: res.stepDirections,
          lastStepIndex: res.stepDirections.length-1,
          stepAnnotations: res.stepAnnotations,
          isLoading: false
        });
      }, wait);

    })
    .catch((err) => { console.log(`Problem getting directions: ${err}`); });
  }

  async getAsyncDirections(origin, destination, category) {
    var responseDirections = await (mapbox_api.getDirections(origin, destination)
      .then((data) => {
        console.log('category of dish', category);
        var steps = data.routes[0].steps;
        var url = findIconForCategory(category);
        console.log('url result', url);
        var annotationImage = {
              url: url,
              height: 50,
              width: 50,
            };

        var stepAnnotations = steps.map((step) => {
          var coords = step.maneuver.location.coordinates;
          var latitude = coords[1];
          var longitude = coords[0];
          var title = '';

          if(step.way_name) {
            title = step.way_name.replace(/\s/g, '-');
          } else {
            title = 'arrival';
          }

          return {
            latitude,
            longitude,
            annotationImage,
            title,
            id: title
          }
        });

        var stepDirections = steps.map((step) => {
          if( step.maneuver.instruction === 'You have arrived at your destination' ) {
            return "You're almost there...";
          }

          return step.maneuver.instruction;
        });

        return {
          steps,
          stepAnnotations,
          stepDirections
        };

      }));
    return responseDirections;
  }

  handleRouteConfirmation() {
    this.setState({
      isConfirmed: true
    });
  }

  handleStepIncrement(stepIndex) {
    this.setState({
      stepIndex
    });
  }

  handleLocationChange(timeToAnnotation) {
    this.setState({
      timeToAnnotation
    });
  }

  handleAnnotationChange(timeToAnnotation) {
    this.setState({
      timeToAnnotation
    });
  }

  handleArrival() {
    this.setState({
      hasArrived: true
    });

    firebase_api.addAdventureToUser(this.props.user.id, this.props.route.props.image.img_key);
    return;
  }

  handleArrivalConfirmation() {
    var restaurantName = this.props.route.props.image.restaurant;<commit_msg>[Save] prevent duplicate adventures<commit_after>import React from 'react-native';
import NavigationBar from 'react-native-navbar';

import CameraDashboard from '../camera/Camera-Dashboard';
import Main from '../main/Main';

import Directions from './Directions';
import RouteOverlay from './Route-Overlay';
import ArrivalOverlay from './Arrival-Overlay';
import Map from './Map';

import mapbox_api from '../../utils/mapbox-api';
import firebase_api from '../../utils/firebase-api';
import { formatNameString } from '../../utils/helpers';

import { findIconForCategory } from '../../utils/filters-data'

var {
  View,
  StyleSheet
} = React;

class MapDashBoard extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      steps: [],
      stepAnnotations: [],
      stepDirections: [],
      stepIndex: -1,
      lastStepIndex: null,
      isLoading: true,
      timetoAnnotation: null,
      isConfirmed: false,
      hasArrived: false
    };
  }

  // [] gets an array of categories for the chosen plate
  componentWillMount() {
    var userCoords = this.props.route.props.userPosition.coords;

    var userPosition = {
      lat: userCoords.latitude,
      lng: userCoords.longitude
    };

    var timeStart = Date.now();

    this.getAsyncDirections(userPosition, this.props.route.props.image.location, this.props.route.props.image.category)
    .then((res) => {
      var wait = 0;
      var timeEnd = Date.now();
      var timeElapsed = timeEnd - timeStart;

      // we get route so fast, make sure we wait at least 1 sec
      if( timeElapsed < 3000 ) {
        wait = 4000 - timeElapsed;
      }

      setTimeout(() => {
        this.setState({
          steps: res.steps,
          stepDirections: res.stepDirections,
          lastStepIndex: res.stepDirections.length-1,
          stepAnnotations: res.stepAnnotations,
          isLoading: false
        });
      }, wait);

    })
    .catch((err) => { console.log(`Problem getting directions: ${err}`); });
  }

  async getAsyncDirections(origin, destination, category) {
    var responseDirections = await (mapbox_api.getDirections(origin, destination)
      .then((data) => {
        console.log('category of dish', category);
        var steps = data.routes[0].steps;
        var url = findIconForCategory(category);
        console.log('url result', url);
        var annotationImage = {
              url: url,
              height: 50,
              width: 50,
            };

        var stepAnnotations = steps.map((step) => {
          var coords = step.maneuver.location.coordinates;
          var latitude = coords[1];
          var longitude = coords[0];
          var title = '';

          if(step.way_name) {
            title = step.way_name.replace(/\s/g, '-');
          } else {
            title = 'arrival';
          }

          return {
            latitude,
            longitude,
            annotationImage,
            title,
            id: title
          }
        });

        var stepDirections = steps.map((step) => {
          if( step.maneuver.instruction === 'You have arrived at your destination' ) {
            return "You're almost there...";
          }

          return step.maneuver.instruction;
        });

        return {
          steps,
          stepAnnotations,
          stepDirections
        };

      }));
    return responseDirections;
  }

  handleRouteConfirmation() {
    this.setState({
      isConfirmed: true
    });
  }

  handleStepIncrement(stepIndex) {
    this.setState({
      stepIndex
    });
  }

  handleLocationChange(timeToAnnotation) {
    this.setState({
      timeToAnnotation
    });
  }

  handleAnnotationChange(timeToAnnotation) {
    this.setState({
      timeToAnnotation
    });
  }

  handleArrival() {
    if(this.state.hasArrived) {
      return;
    }

    this.setState({
      hasArrived: true
    });

    firebase_api.addAdventureToUser(this.props.user.id, this.props.route.props.image.img_key);
  }

  handleArrivalConfirmation() {
    var restaurantName = this.props.route.props.image.restaurant;
{'fraction_non_alphanumeric': 0.1224590365898731, 'fraction_numerical': 0.004681532585930763, 'mean_word_length': 2.1489526764934057, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3933918', 'n_tokens_mistral': 2423, 'n_tokens_neox': 2333, 'n_words': 507}