Skip to content

Commit 2028ebb

Browse files
committed
Show map data from apollo
1 parent 6bdb0a4 commit 2028ebb

File tree

3 files changed

+23
-51
lines changed

3 files changed

+23
-51
lines changed

packages/client/src/index.js

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
/* eslint no-console: 0 */
21
import React from 'react'
32
import ReactDOM from 'react-dom'
4-
import { ApolloClient } from 'apollo-client'
5-
import { InMemoryCache } from 'apollo-cache-inmemory'
6-
import { HttpLink } from 'apollo-link-http'
7-
import gql from "graphql-tag"
8-
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
3+
import { ApolloProvider } from 'react-apollo'
4+
import { BrowserRouter, Route, Switch } from 'react-router-dom'
95
import StartPage from './pages/StartPage'
106
import DemoPage from './pages/DemoPage'
117
import { startPageRoute, demoPageRoute } from './routes'
128
import './index.scss'
9+
import client from './graphql/client'
1310

1411
function addDivWithIdToBody() {
1512
const appElement = document.createElement("div")
@@ -19,37 +16,15 @@ function addDivWithIdToBody() {
1916

2017
addDivWithIdToBody()
2118

22-
const cache = new InMemoryCache()
23-
24-
const client = new ApolloClient({
25-
cache,
26-
link: new HttpLink({
27-
uri: 'https://streams-server.herokuapp.com/graphql',
28-
}),
29-
})
30-
31-
client
32-
.query({
33-
query: gql`
34-
query GetStations {
35-
stations(pageSize: 3) {
36-
stations {
37-
id
38-
text
39-
}
40-
}
41-
}
42-
`,
43-
})
44-
.then(result => console.log('~result~', result))
45-
4619
const App = () => (
47-
<Router>
48-
<Switch>
49-
<Route exact path={startPageRoute} component={StartPage} />
50-
<Route exact path={demoPageRoute} component={DemoPage} />
51-
</Switch>
52-
</Router>
20+
<BrowserRouter>
21+
<ApolloProvider client={client}>
22+
<Switch>
23+
<Route exact path={startPageRoute} component={StartPage} />
24+
<Route exact path={demoPageRoute} component={DemoPage} />
25+
</Switch>
26+
</ApolloProvider>
27+
</BrowserRouter>
5328
)
5429

5530
ReactDOM.render(<App />, document.getElementById('app'))

packages/client/src/pages/DemoPage/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import React from 'react'
2+
import { Query } from 'react-apollo'
23
import { Link } from 'react-router-dom'
34
import Map from '../../patterns/molecules/map/Map'
45
import { startPageRoute } from '../../routes'
56
import Header from '../../patterns/molecules/navigation/Header'
7+
import { STATIONS } from '../../graphql/query'
68

79
const DemoReactPage = () => (
810
<div>
@@ -15,7 +17,9 @@ const DemoReactPage = () => (
1517
</Link>
1618
</div>
1719
<div className="border p-3">
18-
<Map />
20+
<Query query={STATIONS}>
21+
{({ data }) => data.stations && data.stations.stations ? <Map items={data.stations.stations}/> : null}
22+
</Query>
1923
</div>
2024
</div>
2125
</div>

packages/client/src/patterns/molecules/map/Map.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-disable */
22
import React from 'react'
3-
import { arrayOf, shape, number, string } from 'prop-types'
3+
import { arrayOf, shape, number, string, bool } from 'prop-types'
44
import GoogleMapReact from 'google-map-react'
55
import { fitBounds } from 'google-map-react/utils'
66
import Marker from '../../atoms/map/Marker'
7-
import list from '../../../data/stations'
7+
import client from '../../../graphql/client'
88

99
const K_MARGIN_TOP = 60
1010
const K_MARGIN_RIGHT = 30
@@ -18,8 +18,6 @@ class Map extends React.Component {
1818
constructor(props) {
1919
super(props)
2020

21-
const {lat, lng} = list[0]
22-
2321
this.state = {
2422
center: undefined,
2523
zoom: undefined,
@@ -168,8 +166,9 @@ class Map extends React.Component {
168166
}
169167

170168
render() {
171-
const { initialCenter, initialZoom, apiKey, showLimit, items } = this.props;
169+
const { initialZoom, apiKey, showLimit, items } = this.props;
172170
const { center, zoom } = this.state;
171+
const initialCenter = {lat: items[0].lat, lng: items[0].lng}
173172

174173
const markers = items.slice(0, showLimit).map((item, index) => {
175174
return (
@@ -208,10 +207,6 @@ class Map extends React.Component {
208207
}
209208

210209
Map.propTypes = {
211-
initialCenter: shape({
212-
lat: number,
213-
lng: number
214-
}),
215210
initialZoom: number,
216211
apiKey: string,
217212
showLimit: number,
@@ -220,19 +215,17 @@ Map.propTypes = {
220215
id: string,
221216
lat: number,
222217
lng: number,
218+
text: string,
219+
isFocussed: bool,
220+
isSelected: bool,
223221
})
224222
),
225223
}
226224

227225
Map.defaultProps = {
228-
initialCenter: {
229-
lat: list[0].lat,
230-
lng: list[0].lng
231-
},
232226
initialZoom: 11,
233227
apiKey: process.env.API_KEY,
234228
showLimit: 10,
235-
items: list,
236229
}
237230

238231
export default Map

0 commit comments

Comments
 (0)