|
| 1 | +import { computed } from 'mobx'; |
| 2 | +import { observer } from 'mobx-react'; |
| 3 | +import { ObservedComponent } from 'mobx-react-helper'; |
| 4 | +import dynamic from 'next/dynamic'; |
| 5 | +import { MarkerMeta, OpenReactMapProps } from 'open-react-map'; |
| 6 | + |
| 7 | +import { OrganizationStatistic } from '../../models/Organization'; |
| 8 | +import systemStore from '../../models/System'; |
| 9 | + |
| 10 | +const ChinaMap = dynamic(() => import('./ChinaMap'), { ssr: false }); |
| 11 | + |
| 12 | +export interface CityStatisticMapProps { |
| 13 | + data: OrganizationStatistic['coverageArea']; |
| 14 | + onChange?: (city: string) => any; |
| 15 | +} |
| 16 | + |
| 17 | +@observer |
| 18 | +export class CityStatisticMap extends ObservedComponent<CityStatisticMapProps> { |
| 19 | + componentDidMount() { |
| 20 | + systemStore.getCityCoordinate(); |
| 21 | + } |
| 22 | + |
| 23 | + @computed |
| 24 | + get markers() { |
| 25 | + const { cityCoordinate } = systemStore, |
| 26 | + { data } = this.observedProps; |
| 27 | + |
| 28 | + return Object.entries(data) |
| 29 | + .map(([city, count]) => { |
| 30 | + const point = cityCoordinate[city]; |
| 31 | + |
| 32 | + if (point) |
| 33 | + return { |
| 34 | + tooltip: `${city} ${count}`, |
| 35 | + position: [point[1], point[0]], |
| 36 | + }; |
| 37 | + }) |
| 38 | + .filter(Boolean) as MarkerMeta[]; |
| 39 | + } |
| 40 | + |
| 41 | + handleChange: OpenReactMapProps['onMarkerClick'] = ({ latlng: { lat, lng } }) => { |
| 42 | + const { markers } = this; |
| 43 | + const { tooltip } = |
| 44 | + markers.find(({ position: p }) => p instanceof Array && lat === p[0] && lng === p[1]) || {}; |
| 45 | + const [city] = tooltip?.split(/\s+/) || []; |
| 46 | + |
| 47 | + this.props.onChange?.(city); |
| 48 | + }; |
| 49 | + |
| 50 | + render() { |
| 51 | + const { markers } = this; |
| 52 | + |
| 53 | + return ( |
| 54 | + <ChinaMap |
| 55 | + style={{ height: '70vh' }} |
| 56 | + center={[34.32, 108.55]} |
| 57 | + zoom={4} |
| 58 | + markers={markers} |
| 59 | + onMarkerClick={this.handleChange} |
| 60 | + /> |
| 61 | + ); |
| 62 | + } |
| 63 | +} |
0 commit comments