78 lines
1.9 KiB
React
Raw Normal View History

2018-05-17 15:13:53 +02:00
import hash from 'object-hash'
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-25 18:40:07 +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-17 15:13:53 +02:00
componentDidUpdate(prevProps) {
if (prevProps.activity.id !==
this.props.activity.id) {
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() {
const { activity, gpxContent } = this.props
const { jsonData } = getGeoJson(gpxContent)
const bounds = [
[activity.bounds[0], activity.bounds[1]],
[activity.bounds[2], activity.bounds[3]]
]
2018-05-03 21:42:54 +02:00
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}
2018-05-16 23:52:55 +02:00
boundsOptions={{ padding: [10, 10] }}
2018-05-03 21:42:54 +02:00
>
<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}`}
/>
2018-05-17 15:13:53 +02:00
<GeoJSON
// hash as a key to force re-rendering
key={hash(jsonData)}
data={jsonData}
/>
2018-05-03 21:42:54 +02:00
</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)