Skip to content

Commit f56643a

Browse files
author
Cédric Delpoux
committed
Tranform decorator to HOC component
1 parent 65de920 commit f56643a

File tree

7 files changed

+131
-77
lines changed

7 files changed

+131
-77
lines changed

.babelrc

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{
2-
"stage": 0,
3-
"loose": ["es6.modules", "es6.classes"],
4-
"blacklist": [
5-
"es6.tailCall",
6-
"spec.functionName"
7-
]
2+
"presets": [
3+
["latest", {
4+
"es2015": {
5+
"loose": true,
6+
"modules": false
7+
}
8+
}],
9+
"react"
10+
],
811
}

.eslintrc

+4-6
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,26 @@
2626
"consistent-this": [2, "self"],
2727
"curly": 0,
2828
"indent": [1, 2],
29-
"quotes": [1, "single", "avoid-escape"],
3029
"no-multiple-empty-lines": [1, {"max": 1}],
3130
"no-self-compare": 2,
3231
"no-underscore-dangle": 0,
3332
"no-unused-vars": [2, {"vars": "all", "args": "none"}],
3433
"no-use-before-define": 0,
3534
"no-var": 2,
3635
"semi": [2, "never"],
37-
"space-after-keywords": [1, "always"],
3836
"space-before-blocks": [1, "always"],
39-
"space-before-function-parentheses": [1, "never"],
37+
"space-before-function-paren": [1, "never"],
4038
"space-in-parens": [1, "never"],
41-
"spaced-line-comment": [1, "always"],
39+
"spaced-comment": [1, "always"],
4240
"strict": [2, "never"],
4341
"react/jsx-boolean-value": [1, "never"],
44-
"react/jsx-quotes": [1, "double", "avoid-escape"],
4542
"react/jsx-uses-react": 2,
4643
"react/jsx-uses-vars": 2,
44+
"react/jsx-wrap-multilines": 1,
4745
"react/no-did-mount-set-state": 2,
4846
"react/no-did-update-set-state": 2,
4947
"react/react-in-jsx-scope": 2,
5048
"react/self-closing-comp": 1,
51-
"react/wrap-multilines": 1
49+
"quotes": [1, "double", "avoid-escape"]
5250
}
5351
}

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## 2.0.0 - 2016-10-24
4+
5+
* Add : `little-loader` lib to fetch Google Map API
6+
* Add : `query-string` lib to transform params to query string
7+
* Update : `package.json` : description, author, dependencies
8+
* Update : `.babelrc`
9+
* Update : `.eslintrc`
10+
* Update : `src/index.js`
11+
312
## 1.0.1 - 2015-09-11
413

514
* Add : libraryTarget into Webpack config

README.md

+28-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# react-google-maps-loader ![npm](https://img.shields.io/npm/v/react-google-maps-loader.svg) ![license](https://img.shields.io/npm/l/react-google-maps-loader.svg) ![github-issues](https://img.shields.io/github/issues/cedricdelpoux/react-google-maps-loader.svg)
1+
# react-google-maps-loader ![npm](https://img.shields.io/npm/v/react-google-maps-loader.svg) ![license](https://img.shields.io/npm/l/react-google-maps-loader.svg) ![github-issues](https://img.shields.io/github/issues/xuopled/react-google-maps-loader.svg)
22

33
React decorator to use google maps services into your react applications
44

@@ -26,25 +26,34 @@ CONST = GOOGLE_MAPS_API_KEY = 'myapikey' // Change your api key
2626

2727
@googleMapsLoader({ key: GOOGLE_MAPS_API_KEY, libraries: ['places','geometry'] })
2828
export default class MyComponent extends Component {
29-
state = {
30-
map: null,
31-
}
32-
33-
componentDidMount() {
34-
const { googleMaps } = this.props
35-
const map = new googleMaps.Map(React.findDOMNode(this.refs.map))
36-
37-
this.setState({ map })
38-
}
39-
40-
method() {
41-
const { googleMaps } = this.props
42-
43-
return (
44-
<div ref="map"></div>
45-
)
46-
}
29+
constructor() {
30+
super()
31+
this.state = {
32+
map: null,
33+
markers: new Map(),
34+
}
35+
}
36+
37+
componentDidMount() {
38+
const {googleMaps} = this.props
39+
const map = new googleMaps.Map(this.ref_map)
40+
41+
this.setState({map})
42+
}
43+
44+
method() {
45+
const {googleMaps} = this.props
46+
47+
return (
48+
<div ref={ref => this.ref_map = ref} />
49+
)
50+
}
4751
}
52+
53+
export default placesLoader(MyComponent, {
54+
libraries: ["places", "geometry"],
55+
key: GOOGLE_MAPS_API_KEY,
56+
})
4857
```
4958

5059
## Development

package.json

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-google-maps-loader",
3-
"version": "1.0.1",
4-
"description": "React decorator to use google maps services into your react applications",
3+
"version": "2.0.0",
4+
"description": "React HOC to use google maps services into your react applications",
55
"repository": {
66
"type": "git",
77
"url": "git+https://github.com/xuopled/react-google-maps-loader.git"
@@ -14,7 +14,7 @@
1414
"loader",
1515
"decorator"
1616
],
17-
"author": "Cédric Delpoux <cedric.delpoux@gmail.com>",
17+
"author": "Cédric Delpoux <xuopled@gmail.com>",
1818
"license": "MIT",
1919
"bugs": {
2020
"url": "https://github.com/xuopled/react-google-maps-loader/issues"
@@ -25,19 +25,22 @@
2525
"lib"
2626
],
2727
"dependencies": {
28-
"react": "^0.13.1",
29-
"scriptjs": "^2.5.7"
28+
"little-loader": "~0.1.0"
29+
},
30+
"peerDependencies": {
31+
"react": "~15.3.0"
3032
},
3133
"devDependencies": {
32-
"babel": "~5.6.14",
33-
"babel-core": "~5.6.15",
34-
"babel-eslint": "~3.1.20",
35-
"babel-loader": "~5.1.4",
36-
"eslint": "~0.21.2",
37-
"eslint-loader": "~0.14.0",
38-
"eslint-plugin-react": "~2.6.4",
34+
"babel-core": "~6.14.0",
35+
"babel-eslint": "~6.1.0",
36+
"babel-loader": "~6.2.4",
37+
"babel-preset-latest": "~6.14.0",
38+
"babel-preset-react": "^6.16.0",
39+
"eslint": "~3.5.0",
40+
"eslint-loader": "~1.5.0",
41+
"eslint-plugin-react": "~6.2.0",
3942
"node-libs-browser": "~0.5.2",
40-
"webpack": "~1.10.1"
43+
"webpack": "~1.13.1"
4144
},
4245
"scripts": {
4346
"clean": "rm -rf lib",

src/index.js

+49-31
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,64 @@
1-
/* global google */
1+
import React, {Component} from "react"
2+
import load from "little-loader"
3+
import qs from "query-string"
24

3-
import React, { Component } from 'react'
4-
import scriptjs from 'scriptjs'
5+
const GOOGLE_MAP_PLACES_API = "https://maps.googleapis.com/maps/api/js"
6+
const NOT_LOADED = 0
7+
const LOADING = 1
8+
const LOADED = 2
59

6-
export default (options = {}) => (TargetComponent) => (
7-
class extends Component {
8-
state = {
9-
googleMaps: null,
10-
}
10+
const queue = []
11+
let state = NOT_LOADED
12+
let sdk
13+
14+
function useGoogleMapSdk(params, callback) {
15+
if (state === LOADED) {
16+
callback(sdk)
17+
} else if (state === LOADING) {
18+
queue.push(callback)
19+
} else if (window.google != null && window.google.maps != null) {
20+
state = LOADED
21+
sdk = window.google.maps
22+
callback(sdk)
23+
} else {
24+
state = LOADING
25+
queue.push(callback)
26+
27+
load(`${GOOGLE_MAP_PLACES_API}?${qs.stringify(params)}`, (err) => {
28+
if (err) {
29+
throw new Error("Unable to load Google Map SDK")
30+
}
31+
32+
state = LOADED
33+
sdk = window.google.maps
1134

12-
componentDidMount() {
13-
if (typeof window.google === 'undefined') {
14-
window.googleMapsLoaded = () => {
15-
scriptjs.done('google-maps-places')
16-
}
17-
18-
options.callback = 'googleMapsLoaded'
19-
20-
const queryString = Object.keys(options).reduce((result, key) => (
21-
options[key] !== null && options[key] !== undefined
22-
? (result += key + '=' + options[key] + '&')
23-
: result
24-
), '?').slice(0, -1)
25-
26-
scriptjs('https://maps.googleapis.com/maps/api/js' + queryString)
27-
scriptjs.ready('google-maps-places', () => {
28-
this.handleLoaded(google.maps)
29-
})
30-
} else {
31-
this.handleLoaded(google.maps)
35+
while (queue.length > 0) {
36+
queue.pop()(sdk)
37+
}
38+
})
39+
}
40+
}
41+
42+
const GoogleMapsLoader = (TargetComponent, params) => (
43+
class extends Component {
44+
constructor() {
45+
super()
46+
this.state = {
47+
googleMaps: null,
3248
}
3349
}
3450

35-
handleLoaded(googleMaps) {
36-
this.setState({ googleMaps })
51+
componentWillMount() {
52+
useGoogleMapSdk(params, googleMaps => this.setState({googleMaps}))
3753
}
3854

3955
render() {
40-
const { googleMaps } = this.state
56+
const {googleMaps} = this.state
4157
return googleMaps
4258
? <TargetComponent googleMaps={googleMaps} {...this.props} />
4359
: null
4460
}
4561
}
4662
)
63+
64+
export default GoogleMapsLoader

webpack.config.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,24 @@ module.exports = {
1818
},
1919
module: {
2020
preLoaders: [
21-
{ test: /\.js?$/, include: sources, loader: 'eslint' },
21+
{
22+
test: /\.js?$/,
23+
include: sources,
24+
loader: 'eslint',
25+
query: {
26+
presets: ['latest']
27+
},
28+
},
2229
],
2330
loaders: [
24-
{ test: /\.js$/, include: sources, loader: 'babel' },
31+
{
32+
test: /\.js$/,
33+
include: sources,
34+
loader: 'babel',
35+
query: {
36+
presets: ['latest']
37+
},
38+
},
2539
],
2640
},
2741
externals: {

0 commit comments

Comments
 (0)