1 | import React from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 | import {
|
4 | compose,
|
5 | defaultProps,
|
6 | withHandlers,
|
7 | withState,
|
8 | withContext,
|
9 | withProps,
|
10 | withPropsOnChange,
|
11 | } from 'recompose';
|
12 | import { createSelector } from 'reselect';
|
13 |
|
14 | import {
|
15 | susolvkaCoords,
|
16 | generateMarkers,
|
17 | heatmapData,
|
18 | generateHeatmapData,
|
19 | } from './data/fakeData';
|
20 |
|
21 | import GoogleMapReact from '../src';
|
22 | import SimpleMarker from './markers/SimpleMarker';
|
23 |
|
24 | import ptInBounds from './utils/ptInBounds';
|
25 | import withStateSelector from './utils/withStateSelector';
|
26 | import { GOOGLE_API_KEY } from './config/Google_API_key';
|
27 | import withSafeInterval from './utils/withSafeInterval';
|
28 |
|
29 | export const gMapHeatmap = (
|
30 | {
|
31 | style,
|
32 | hoverDistance,
|
33 | options,
|
34 | heatmap,
|
35 | mapParams: { center, zoom },
|
36 | onChange,
|
37 | onChildMouseEnter,
|
38 | onChildMouseLeave,
|
39 | markers,
|
40 | draggable,
|
41 | }
|
42 | ) => (
|
43 | <GoogleMapReact
|
44 | bootstrapURLKeys={{
|
45 | key: GOOGLE_API_KEY,
|
46 | }}
|
47 | style={style}
|
48 | options={options}
|
49 | draggable={draggable}
|
50 | hoverDistance={hoverDistance}
|
51 | zoom={zoom}
|
52 | center={center}
|
53 | onChange={onChange}
|
54 | onChildMouseEnter={onChildMouseEnter}
|
55 | onChildMouseLeave={onChildMouseLeave}
|
56 | heatmap={heatmap}
|
57 | heatmapLibrary
|
58 | >
|
59 | {markers}
|
60 | </GoogleMapReact>
|
61 | );
|
62 |
|
63 | export const gMapHOC = compose(
|
64 | defaultProps({
|
65 | clusterRadius: 60,
|
66 | hoverDistance: 30,
|
67 | options: {
|
68 | minZoom: 3,
|
69 | maxZoom: 15,
|
70 | },
|
71 | style: {
|
72 | position: 'relative',
|
73 | margin: 10,
|
74 | padding: 10,
|
75 | flex: 1,
|
76 | },
|
77 | }),
|
78 | withContext({ hello: PropTypes.string }, () => ({ hello: 'world' })),
|
79 |
|
80 | withStateSelector('markers', 'setMarkers', () =>
|
81 | createSelector(
|
82 | ({ route: { markersCount = 20 } }) => markersCount,
|
83 | markersCount => generateMarkers(markersCount)
|
84 | )),
|
85 | withState('hoveredMarkerId', 'setHoveredMarkerId', -1),
|
86 | withState('mapParams', 'setMapParams', { center: susolvkaCoords, zoom: 6 }),
|
87 | withSafeInterval,
|
88 | withState('heatmap', 'setHeatmap', heatmapData),
|
89 |
|
90 | withHandlers({
|
91 | onChange: ({ setMapParams, setSafeInterval, setHeatmap, mapParams }) =>
|
92 | ({ center, zoom, bounds }) => {
|
93 | setMapParams({ center, zoom, bounds });
|
94 | const boundSetHeatmap = setHeatmap.bind(this);
|
95 | setSafeInterval(
|
96 | () => {
|
97 | boundSetHeatmap(
|
98 | generateHeatmapData(mapParams.center.lat, mapParams.center.lng)
|
99 | );
|
100 | },
|
101 | 3000
|
102 | );
|
103 | },
|
104 | onChildMouseEnter: ({ setHoveredMarkerId }) =>
|
105 | (hoverKey, { id }) => {
|
106 | setHoveredMarkerId(id);
|
107 | },
|
108 | onChildMouseLeave: ({ setHoveredMarkerId }) =>
|
109 | () => {
|
110 | setHoveredMarkerId(-1);
|
111 | },
|
112 | }),
|
113 | withPropsOnChange(['markers', 'mapParams'], ({
|
114 | markers,
|
115 | mapParams: { bounds },
|
116 | }) => ({
|
117 | markers: bounds ? markers.filter(m => ptInBounds(bounds, m)) : [],
|
118 | })),
|
119 | withProps(({ hoveredMarkerId }) => ({
|
120 | draggable: hoveredMarkerId === -1,
|
121 | })),
|
122 | withPropsOnChange(['markers'], ({ markers }) => ({
|
123 | markers: markers.map(({ ...markerProps, id }) => (
|
124 | <SimpleMarker key={id} id={id} {...markerProps} />
|
125 | )),
|
126 | }))
|
127 | );
|
128 |
|
129 | export default gMapHOC(gMapHeatmap);
|