-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeatherApp.js
106 lines (94 loc) · 2.91 KB
/
WeatherApp.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
import React from 'react';
import { StyleSheet, Text, View, Image, Dimensions, ActivityIndicator, ScrollView } from 'react-native';
const APIKEY = '****';
const URL_PATH = "http://api.wunderground.com/api/";
const FORECAST_PATH = "/forecast10day/q/";
const FORECAST_DAYS = 5;
export default class App extends React.Component {
constructor() {
super();
this.state = {
zipcode: '64108',
forecast: [],
error: null,
}
}
fetchForecast(zipcode){
if(this.state.error) {
this.setState({error: null})
}
fetch(`${URL_PATH}${APIKEY}${FORECAST_PATH}${zipcode}.json`)
.then(response => response.json())
.then(data => this.setParsedResults(data))
.catch(error => this.setState({error: error}));
}
setParsedResults(data) {
const weather = data.forecast.simpleforecast.forecastday;
const forecast = [];
weather.forEach( (item, i) => {
if(i < FORECAST_DAYS && i <= 9 ) {
forecast[i] = {
date: `${item.date.weekday}, ${item.date.month}/${item.date.day}/${item.date.year}`,
high: item.high,
low: item.low,
conditions: item.conditions,
avewind: item.avewind,
average_humidity: item.avehumidity,
icon_url: item.icon_url
}
}
});
this.setState({forecast: forecast});
}
componentDidMount() {
this.fetchForecast(this.state.zipcode);
}
render() {
return (
<View style={styles.container}>
<Text style={{fontWeight: "900"}}> {"\n"}{this.state.zipcode} Weather </Text>
{ this.state.forecast.length > 0
? this.state.forecast.map( (element) =>
<WeatherBox element={element} key={element.date} />
)
: <ActivityIndicator />
}
</View>
);
}
}
const WeatherBox = ({ element }) =>
<View style={styles.weatherBox}>
<View style={{justifyContent: "flex-start", flex: 2}}>
<Text style={{fontWeight: "900"}}>{element.date}</Text>
<Text>{element.conditions}</Text>
<Text>High: {element.high.fahrenheit} F | {element.high.celsius} C </Text>
<Text>Low: {element.low.fahrenheit} F | {element.low.celsius} C </Text>
<Text>Wind: {element.avewind.dir} @ {element.avewind.mph} MPH</Text>
</View>
<View style={{flex: 1, justifyContent: "flex-end"}}>
<Image style={{width: 50, height: 50}} source={{uri: element.icon_url}} />
</View>
</View>
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#888',
alignItems: 'center',
justifyContent: 'center',
},
weatherBox : {
margin: 5,
borderWidth: 2,
paddingLeft: 15,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: "white",
width: Dimensions.get('window').width * 0.8,
height: Dimensions.get('window').height * .12,
flex: 1,
flexDirection: 'row',
borderTopLeftRadius: 20,
borderBottomRightRadius: 20
}
});