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 | 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 | >
|
50 | {markers}
|
51 | </GoogleMapReact>
|
52 | );
|
53 |
|
54 | export const gMapHOC = compose(
|
55 | defaultProps({
|
56 | clusterRadius: 60,
|
57 | hoverDistance: 30,
|
58 | options: {
|
59 | minZoom: 3,
|
60 | maxZoom: 15,
|
61 | },
|
62 | style: {
|
63 | position: 'relative',
|
64 | margin: 0,
|
65 | padding: 0,
|
66 | flex: 1,
|
67 | },
|
68 | }),
|
69 | withContext({ hello: PropTypes.string }, () => ({ hello: 'world' })),
|
70 |
|
71 | withStateSelector('markers', 'setMarkers', () =>
|
72 | createSelector(
|
73 | ({ route: { markersCount = 20 } }) => markersCount,
|
74 | markersCount => generateMarkers(markersCount)
|
75 | )),
|
76 | withState('hoveredMarkerId', 'setHoveredMarkerId', -1),
|
77 | withState('mapParams', 'setMapParams', { center: susolvkaCoords, zoom: 6 }),
|
78 |
|
79 | withHandlers({
|
80 | onChange: ({ setMapParams }) =>
|
81 | ({ center, zoom, bounds }) => {
|
82 | setMapParams({ center, zoom, bounds });
|
83 | },
|
84 | onChildMouseEnter: ({ setHoveredMarkerId }) =>
|
85 | (hoverKey, { id }) => {
|
86 | setHoveredMarkerId(id);
|
87 | },
|
88 | onChildMouseLeave: ({ setHoveredMarkerId }) =>
|
89 | () => {
|
90 | setHoveredMarkerId(-1);
|
91 | },
|
92 | }),
|
93 | withPropsOnChange(['markers', 'mapParams'], ({
|
94 | markers,
|
95 | mapParams: { bounds },
|
96 | }) => ({
|
97 | markers: bounds ? markers.filter(m => ptInBounds(bounds, m)) : [],
|
98 | })),
|
99 | withProps(({ hoveredMarkerId }) => ({
|
100 | draggable: hoveredMarkerId === -1,
|
101 | })),
|
102 | withPropsOnChange(['markers'], ({ markers }) => ({
|
103 | markers: markers.map(({ ...markerProps, id }) => (
|
104 | <SimpleMarker key={id} id={id} {...markerProps} />
|
105 | )),
|
106 | }))
|
107 | );
|
108 |
|
109 | export default gMapHOC(gMap);
|