-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
89 lines (83 loc) · 2.49 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
import React, { useState } from 'react';
import axios from 'axios';
import { BASE_URL, API_KEY } from './src/constant';
import { View, Text, StyleSheet, ActivityIndicator, KeyboardAvoidingView, Platform, TouchableWithoutFeedback, Keyboard, ScrollView } from 'react-native';
import WeatherSearch from './src/components/weatherSearch';
import WeatherInfo from './src/components/weatherInfo';
const App = () => {
const [weatherData, setWeatherData] = useState();
const [status, setStatus] = useState('');
const searchWeather = (location) => {
setStatus('loading');
Keyboard.dismiss();
axios
.get(`${BASE_URL}?q=${location}&appid=${API_KEY}`)
.then((response) => {
const data = response.data;
data.visibility /= 1000;
data.visibility = data.visibility.toFixed(2);
data.main.temp -= 273.15;
data.main.temp = data.main.temp.toFixed(2);
setWeatherData(data);
setStatus('success');
})
.catch((error) => {
setStatus('error');
});
};
const renderComponent = () => {
switch (status) {
case 'loading':
return <ActivityIndicator size="large" color="#00f" />;
case 'success':
return <WeatherInfo weatherData={weatherData} />;
case 'error':
return <Text style={styles.errorText}>Something went wrong. Please try again with a correct city name.</Text>;
default:
return null;
}
};
return (
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<View style={styles.container}>
<Text style={styles.header}>Weather App</Text>
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={styles.avoidingView}
>
<WeatherSearch searchWeather={searchWeather} />
</KeyboardAvoidingView>
<ScrollView contentContainerStyle={styles.resultContainer}>
{renderComponent()}
</ScrollView>
</View>
</TouchableWithoutFeedback>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
backgroundColor: '#f0f0f0',
},
header: {
fontSize: 24,
fontWeight: 'bold',
textAlign: 'center',
marginBottom: 20,
marginTop: 40, // Menambah jarak dari atas
},
avoidingView: {
marginBottom: 10, // Mengatur jarak dari elemen di bawahnya
},
resultContainer: {
flexGrow: 1,
alignItems: 'center',
},
errorText: {
color: 'red',
fontSize: 16,
textAlign: 'center',
},
});
export default App;