-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
80 lines (68 loc) · 2.11 KB
/
App.tsx
File metadata and controls
80 lines (68 loc) · 2.11 KB
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
import { useState } from 'react';
import { Text, View, ImageBackground, StatusBar, ActivityIndicator } from 'react-native';
import SearchInput from 'src/components/SearchInput';
import WeatherDisplay from 'src/components/WeatherDisplay';
import getImageForWeather from 'src/utils/getImageForWeather';
import fetchLocationId from 'src/api/fetchLocationId';
import fetchWeather from 'src/api/fetchWeather';
import { Weather } from 'src/types';
import commonStyles from './styles';
import styles from './App.styles';
const initialWeather: Weather = {
location: '',
weather: '',
temperature: 0,
}
export default function App() {
const [weather, setWeather] = useState<Weather>(initialWeather);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(false);
const changeLocation = async (city: string) => {
if (!city) return;
setLoading(true);
try {
const locationId = await fetchLocationId(city);
const weather = await fetchWeather(locationId);
setWeather(weather);
setError(false);
} catch (error) {
setWeather(initialWeather);
setError(true);
} finally {
setLoading(false);
}
};
return (
<View style={styles.container}>
<StatusBar barStyle="light-content" />
<ImageBackground
source={getImageForWeather(weather.weather)}
style={styles.imageContainer}
imageStyle={styles.image}
>
<View style={styles.detailsContainer}>
<ActivityIndicator
animating={loading}
size="large"
color="white"
/>
{!loading && (
<View>
{error ? (
<Text style={[commonStyles.text, commonStyles.textSmall]}>
Could not load weather, please try a different city.
</Text>
) : (
<WeatherDisplay weather={weather} />
)}
</View>
)}
<SearchInput
placeholder="Search any city"
onSubmit={changeLocation}
/>
</View>
</ImageBackground>
</View>
);
}