import React, { Component } from 'react'; import { NavBar, SearchBar, WhiteSpace, List, Toast, Icon } from 'antd-mobile'; import { SearchMask, IndexNav, Indicator, PositionCity, SearchArea } from '../../components'; import { getLocalCity } from '../../services/locationServices'; import { LASTEST_CITY, getAllCities, saveLocalStorageCity, getLocalStorageCity, searchCityByName, transformCityMenuData } from '../../services/cityServices'; import { throttle } from '../../utils'; import './city.css'; const Item = List.Item; const searchCity = throttle(searchCityByName); class City extends Component { constructor(props) { super(props); this.state = { localCity: '北京', hotCity: [], latestCity: [], city: {}, labels: [], searchCities: [], moving: false, indicator: '', loading: false, searchArea: false }; } componentDidMount = () => { this.setState(() => { Toast.loading('Loading...'); return { loading: true }; }); Promise.all([getLocalCity(), getAllCities(), getLocalStorageCity(LASTEST_CITY)]).then(result => { const localCity = result[0] && result[0].replace(/市/, ''); const city = result[1].afterFilterAllCity.allCity; const labels = result[1].afterFilterAllCity.validateLetters; const hotCity = result[1].hotCity; const latestCity = result[2]; this.setState(() => { Toast.hide(); return { localCity, city, hotCity, labels, latestCity, loading: false }; }); }); } onSearchInput = async value => { if (!value) { this.hideMenuDialog(); return; } const { labels, city } = this.state; const cities = await searchCity(value, labels, city); this.setState({ searchArea: true, searchCities: transformCityMenuData(cities) }); } hideMenuDialog = () => { this.setState({ searchArea: false, searchCities: [] }); } renderLocalCity = () => { const { localCity, latestCity } = this.state; return } renderHotCity = () => { const { hotCity } = this.state; return } renderCities = () => { const { city } = this.state; return Object.keys(city).map(letter => { const cList = city[letter]; return (
this[`section${letter}`] = element }> letter } className="select-city-list"> { cList.length > 0 && cList.map(c => { return this.onSelectCity(c) }>{ c.city } }) }
); }); } renderSearchCity = () => { const { searchCities } = this.state; return } onChangeMenu = value => { this.hideMenuDialog(); this.onSelectCity(value[0]); } onSelectCity = (city) => { if (this.state.moving) { return; } const { history, location } = this.props; const { cityCode, ...rest } = location.state; const backCity = typeof city === 'string' ? city : city.city; rest[`city${cityCode}`] = backCity; saveLocalStorageCity(LASTEST_CITY, backCity); history.push({ pathname: '/', state: { ...rest } }); } onNavChange = (nav) => { this.setState(() => { const label = nav.label ? nav.label : this.state.label; const moving = nav.moving ? nav.moving : this.state.moving; this[`section${label}`] && this[`section${label}`].scrollIntoView(); return { moving, indicator: label } }); } render() { const { labels, indicator, moving, loading, searchArea } = this.state; if (loading) { return null; } return (
} leftContent="返回" onLeftClick={ () => this.props.history.goBack() } >选择城市
{ searchArea && this.renderSearchCity() }
{ this.renderLocalCity() } { this.renderHotCity() } { this.renderCities() }
); } } export default City;