<commit_before>import React, { Component } from 'react'
import { RIEToggle, RIEInput, RIETextArea, REINumber, RIETags, RIESelect } from 'riek'
import { Route } from 'react-router'
import DatePicker from 'react-datepicker'
import WhoAmI from './WhoAmI'
import moment from 'moment'
import firebase from 'APP/fire'
// const auth = firebase.auth()
const db = firebase.database()

// WHEN CAN YOU DEFINE THE UID?  WE NEED TO GRAB AS SOON AS POSSIBLE
// receives  userRef={db.ref('users')} auth={auth} from container

export default class InlineBuddyEdit extends Component {
  constructor(props) {
    super(props)
    // in the future instead of e-mail add an additional field for preferred contact info
    // set scope on OAuth request and include phone number
    console.log('inline buddy edit props', this.props.currentUser, this.props)
    this.state = {
      uid: '',
      name: 'Please enter your name',
      email: 'no email',
      status: { id: '1', text: 'Invited' },
      statusOptions: [
        { id: '1', text: 'Invited' },
        { id: '2', text: 'Going' },
        { id: '3', text: 'Can\'t make it' }
      ],
      homeBase: 'Please enter your city',
      startDate: '',
      endDate:  ''


    }

    // this.state = {
    //   uid: '',
    //   name: this.props.auth.currentUser ? this.props.currentUser.name : 'Please enter your name',
    //   email: this.props.auth.currentUser ? this.props.auth.currentUser.email : 'no email',
    //   status: { id: '1', text: 'Invited' },
    //   statusOptions: [
    //     { id: '1', text: 'Invited' },
    //     { id: '2', text: 'Going' },
    //     { id: '3', text: 'Can\'t make it' }
    //   ],
    //   homeBase: this.props.auth.currentUser ? this.props.currentUser.homeBase :'Please enter your city',
    //   startDate: this.props.auth.currentUser ? this.props.currentUser.startDate : '',
    //   endDate: this.props.auth.currentUser ? this.props.currentUser.endDate : ''
    // }
  }

  componentDidMount() {
    // When the component mounts, start listening to the userRef
    // we were given.
    this.props.auth.onAuthStateChanged(user => {
      this.setState({
        uid: user.uid,
        email: user.email
      })
    })
    this.listenTo(this.props.userRef.child('/' + this.state.uid))
  }

  componentWillUnmount() {
    // When we unmount, stop listening.
    this.unsubscribe()
  }

  componentWillReceiveProps(incoming, outgoing) {
    // When the props sent to us by our parent component change,
    // start listening to the new firebase reference.
    this.listenTo(incoming.userRef)
  }

  listenTo(userRef) {
    // If we're already listening to a ref, stop listening there.
    if (this.unsubscribe) this.unsubscribe()
    // Whenever our ref's value changes, set {value} on our state.
    const listener = firebase.database()
                    .ref('users/' + this.state.uid)
                    .on('value', snapshot => {
                      this.setState(snapshot.val()[this.state.uid])
                    })
    // Set unsubscribe to be a function that detaches the listener.
    this.unsubscribe = () => userRef.off('value', listener)
  }

  virtualServerCallback = (newState) => {
    this.setState(newState)
    // this.updateDb()
  }

  postUserInfoToDB = (e) => {
    e.preventDefault()
    const uid = this.props.auth.currentUser.uid
    console.log('state', this.state)
    const startDate = this.validateDate(this.state.startDate)
    const endDate = this.validateDate(this.state.endDate)
    console.log('end date', endDate)
    this.props.userRef.child(uid).set({
      name: this.state.name,
      homeBase: this.state.homeBase,
      status: this.state.status,
      startDate: startDate,
      endDate: endDate
      // endDate: this.state.endDate ? this.state.endDate.toJSON() : null
    })
  }

  validateDate = (date) => {
    console.log('date', date)
    if (!date) return null
    else if (typeof date === 'string') return date
    else return date.toJSON()
  }

  handleChangeStart = (startDate) => {
    this.setState({
      startDate: startDate
    })
  }
  handleChangeEnd = (endDate) => {
    this.setState({
      endDate: endDate
    })
  }

  render() {
    return (<commit_msg>requiring name and home base fields<commit_after>import React, { Component } from 'react'
import { RIEToggle, RIEInput, RIETextArea, REINumber, RIETags, RIESelect } from 'riek'
import { Route } from 'react-router'
import DatePicker from 'react-datepicker'
import WhoAmI from './WhoAmI'
import moment from 'moment'
import firebase from 'APP/fire'
// const auth = firebase.auth()
const db = firebase.database()

// WHEN CAN YOU DEFINE THE UID?  WE NEED TO GRAB AS SOON AS POSSIBLE
// receives  userRef={db.ref('users')} auth={auth} from container

export default class InlineBuddyEdit extends Component {
  constructor(props) {
    super(props)
    // in the future instead of e-mail add an additional field for preferred contact info
    // set scope on OAuth request and include phone number
    console.log('inline buddy edit props', this.props.currentUser, this.props)
    this.state = {
      uid: '',
      name: 'Please enter your name',
      email: 'no email',
      status: { id: '1', text: 'Invited' },
      statusOptions: [
        { id: '1', text: 'Invited' },
        { id: '2', text: 'Going' },
        { id: '3', text: 'Can\'t make it' }
      ],
      homeBase: 'Please enter your city',
      startDate: '',
      endDate:  ''


    }

    // this.state = {
    //   uid: '',
    //   name: this.props.auth.currentUser ? this.props.currentUser.name : 'Please enter your name',
    //   email: this.props.auth.currentUser ? this.props.auth.currentUser.email : 'no email',
    //   status: { id: '1', text: 'Invited' },
    //   statusOptions: [
    //     { id: '1', text: 'Invited' },
    //     { id: '2', text: 'Going' },
    //     { id: '3', text: 'Can\'t make it' }
    //   ],
    //   homeBase: this.props.auth.currentUser ? this.props.currentUser.homeBase :'Please enter your city',
    //   startDate: this.props.auth.currentUser ? this.props.currentUser.startDate : '',
    //   endDate: this.props.auth.currentUser ? this.props.currentUser.endDate : ''
    // }
  }

  componentDidMount() {
    // When the component mounts, start listening to the userRef
    // we were given.
    this.props.auth.onAuthStateChanged(user => {
      this.setState({
        uid: user.uid,
        email: user.email
      })
    })
    this.listenTo(this.props.userRef.child('/' + this.state.uid))
  }

  componentWillUnmount() {
    // When we unmount, stop listening.
    this.unsubscribe()
  }

  componentWillReceiveProps(incoming, outgoing) {
    // When the props sent to us by our parent component change,
    // start listening to the new firebase reference.
    this.listenTo(incoming.userRef)
  }

  listenTo(userRef) {
    // If we're already listening to a ref, stop listening there.
    if (this.unsubscribe) this.unsubscribe()
    // Whenever our ref's value changes, set {value} on our state.
    const listener = firebase.database()
                    .ref('users/' + this.state.uid)
                    .on('value', snapshot => {
                      this.setState(snapshot.val()[this.state.uid])
                    })
    // Set unsubscribe to be a function that detaches the listener.
    this.unsubscribe = () => userRef.off('value', listener)
  }

  virtualServerCallback = (newState) => {
    this.setState(newState)
    // this.updateDb()
  }

  postUserInfoToDB = (e) => {
    e.preventDefault()
    const uid = this.props.auth.currentUser.uid
    this.props.userRef.child(uid).set({
      name: this.state.name ? this.state.name : 'Please enter your name',
      homeBase: this.state.homeBase ? this.state.homeBase : 'Please enter homebase',
      status: this.state.status,
      startDate: this.validateDate(this.state.startDate),
      endDate: this.validateDate(this.state.endDate)
      // endDate: this.state.endDate ? this.state.endDate.toJSON() : null
    })
  }

  validateDate = (date) => {
    console.log('date', date)
    if (!date) return null
    else if (typeof date === 'string') return date
    else return date.toJSON()
  }

  handleChangeStart = (startDate) => {
    this.setState({
      startDate: startDate
    })
  }
  handleChangeEnd = (endDate) => {
    this.setState({
      endDate: endDate
    })
  }

  render() {
    return (
{'fraction_non_alphanumeric': 0.13857499702628762, 'fraction_numerical': 0.001903175924824551, 'mean_word_length': 2.5342580916351407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '953294', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2601, 'n_words': 683}