-
Notifications
You must be signed in to change notification settings - Fork 3
/
App.js
56 lines (48 loc) · 1.62 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
import { StatusBar } from 'expo-status-bar';
import React, {useEffect, useState}from 'react';
import { StyleSheet, Text, View, ImageBackground } from 'react-native';
import * as Location from 'expo-location';
import DateTime from './components/DateTime'
import WeatherScroll from './components/WeatherScroll'
const API_KEY ='49cc8c821cd2aff9af04c9f98c36eb74';
const img = require('./assets/image.png')
export default function App() {
const [data, setData] = useState({});
useEffect(() => {
(async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
fetchDataFromApi("40.7128", "-74.0060")
return;
}
let location = await Location.getCurrentPositionAsync({});
fetchDataFromApi(location.coords.latitude, location.coords.longitude);
})();
}, [])
const fetchDataFromApi = (latitude, longitude) => {
if(latitude && longitude) {
fetch(`https://api.openweathermap.org/data/2.5/onecall?lat=${latitude}&lon=${longitude}&exclude=hourly,minutely&units=metric&appid=${API_KEY}`).then(res => res.json()).then(data => {
// console.log(data)
setData(data)
})
}
}
return (
<View style={styles.container}>
<ImageBackground source={img} style={styles.image} >
<DateTime current={data.current} timezone={data.timezone} lat={data.lat} lon={data.lon}/>
<WeatherScroll weatherData={data.daily}/>
</ImageBackground>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
image:{
flex:1,
resizeMode:"cover",
justifyContent:"center"
}
});