-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
93 lines (79 loc) · 2 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
90
91
92
93
import React from 'react';
import { Alert } from 'react-native';
import Loading from './Loading';
import * as Location from 'expo-location';
import axios from 'axios';
import propTypes from 'prop-types';
import {HomeScreen} from './Weather';
import {WeatherView} from './View/WeatherView.js';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
const API_KEY = '43bf7802f3e1ec3e2f1fe2117a88cb5f';
const Main = createStackNavigator();
const weatherTypes = {
condition: [
'Thunderstorm',
'Drizzle',
'Rain',
'Snow',
'Clear',
'Clouds',
'Mist',
'Smoke',
'Haze',
'Dust',
'Fog',
'Sand',
'Dust',
'Ash',
'Squall',
'Tornado',
]
};
export default class extends React.Component {
state = {
isLoading: true,
};
getWeather = async (latitude, longitude) => {
const {
data: {
main: { temp },
weather,
},
} = await axios.get(
`https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&APPID=${API_KEY}&units=metric`,
);
this.setState({ isLoading: false, condition: weather[0].main, temp });
};
getLocation = async () => {
try {
await Location.requestPermissionsAsync();
const {
coords: { latitude, longitude },
} = await Location.getCurrentPositionAsync({});
this.getWeather(latitude, longitude);
// Send to Api get Weather
} catch (error) {
Alert.alert("Can't find you", 'So sad.');
}
};
componentDidMount() {
this.getLocation();
}
render() {
const { isLoading, temp, condition } = this.state;
return isLoading ? (
<Loading />
) : (
<NavigationContainer>
<Main.Navigator initialRouteName="homeScreen">
<Main.Screen name='homeScreen'>
{props => <HomeScreen {...props} temperature={Math.round(temp)} condition={condition}/> }
</Main.Screen>
<Main.Screen name='weatherScreen' component={WeatherView}/>
</Main.Navigator>
</NavigationContainer>
);
}
}