Skip to content

Commit 961217a

Browse files
committed
Add temperature with apollo-link-rest
1 parent 599a9a0 commit 961217a

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

src/components/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Section from './Section'
99
import CurrentUser from './CurrentUser'
1010
import Profile from './Profile'
1111
import Reviews from './Reviews'
12+
import CurrentTemperature from './CurrentTemperature'
1213

1314
const Book = ({ user }) => (
1415
<div>
@@ -31,6 +32,7 @@ class App extends Component {
3132
<h1 className="App-title">The GraphQL Guide</h1>
3233
</Link>
3334
<CurrentUser />
35+
<CurrentTemperature />
3436
</header>
3537
<Switch>
3638
<Route exact path="/" render={() => <Redirect to="/Preface" />} />

src/components/CurrentTemperature.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import React, { Component } from 'react'
2+
import { Query } from 'react-apollo'
3+
import gql from 'graphql-tag'
4+
import IconButton from '@material-ui/core/IconButton'
5+
import MyLocation from '@material-ui/icons/MyLocation'
6+
7+
const kelvinToCelsius = kelvin => Math.round(kelvin - 273.15)
8+
const kelvinToFahrenheit = kelvin =>
9+
Math.round((kelvin - 273.15) * (9 / 5) + 32)
10+
11+
class CurrentTemperature extends Component {
12+
state = {
13+
lat: null,
14+
lon: null,
15+
gettingPosition: false,
16+
displayInCelsius: true
17+
}
18+
19+
requestLocation = () => {
20+
this.setState({ gettingPosition: true })
21+
window.navigator.geolocation.getCurrentPosition(
22+
({ coords: { latitude, longitude } }) => {
23+
this.setState({ lat: latitude, lon: longitude, gettingPosition: false })
24+
}
25+
)
26+
}
27+
28+
toggleDisplayFormat = () => {
29+
this.setState({
30+
displayInCelsius: !this.state.displayInCelsius
31+
})
32+
}
33+
34+
render() {
35+
const dontHaveLocationYet = !this.state.lat
36+
37+
return (
38+
<div className="Weather">
39+
<Query
40+
query={TEMPERATURE_QUERY}
41+
skip={dontHaveLocationYet}
42+
variables={{ lat: this.state.lat, lon: this.state.lon }}
43+
>
44+
{({ data, loading }) => {
45+
if (loading || this.state.gettingPosition) {
46+
return <div className="Spinner" />
47+
}
48+
49+
if (dontHaveLocationYet) {
50+
return (
51+
<IconButton
52+
className="Weather-get-location"
53+
onClick={this.requestLocation}
54+
color="inherit"
55+
>
56+
<MyLocation />
57+
</IconButton>
58+
)
59+
}
60+
61+
const kelvin = data.weather.main.temp
62+
const formattedTemp = this.state.displayInCelsius
63+
? `${kelvinToCelsius(kelvin)} °C`
64+
: `${kelvinToFahrenheit(kelvin)} °F`
65+
66+
return (
67+
<IconButton onClick={this.toggleDisplayFormat}>
68+
{formattedTemp}
69+
</IconButton>
70+
)
71+
}}
72+
</Query>
73+
</div>
74+
)
75+
}
76+
}
77+
78+
const TEMPERATURE_QUERY = gql`
79+
query TemperatureQuery {
80+
weather(lat: $lat, lon: $lon)
81+
@rest(
82+
type: "WeatherReport"
83+
path: "weather?appid=4fb00091f111862bed77432aead33d04&{args}"
84+
) {
85+
main
86+
}
87+
}
88+
`
89+
90+
export default CurrentTemperature

src/lib/apollo.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getMainDefinition } from 'apollo-utilities'
77
import { setContext } from 'apollo-link-context'
88
import { getAuthToken } from 'auth0-helpers'
99
import { withClientState } from 'apollo-link-state'
10+
import { RestLink } from 'apollo-link-rest'
1011

1112
import { errorLink } from './errorLink'
1213

@@ -78,6 +79,10 @@ const stateLink = withClientState({
7879
}
7980
})
8081

81-
const link = ApolloLink.from([errorLink, stateLink, networkLink])
82+
const restLink = new RestLink({
83+
uri: 'https://api.openweathermap.org/data/2.5/'
84+
})
85+
86+
const link = ApolloLink.from([errorLink, stateLink, restLink, networkLink])
8287

8388
export const apollo = new ApolloClient({ link, cache })

0 commit comments

Comments
 (0)