-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeatherProject.js
92 lines (70 loc) · 2.46 KB
/
WeatherProject.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
'use restrict'
const APPID = '9e517c39e4a8a270d91231c8fb7d01b8';
// Import way for ES6
import React, {Component} from 'react';
import {
Text,
View,
TextInput,
Image } from 'react-native';
var Forecast = require('./Forecast');
var styles = require('./styles/weatherStyle');
class WeatherProject extends Component {// Parent of Forecast
constructor(props) {
super(props);
this._handleTextChange = this._handleTextChange.bind(this);
this.state = {cityname: '', forecast: null};
}
_handleTextChange(event) {
var cityname = event.nativeEvent.text;
this.setState({cityname: cityname});
var requestUrl = 'http://api.openweathermap.org/data/2.5/weather?q=' + cityname + '&appid=' + APPID;
fetch(requestUrl)
.then((response) => response.json())
.then((responseJSON) => {
console.log(responseJSON);
this.setState({
forecast: {
main: responseJSON.name,
description: responseJSON.weather[0].main,
temp: responseJSON.main.temp
}
})
})
.catch((error) => {
console.warn(error);
});
}
render() {
var content = null;
console.log(this);
if (this.state.forecast !== null) {
content = <Forecast
main={this.state.forecast.main}
description={this.state.forecast.description}
temp={this.state.forecast.temp}
/>;
}
return (
<View style={styles.container}>
<Image source={require('./img/flowers.png')}
resizeMode='cover'
style={styles.backdrop}>
<View style={styles.overlay}>
<View style={styles.row}>
<Text style={styles.mainText}>
Current weather for
</Text>
<TextInput
style={styles.cityStyle}
returnKeyType='go'
onSubmitEditing={this._handleTextChange}/>
</View>
{content}
</View>
</Image>
</View>
);
}
}
module.exports = WeatherProject;