62 lines
1.5 KiB
React
Raw Normal View History

2018-05-03 14:07:31 +02:00
import React from 'react'
2018-05-03 21:42:54 +02:00
import { GeoJSON, Map, TileLayer } from 'react-leaflet'
import { connect } from 'react-redux'
2018-05-03 14:07:31 +02:00
2018-05-03 21:42:54 +02:00
import { getActivityGpx } from '../../actions/activities'
import { getGeoJson, thunderforestApiKey } from '../../utils'
2018-05-03 14:07:31 +02:00
2018-05-03 21:42:54 +02:00
class ActivityMap extends React.Component {
2018-05-03 14:07:31 +02:00
constructor(props, context) {
super(props, context)
this.state = {
2018-05-03 21:42:54 +02:00
zoom: 13,
2018-05-03 14:07:31 +02:00
}
}
2018-05-03 21:42:54 +02:00
componentDidMount() {
this.props.loadActivityGpx(this.props.activity.id)
}
2018-05-04 22:30:11 +02:00
componentWillUnmount() {
this.props.loadActivityGpx(null)
}
2018-05-03 14:07:31 +02:00
render() {
2018-05-03 21:42:54 +02:00
const { gpxContent } = this.props
const { jsonData, bounds } = getGeoJson(gpxContent)
2018-05-03 14:07:31 +02:00
return (
2018-05-03 21:42:54 +02:00
<div>
{jsonData && (
<Map
zoom={this.state.zoom}
bounds={bounds}
boundsOptions={{ padding: [50, 50] }}
>
<TileLayer
// eslint-disable-next-line max-len
attribution='&copy; <a href="http://www.thunderforest.com/">Thunderforest</a>, &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
// eslint-disable-next-line max-len
url={`https://{s}.tile.thunderforest.com/outdoors/{z}/{x}/{y}.png?apikey=${thunderforestApiKey}`}
/>
<GeoJSON data={jsonData} />
</Map>
)}
</div>
2018-05-03 14:07:31 +02:00
)
}
}
2018-05-03 21:42:54 +02:00
export default connect(
state => ({
gpxContent: state.gpx
}),
dispatch => ({
loadActivityGpx: activityId => {
dispatch(getActivityGpx(activityId))
},
})
)(ActivityMap)