-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
120 lines (111 loc) · 2.97 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
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
import { StatusBar } from 'expo-status-bar'
import { ActivityIndicator, ImageBackground, KeyboardAvoidingView, Platform, StyleSheet, Text, TextInput, View } from 'react-native'
import SearchInput from './components/SearchInput'
import getImageForWeather from './utils/weatherIImages'
import { useEffect, useState } from 'react'
import fetchWeather from './utils/api'
type AppState = {
loading: boolean;
error: boolean;
address: string;
temperature: number;
description: string;
}
const initialState: AppState = {
loading: true,
error: false,
address: '',
temperature: 0,
description: ''
}
export default function App() {
const [state, setState] = useState<AppState>(initialState)
const handleUpdateLocation = async (city: string) => {
if (!city) return
try {
const { address, temperature, description } = await fetchWeather(city)
setState({
loading: false,
error: false,
address,
temperature,
description
})
} catch (err) {
setState(prevState => ({
...prevState,
loading: false,
error: true
}))
}
}
useEffect(() => {
async function fetchInitialData() {
await handleUpdateLocation('San Francisco')
}
fetchInitialData()
}, [])
return (
<KeyboardAvoidingView style={styles.container} behavior='padding'>
<StatusBar style='auto' />
<ImageBackground
source={getImageForWeather(state.description)}
style={styles.imageContainer}
imageStyle={styles.image}
>
<View style={styles.detailsContainer}>
<ActivityIndicator animating={state.loading} color='white' size='large'/>
{!state.loading && (
<View>
{state.error && (
<Text style={[styles.smallText, styles.textStyle]}>
Could not load weather data. Please try a different city.
</Text>
)}
{!state.error && (
<View>
<Text style={[styles.largeText, styles.textStyle]}>{state.address}</Text>
<Text style={[styles.smallText, styles.textStyle]}>{state.description}</Text>
<Text style={[styles.largeText, styles.textStyle]}>{state.temperature}°</Text>
</View>
)}
<SearchInput placeholder='Search any City' locationCallback={handleUpdateLocation}/>
</View>
)}
</View>
</ImageBackground>
</KeyboardAvoidingView>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#34495e'
},
detailsContainer: {
flex: 1,
justifyContent: 'center',
backgroundColor: 'rgba(0, 0, 0, 0.2)',
paddingHorizontal: 20
},
smallText: {
fontSize: 18
},
largeText: {
fontSize: 44
},
textStyle: {
textAlign: 'center',
fontFamily: Platform.OS == 'ios' ? 'AvenirNext-Regular' : 'Roboto',
color: 'white'
},
imageContainer: {
flex: 1
},
image: {
flex: 1,
width: null,
height: null,
resizeMode: 'cover'
}
})