-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
87 lines (76 loc) · 1.71 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
import React, { useState, useEffect } from 'react';
import {
Text,
SafeAreaView,
StyleSheet,
ImageBackground,
Keyboard,
Alert
} from 'react-native';
import axios from 'axios';
import Form from './src/components/Form';
import ActivatedCity from './src/components/ActivatedCity';
import EmptyCity from './src/components/EmptyCity';
export default function App() {
const [city, setCity] = useState('');
const [inputCity, setInputCity] = useState('');
useEffect(() => {
setInputCity('');
}, [setCity]);
async function handleSubmit() {
try {
const response = await axios.get(
`http://api.openweathermap.org/data/2.5/weather?q=${inputCity},br&appid=531a125eabbef01c0a15891c5e5f73b9&units=metric`
);
const {
name,
main: { temp, feels_like, humidity }
} = response.data;
Keyboard.dismiss();
setCity({ name, temp, feels_like, humidity, activated: true });
} catch (err) {
Alert.alert('Oops...', 'Cidade não encontrada!', [
{ text: 'Tentar Novamente' }
]);
}
}
return (
<SafeAreaView>
<ImageBackground
style={styles.container}
source={require('./src/assets/bg.png')}
>
<Text style={styles.textTitle}>CLIMA AGORA</Text>
<Form
value={inputCity}
onChange={setInputCity}
onSubmit={handleSubmit}
/>
{city.activated ? (
<ActivatedCity
city={city}
/>
) : (
<EmptyCity />
)}
</ImageBackground>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignContent: 'center',
minHeight: '100%'
},
textTitle: {
paddingTop: 0,
textAlign: 'center',
fontSize: 24,
color: '#FFF',
fontWeight: 'bold',
paddingTop: 50,
letterSpacing: 5,
marginBottom: 20
},
});