/
edit.js
263 lines (256 loc) · 7.54 KB
/
edit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/** @format */
/**
* External dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { Component, createRef, Fragment, RawHTML } from '@wordpress/element';
import {
Button,
ButtonGroup,
IconButton,
PanelBody,
Placeholder,
Spinner,
Toolbar,
TextControl,
withNotices,
} from '@wordpress/components';
import {
InspectorControls,
BlockControls,
BlockAlignmentToolbar,
PanelColorSettings,
} from '@wordpress/editor';
import apiFetch from '@wordpress/api-fetch';
import { debounce, clone } from 'lodash';
/**
* Internal dependencies
*/
import { settings } from './settings.js';
import AddPoint from './add-point';
import Locations from './locations';
import Map from './component.js';
import MapThemePicker from './map-theme-picker';
import LocationSearch from './location-search';
const API_STATE_LOADING = 0;
const API_STATE_FAILURE = 1;
const API_STATE_SUCCESS = 2;
class MapEdit extends Component {
constructor() {
super( ...arguments );
const { focus_location } = this.props.attributes;
this.state = {
addPointVisibility: false,
apiState: API_STATE_LOADING,
focus_location_temp: focus_location && clone( focus_location.place_title ),
};
this.mapRef = createRef();
this.debouncedUpdateAPIKey = debounce( this.updateAPIKey, 800 );
}
componentWillUnmount() {
this.debouncedAPIKey.cancel();
}
addPoint = point => {
const { attributes, setAttributes } = this.props;
const { points } = attributes;
const newPoints = points.slice( 0 );
newPoints.push( point );
setAttributes( { points: newPoints } );
this.setState( { addPointVisibility: false } );
};
updateAlignment = value => {
this.props.setAttributes( { align: value } );
// Allow one cycle for alignment change to take effect
setTimeout( this.mapRef.current.sizeMap, 0 );
};
updateAPIKeyControl = value => {
this.setState( { apiKeyControl: value }, this.debouncedUpdateAPIKey );
};
updateAPIKey = () => {
const { noticeOperations } = this.props;
const { apiKeyControl } = this.state;
noticeOperations.removeAllNotices();
apiKeyControl && this.apiCall( apiKeyControl, 'POST' );
};
removeAPIKey = () => {
this.apiCall( null, 'DELETE' );
};
apiCall( service_api_key = null, method = 'GET' ) {
const { noticeOperations } = this.props;
const url = '/wp-json/jetpack/v4/service-api-keys/googlemaps';
const fetch = service_api_key ? { url, method, data: { service_api_key } } : { url, method };
apiFetch( fetch ).then(
result => {
noticeOperations.removeAllNotices();
this.setState( {
apiState: result.service_api_key ? API_STATE_SUCCESS : API_STATE_FAILURE,
api_key: result.service_api_key,
apiKeyControl: result.service_api_key,
} );
},
result => {
noticeOperations.removeAllNotices();
noticeOperations.createErrorNotice( result.message );
this.setState( {
apiState: API_STATE_FAILURE,
} );
}
);
}
componentDidMount() {
this.apiCall();
}
render() {
const { className, setAttributes, attributes, noticeUI, notices } = this.props;
const { map_style, points, zoom, map_center, marker_color, align, focus_location } = attributes;
const {
addPointVisibility,
api_key,
apiKeyControl,
apiState,
focus_location_temp,
} = this.state;
const inspectorControls = (
<Fragment>
<BlockControls>
<BlockAlignmentToolbar
value={ align }
onChange={ this.updateAlignment }
controls={ [ 'center', 'wide', 'full' ] }
/>
<Toolbar>
<IconButton
icon={ settings.markerIcon }
label="Add a marker"
onClick={ () => this.setState( { addPointVisibility: true } ) }
/>
</Toolbar>
</BlockControls>
<InspectorControls>
{ points.length < 1 && (
<PanelBody title={ __( 'Map Focus', 'jetpack' ) }>
<LocationSearch
label={ __( 'Center Point', 'jetpack' ) }
placeholder={ __( 'Choose a center location...' ) }
onAddPoint={ value => {
this.setState( { focus_location_temp: value.place_title } );
setAttributes( { focus_location: value } );
} }
onChange={ value => this.setState( { focus_location_temp: value } ) }
value={ focus_location_temp }
/>
</PanelBody>
) }
<PanelBody title={ __( 'Map Theme', 'jetpack' ) }>
<MapThemePicker
value={ map_style }
onChange={ value => {
setAttributes( { map_style: value } );
} }
options={ settings.map_styleOptions }
/>
</PanelBody>
<PanelColorSettings
title={ __( 'Colors', 'jetpack' ) }
initialOpen={ true }
colorSettings={ [
{
value: marker_color,
onChange: value => setAttributes( { marker_color: value } ),
label: 'Marker Color',
},
] }
/>
{ points.length ? (
<PanelBody title={ __( 'Markers', 'jetpack' ) } initialOpen={ false }>
<Locations
points={ points }
onChange={ value => {
setAttributes( { points: value } );
} }
/>
</PanelBody>
) : null }
<PanelBody title={ __( 'Google Maps API Key', 'jetpack' ) } initialOpen={ false }>
<TextControl
label={ __( 'Google Maps API Key', 'jetpack' ) }
value={ apiKeyControl }
onChange={ value => this.setState( { apiKeyControl: value } ) }
/>
<ButtonGroup>
<Button type="button" onClick={ this.updateAPIKey } isDefault>
{ __( 'Update Key' ) }
</Button>
<Button type="button" onClick={ this.removeAPIKey } isDefault>
{ __( 'Remove Key' ) }
</Button>
</ButtonGroup>
</PanelBody>
</InspectorControls>
</Fragment>
);
const placholderAPIStateLoading = (
<Placeholder icon={ settings.icon }>
<Spinner />
</Placeholder>
);
const getAPIInstructions = sprintf(
"This is your first map block. You need to get a Google Maps API key. <a href='%s' target='_blank'>Here's how to do it</a>.",
'https://developers.google.com/maps/documentation/javascript/get-api-key'
);
const placeholderAPIStateFailure = (
<Placeholder icon={ settings.icon } label={ __( 'Map', 'jetpack' ) } notices={ notices }>
<Fragment>
<div className="components-placeholder__instructions">
<RawHTML>{ getAPIInstructions }</RawHTML>
</div>
<TextControl
placeholder="Paste Key Here"
value={ apiKeyControl }
onChange={ this.updateAPIKeyControl }
/>
</Fragment>
</Placeholder>
);
const placeholderAPIStateSuccess = (
<Fragment>
{ inspectorControls }
<div className={ className }>
<Map
ref={ this.mapRef }
map_style={ map_style }
points={ points }
zoom={ zoom }
map_center={ map_center }
focus_location={ focus_location }
marker_color={ marker_color }
onSetZoom={ value => {
setAttributes( { zoom: value } );
} }
admin={ true }
api_key={ api_key }
onSetPoints={ value => setAttributes( { points: value } ) }
onMapLoaded={ () => this.setState( { addPointVisibility: true } ) }
onMarkerClick={ () => this.setState( { addPointVisibility: false } ) }
>
{ addPointVisibility && (
<AddPoint
onAddPoint={ this.addPoint }
onClose={ () => this.setState( { addPointVisibility: false } ) }
/>
) }
</Map>
</div>
</Fragment>
);
return (
<Fragment>
{ noticeUI }
{ apiState === API_STATE_LOADING && placholderAPIStateLoading }
{ apiState === API_STATE_FAILURE && placeholderAPIStateFailure }
{ apiState === API_STATE_SUCCESS && placeholderAPIStateSuccess }
</Fragment>
);
}
}
export default withNotices( MapEdit );