forked from fullstackreact/google-maps-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleApiComponent.js
More file actions
69 lines (56 loc) · 1.78 KB
/
GoogleApiComponent.js
File metadata and controls
69 lines (56 loc) · 1.78 KB
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
import React from 'react'
import ReactDOM from 'react-dom'
import {ScriptCache} from './lib/ScriptCache'
import GoogleApi from './lib/GoogleApi'
const defaultMapConfig = {}
const defaultCreateCache = (options) => {
options = options || {};
const apiKey = options.apiKey;
const libraries = options.libraries || ['places'];
const version = options.version || '3.29';
const language = options.language || 'en';
return ScriptCache({
google: GoogleApi({
apiKey: apiKey,
language: language,
libraries: libraries,
version: version
})
});
};
export const wrapper = (options) => (WrappedComponent) => {
const apiKey = options.apiKey;
const libraries = options.libraries || ['places'];
const version = options.version || '3';
const createCache = options.createCache || defaultCreateCache;
class Wrapper extends React.Component {
constructor(props, context) {
super(props, context);
this.scriptCache = createCache(options);
this.scriptCache.google.onLoad(this.onLoad.bind(this))
this.state = {
loaded: false,
map: null,
google: null
}
}
onLoad(err, tag) {
this._gapi = window.google;
this.setState({loaded: true, google: this._gapi})
}
render() {
const props = Object.assign({}, this.props, {
loaded: this.state.loaded,
google: window.google
});
return (
<div>
<WrappedComponent {...props}/>
<div ref='map'/>
</div>
)
}
}
return Wrapper;
}
export default wrapper;