import React, {Component} from 'react'
import AddressPicker from 'components/address_picker'
import Field from 'components/field'
import auto_bind from 'common/auto_bind'

export default class AddressField extends Component {
  constructor(props) {
    super(props)
    auto_bind(this)
  }

  static defaultProps = {
    show_label: true,
    update() {},
  }

  on_place_select({label: address, location: {lat: latitude, lng: longitude}}) {
    this.props.update({address, latitude, longitude})
  }

  on_clear_place_input() {
    this.props.update({address: ""})
  }

  render() {
    const {address} = this.props
    return (
      <Field {...this.props} category="address">
        <AddressPicker
          initial_query={address}
          on_place_select={this.on_place_select}
          on_clear_place_input={this.on_clear_place_input}
        />
      </Field>
    )
  }
}
