jam-cloud/web/app/assets/javascripts/react-components/SelectLocation.js.jsx.coffee

108 lines
3.1 KiB
CoffeeScript
Raw Permalink Normal View History

2016-01-28 19:55:17 +00:00
context = window
rest = window.JK.Rest()
logger = context.JK.logger
@SelectLocation = React.createClass({
mixins: [Reflux.listenTo(@LocationStore, "onLocationsChanged")]
2016-01-28 19:55:17 +00:00
propTypes: {
onItemChanged: React.PropTypes.func.isRequired
}
getInitialState: () ->
{selectedCountry: null, countries: LocationStore.countries || {US: {name: 'United States', regions: []}}}
2016-01-28 19:55:17 +00:00
onLocationsChanged: (countries) ->
@setState({countries: countries})
onCountryChanged: (e) ->
val = $(e.target).val()
@changed(val, null, null)
@setState({selectedCountry: val, selectedRegion: null, selectedCity: null})
2016-01-28 19:55:17 +00:00
if val?
LocationActions.selectCountry(val)
onRegionChanged: (e) ->
val = $(e.target).val()
@changed(this.currentCountry(), val, null)
@setState({selectedRegion: val, selectedCity: null})
if val? && this.props.showCity
LocationActions.selectRegion(this.currentCountry(), val)
onCityChanged: (e) ->
val = $(e.target).val()
@changed(this.currentCountry(), this.currentRegion(), val)
@setState({selectedCity: val})
2016-01-28 19:55:17 +00:00
changed: (country, region, city) ->
2016-01-28 19:55:17 +00:00
if country == ''
country = null
if region == ''
region = null
if city == ''
city = null
@props.onItemChanged(country, region, city)
currentCity: () ->
this.state.selectedCity || this.props.selectedCity
currentCountry: () ->
this.state.selectedCountry || this.props.selectedCountry || 'US'
currentRegion: () ->
this.state.selectedRegion || this.props.selectedRegion
defaultText: () ->
if this.props.defaultText?
this.props.defaultText
else
'Any'
2016-01-28 19:55:17 +00:00
render: () ->
countries = [`<option key="" value="">{this.defaultText()}</option>`]
2016-01-28 19:55:17 +00:00
for countryId, countryInfo of @state.countries
countries.push(`<option key={countryId} value={countryId}>{countryInfo.name}</option>`)
2016-01-28 19:55:17 +00:00
country = @state.countries[this.currentCountry()]
regions = [`<option key="" value="">{this.defaultText()}</option>`]
cities = [`<option key="" value="">{this.defaultText()}</option>`]
2016-01-28 19:55:17 +00:00
if country? && country.regions
for region in country.regions
regions.push(`<option key={region.id} value={region.id}>{region.name}</option>`)
2016-01-28 19:55:17 +00:00
if this.currentRegion() == region.id && this.props.showCity
for city in region.cities
cities.push(`<option key={city} value={city}>{city}</option>`)
if !this.props.hideCountry
countryJsx = `<div><h3>Country:</h3>
<select name="countries" onChange={this.onCountryChanged} value={this.currentCountry()}>{countries}</select>
</div>`
2016-01-28 19:55:17 +00:00
disabled = regions.length == 1
if this.props.showCity
cityJsx = `<div><h3>City:</h3>
<select name="cities" disabled={cities.length == 1} onChange={this.onCityChanged} value={this.currentCity()}>{cities}</select>
</div>`
2016-01-28 19:55:17 +00:00
`<div className="SelectLocation">
{countryJsx}
2016-01-28 19:55:17 +00:00
<h3>State/Region:</h3>
<select name="regions" disabled={disabled} onChange={this.onRegionChanged}
value={this.currentRegion()}>{regions}</select>
{cityJsx}
2016-01-28 19:55:17 +00:00
</div>`
})