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