-
Notifications
You must be signed in to change notification settings - Fork 13
/
App-Step8-Networking.js
109 lines (102 loc) · 2.5 KB
/
App-Step8-Networking.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
import React from 'react';
import {
ScrollView,
StyleSheet,
Text,
View,
} from 'react-native';
import { ListItem } from 'react-native-elements';
class CityWeather extends React.Component {
render() {
return (
<ListItem
title={this.props.name}
subtitle={
this.props.weather ? this.props.weather : 'TBD'
}
badge={{
value: this.props.temp
? this.props.temp + ' °F'
: '0 °F',
badgeContainerStyle: {
backgroundColor: 'lightblue',
},
}}
avatar={{ uri: this.props.icon }}
onPress={
this.props.onPress ? this.props.onPress() : null
}
/>
);
}
}
export default class App extends React.Component {
constructor() {
super();
this.state = {
cities: [
{ name: 'San Jose', id: 5392171 },
{ name: 'New York', id: 5128581 },
{ name: 'London', id: 2643744 },
{ name: 'Paris', id: 2968815 },
{ name: 'Hong Kong', id: 1819729 },
{ name: 'Singapore', id: 1880252 },
{ name: 'Beijing', id: 1816670 },
{ name: 'Sydney', id: 6619279 },
{ name: 'São Paulo', id: 3448439 },
{ name: 'San Juan', id: 4568138 },
{ name: 'Mumbai', id: 1275339 },
{ name: 'Reykjavík', id: 6692263 },
],
};
}
componentDidMount() {
const ids = this.state.cities
.map(city => city.id)
.toString();
fetch(
`http://api.openweathermap.org/data/2.5/group?units=imperial&APPID=b1b35bba8b434a28a0be2a3e1071ae5b&id=${ids}`
)
.then(res => res.json())
.then(body =>
body.list.map(city => {
return {
id: city.id,
name: city.name,
temp: city.main.temp,
icon: 'http://openweathermap.org/img/w/' +
city.weather[0].icon +
'.png',
weather: city.weather[0].main,
};
}))
.then(cities => {
this.setState({
cities: cities,
});
});
}
render() {
return (
<ScrollView>
{this.state.cities.map(city => (
<CityWeather
key={city.id}
name={city.name}
temp={city.temp}
weather={city.weather}
icon={city.icon}
/>
))}
</ScrollView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});