-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
180 lines (154 loc) · 4.33 KB
/
App.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import React from 'react';
import {
StyleSheet,
View,
ImageBackground,
Text,
KeyboardAvoidingView,
Platform,
ActivityIndicator,
StatusBar,
} from 'react-native';
import { getWeather } from './utils/api';
import getImageForWeather from './utils/getImageForWeather';
import getIconForWeather from './utils/getIconForWeather';
import SearchInput from './SearchInput';
import moment from 'moment';
import {toCamelCase} from 'string-manager'
export default class App extends React.Component {
constructor(props) {
super(props);
this.handleDate = this.handleDate.bind(this);
this.state = {
loading: false,
error: false,
location: '',
weather: '',
timestamp: '2000-01-01T00:00:00.000000Z',
temperature: 0,
wind: '',
sunrise: '',
sunset: ''
};
}
componentDidMount() {
this.handleUpdateLocation('Yogyakarta');
}
handleDate = date => moment(date).format("hh:mm");
handleUpdateLocation = async city => {
if (!city) return;
this.setState({ loading: true }, async () => {
try {
const { location, weather, timestamp, temperature, wind, sunrise, sunset } = await getWeather(city);
this.setState({
loading: false,
error: false,
location,
weather,
timestamp,
temperature,
wind,
sunrise,
sunset
});
} catch (e) {
this.setState({
loading: false,
error: true,
});
}
});
};
render() {
const { loading, error, location, weather, timestamp, temperature, wind, sunrise, sunset } = this.state;
return (
<KeyboardAvoidingView style={styles.container} behavior="padding">
<StatusBar barStyle="light-content" />
<ImageBackground
source={getImageForWeather(weather.main)}
style={styles.imageContainer}
imageStyle={styles.image}
>
<View style={styles.detailsContainer}>
<ActivityIndicator animating={loading} color="white" size="large" />
{!loading && (
<View>
{error && (
<Text style={[styles.smallText, styles.textStyle]}>
😞 Tidak bisa menampilkan cuaca, coba beberapa saat lagi...
</Text>
)}
{!error && (
<View>
<Text style={[styles.iconText, styles.textStyle]}>
{getIconForWeather(weather.main)}
</Text>
<Text style={[styles.largeText, styles.textStyle]}>
{location}
</Text>
<Text style={[styles.weatherText, styles.textStyle]}>
{weather.main}, {toCamelCase(weather.description)}
</Text>
<Text style={[styles.largeText, styles.textStyle]}>
{`${Math.round(temperature)}°C`}
</Text>
<Text style={[styles.weatherText, styles.textStyle]}>
{`${Math.round(wind.speed)} m/s`} | {`${Math.round(wind.deg)}°`}
</Text>
</View>
)}
<SearchInput
placeholder="Ketik nama kota"
onSubmit={this.handleUpdateLocation}
/>
{!error && (
<Text style={[styles.smallText, styles.textStyle]}>
Update terakhir {this.handleDate(timestamp)}
</Text>
)}
</View>
)}
</View>
</ImageBackground>
</KeyboardAvoidingView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#34495E',
},
imageContainer: {
flex: 1,
},
image: {
flex: 1,
width: null,
height: null,
resizeMode: 'cover',
},
detailsContainer: {
flex: 1,
justifyContent: 'center',
backgroundColor: 'rgba(0,0,0,0.2)',
paddingHorizontal: 20,
},
textStyle: {
textAlign: 'center',
fontFamily: Platform.OS === 'ios' ? 'AvenirNext-Regular' : 'Roboto',
color: 'white',
},
iconText: {
fontSize: 72,
},
largeText: {
fontSize: 44,
},
smallText: {
fontSize: 18,
},
weatherText: {
fontSize: 22,
}
});