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 { londonCoords, generateMarkers } from './data/fakeData';
|
15 |
|
16 | import GoogleMapReact from '../src';
|
17 | import SimpleMarker from './markers/SimpleMarker';
|
18 |
|
19 | import ptInBounds from './utils/ptInBounds';
|
20 | import withStateSelector from './utils/withStateSelector';
|
21 | import { GOOGLE_API_KEY } from './config/Google_API_key';
|
22 |
|
23 | export const gMap = (
|
24 | {
|
25 | style,
|
26 | hoverDistance,
|
27 | options,
|
28 | mapParams: { center, zoom },
|
29 | onChange,
|
30 | onChildMouseEnter,
|
31 | onChildMouseLeave,
|
32 | markers,
|
33 | draggable,
|
34 | }
|
35 | ) => (
|
36 | <GoogleMapReact
|
37 | bootstrapURLKeys={{
|
38 | key: GOOGLE_API_KEY,
|
39 | }}
|
40 | style={style}
|
41 | options={options}
|
42 | draggable={draggable}
|
43 | hoverDistance={hoverDistance}
|
44 | zoom={zoom}
|
45 | center={center}
|
46 | onChange={onChange}
|
47 | onChildMouseEnter={onChildMouseEnter}
|
48 | onChildMouseLeave={onChildMouseLeave}
|
49 | layerTypes={
|
50 | zoom > 12
|
51 | ? []
|
52 | : zoom > 10 ? ['TrafficLayer'] : ['TrafficLayer', 'TransitLayer']
|
53 | }
|
54 | >
|
55 | {markers}
|
56 | </GoogleMapReact>
|
57 | );
|
58 |
|
59 | export const gMapHOC = compose(
|
60 | defaultProps({
|
61 | clusterRadius: 60,
|
62 | hoverDistance: 30,
|
63 | options: {
|
64 | minZoom: 3,
|
65 | maxZoom: 15,
|
66 | },
|
67 | style: {
|
68 | position: 'relative',
|
69 | margin: 0,
|
70 | padding: 0,
|
71 | flex: 1,
|
72 | },
|
73 | }),
|
74 | withContext({ hello: PropTypes.string }, () => ({ hello: 'world' })),
|
75 |
|
76 | withStateSelector('markers', 'setMarkers', () =>
|
77 | createSelector(
|
78 | ({ route: { markersCount = 20 } }) => markersCount,
|
79 | markersCount => generateMarkers(markersCount)
|
80 | )),
|
81 | withState('hoveredMarkerId', 'setHoveredMarkerId', -1),
|
82 | withState('mapParams', 'setMapParams', { center: londonCoords, zoom: 9 }),
|
83 |
|
84 | withHandlers({
|
85 | onChange: ({ setMapParams }) =>
|
86 | ({ center, zoom, bounds }) => {
|
87 | setMapParams({ center, zoom, bounds });
|
88 | },
|
89 | onChildMouseEnter: ({ setHoveredMarkerId }) =>
|
90 | (hoverKey, { id }) => {
|
91 | setHoveredMarkerId(id);
|
92 | },
|
93 | onChildMouseLeave: ({ setHoveredMarkerId }) =>
|
94 | () => {
|
95 | setHoveredMarkerId(-1);
|
96 | },
|
97 | }),
|
98 | withPropsOnChange(['markers', 'mapParams'], ({
|
99 | markers,
|
100 | mapParams: { bounds },
|
101 | }) => ({
|
102 | markers: bounds ? markers.filter(m => ptInBounds(bounds, m)) : [],
|
103 | })),
|
104 | withProps(({ hoveredMarkerId }) => ({
|
105 | draggable: hoveredMarkerId === -1,
|
106 | })),
|
107 | withPropsOnChange(['markers'], ({ markers }) => ({
|
108 | markers: markers.map(({ ...markerProps, id }) => (
|
109 | <SimpleMarker key={id} id={id} {...markerProps} />
|
110 | )),
|
111 | }))
|
112 | );
|
113 |
|
114 | export default gMapHOC(gMap);
|