<commit_before>import React, { Component } from 'react';
import TextField from 'material-ui/TextField';
import FlatButton from 'material-ui/FlatButton';
import FloatingActionButton from 'material-ui/FloatingActionButton';
import firebase, { auth } from '../../javascripts/firebase';
import storage from '../../javascripts/firebase-storage';
// import UserProfile from './UserProfile'; // New.

import SightingFormSubmit from './SightingFormSubmit';
import SightingFormSelect from './SightingFormSelect';
import IconPhoto from '../IconPhoto';

import store from '../../store/configureStore';

class SightingForm extends Component {
  constructor(props) {
    super(props);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.handleChange = this.handleChange.bind(this);
    this.uploadFile = this.uploadFile.bind(this);
    this.writeSpecies = this.writeSpecies.bind(this);
    this.getPosition = this.getPosition.bind(this);
    this.showPosition = this.showPosition.bind(this);
    this.state = {
      scientificName: '',
      count: '',
      sex: '',
      lat: '',
      lon: '',
      photoURL: null,
      currentTime: null
    };
  }

  showPosition(position) {
    this.setState({
      lat: position.coords.latitude,
      lon: position.coords.longitude
    });
  }

  getPosition() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(this.showPosition);
    } else {
      this.props.handleNotification('Geolocation is not supported by this browser.');
    }
  }

  writeSpecies() {
    let sp = this.state.scientificName;

    let genus = sp.split(/ |\./)[0];
    let species = sp.split(/ |\./)[1] || 'NA';
    let snap;
    firebase
      .database()
      .ref()
      .child('species/' + genus)
      .once('value')
      .then(snap => {
        snap = snap.val();
        if (!snap) {
          let newSpecies = firebase
            .database()
            .ref('species')
            .child(genus);
          newSpecies.set({
            rawData: sp,
            genus: genus,
            species: species
          });
        }
      });
  }

  uploadFile(e) {
    // console.log('Photo uploading...', e.target.files); // Debugging.
    this.photoURL = e.target.files[0];
  }

  handleSubmit(e) {
    e.preventDefault();
    let newSightingRef = firebase
      .database()
      .ref('sightings')
      .child(auth.currentUser.uid);
    let newSightingKey = firebase
      .database()
      .ref('sightings')
      .child(auth.currentUser.uid)
      .push().key;
    let file = this.photoURL || null;
    if (file && !file.type.match('image.*')) {<commit_msg>Add separation of genus vs species in Species node<commit_after>import React, { Component } from 'react';
import TextField from 'material-ui/TextField';
import FlatButton from 'material-ui/FlatButton';
import FloatingActionButton from 'material-ui/FloatingActionButton';
import firebase, { auth } from '../../javascripts/firebase';
import storage from '../../javascripts/firebase-storage';
// import UserProfile from './UserProfile'; // New.

import SightingFormSubmit from './SightingFormSubmit';
import SightingFormSelect from './SightingFormSelect';
import IconPhoto from '../IconPhoto';

import store from '../../store/configureStore';

class SightingForm extends Component {
  constructor(props) {
    super(props);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.handleChange = this.handleChange.bind(this);
    this.uploadFile = this.uploadFile.bind(this);
    this.writeSpecies = this.writeSpecies.bind(this);
    this.getPosition = this.getPosition.bind(this);
    this.showPosition = this.showPosition.bind(this);
    this.state = {
      scientificName: '',
      count: '',
      sex: '',
      lat: '',
      lon: '',
      photoURL: null,
      currentTime: null
    };
  }

  showPosition(position) {
    this.setState({
      lat: position.coords.latitude,
      lon: position.coords.longitude
    });
  }

  getPosition() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(this.showPosition);
    } else {
      this.props.handleNotification('Geolocation is not supported by this browser.');
    }
  }

  writeSpecies() {
    let sp = this.state.scientificName;

    let genus = sp.split(/ |\./)[0];
    let species = sp.split(/ |\./)[1];
    let snap;

    if (species !== null) {
      firebase
        .database()
        .ref()
        .child('species/' + genus)
        .child(species)
        .once('value')
        .then(snap => {
          snap = snap.val();
          if (!snap) {
            let newSpecies = firebase
              .database()
              .ref('species')
              .child(genus)
              .child(species);
            newSpecies.set({
              rawData: sp,
              genus: genus,
              species: species
            });
          }
        });
    } else {
      firebase
        .database()
        .ref()
        .child('species/' + genus)
        .once('value')
        .then(snap => {
          snap = snap.val();
          if (!snap) {
            let newSpecies = firebase
              .database()
              .ref('species')
              .child(genus);
            newSpecies.set({
              rawData: sp,
              genus: genus,
              species: species || 'NA'
            });
          }
        });
    }

    // SAVE original method: storing to single branch:
    // firebase
    //   .database()
    //   .ref()
    //   .child('species/' + genus)
    //   .once('value')
    //   .then(snap => {
    //     snap = snap.val();
    //     if (!snap) {
    //       let newSpecies = firebase
    //         .database()
    //         .ref('species')
    //         .child(genus);
    //       newSpecies.set({
    //         rawData: sp,
    //         genus: genus,
    //         species: species
    //       });
    //     }
    //   });
  }

  uploadFile(e) {
    // console.log('Photo uploading...', e.target.files); // Debugging.
    this.photoURL = e.target.files[0];
  }

  handleSubmit(e) {
    e.preventDefault();
    let newSightingRef = firebase
      .database()
      .ref('sightings')
      .child(auth.currentUser.uid);
    let newSightingKey = firebase
      .database()
      .ref('sightings')
      .child(auth.currentUser.uid)
      .push().key;
    let file = this.photoURL || null;
    if (file && !file.type.match('image.*')) {
{'fraction_non_alphanumeric': 0.14884663341645885, 'fraction_numerical': 0.0009351620947630922, 'mean_word_length': 2.016925246826516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '7239741', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1893, 'n_words': 391}