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