-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
87 lines (78 loc) · 2.09 KB
/
App.tsx
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
import React, { useEffect, useState } from 'react';
import { ActivityIndicator, StyleSheet, Text, View, StatusBar, Dimensions } from 'react-native';
import SearchBar from './src/screens/SearchBar';
import Weather from './src/screens/Weather';
const API_KEY = '207747207310538a4e209c54ba715d68';
interface WeatherData {
weather: {
main: string;
}[];
name: string;
main: {
temp: number;
humidity: number;
};
wind: {
speed: number;
};
}
const App: React.FC = () => {
const [weatherData, setWeatherData] = useState<WeatherData | null>(null);
const [loaded, setLoaded] = useState<boolean>(true);
async function fetchWeatherData(cityName: string) {
setLoaded(false);
const API = `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&units=metric&appid=${API_KEY}`;
try {
const response = await fetch(API);
if (response.status === 200) {
const data: WeatherData = await response.json();
setWeatherData(data);
} else {
setWeatherData(null);
}
setLoaded(true);
} catch (error) {
console.log(error);
}
}
useEffect(() => {
fetchWeatherData('Mumbai');
}, []);
if (!loaded) {
return (
<View style={styles.container}>
<ActivityIndicator color="gray" size={36} />
</View>
);
} else if (weatherData === null) {
return (
<View style={styles.container}>
<StatusBar backgroundColor="darkgray" />
<SearchBar fetchWeatherData={fetchWeatherData} />
<Text style={styles.primaryText}>
City Not Found! Try Different City
</Text>
</View>
);
}
return (
<View style={styles.container}>
<StatusBar backgroundColor="#000000" barStyle="light-content" />
<Weather weatherData={weatherData} fetchWeatherData={fetchWeatherData} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff0f5',
alignItems: 'center',
justifyContent: 'center',
},
primaryText: {
marginVertical: 20,
fontSize: 28,
textAlign: 'center',
},
});
export default App;