-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
111 lines (97 loc) · 3.5 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {StatusBar} from 'expo-status-bar';
import React, {useEffect, useState} from 'react'
import {StyleSheet, Text, View, RefreshControl, ScrollView,} from 'react-native';
import * as Locatiion from 'expo-location';
import WeatherInfo from './components/WeatherInfo';
import Units from './components/Units';
import {apiKey} from '@env';
import Searchbar from './components/Searchbar';
import {SafeAreaView} from 'react-native-safe-area-context';
const baseUrl = 'https://api.openweathermap.org/data/2.5/'
export default function App() {
const [errorMessage, setErrorMessage] = useState(null)
const [currentWeather, setCurrentWeather] = useState(null)
const [unitsSystem, setUnitsSystem] = useState("metric")
const [refresh, setRefresh] = useState(false)
const wait = (timeout) => {
return new Promise(resolve => setTimeout(resolve, timeout));
}
const onRefresh = React.useCallback(() => {
setRefresh(true);
load()
wait(2000).then(() => setRefresh(false));
}, [])
useEffect(() => {
load()
}, [unitsSystem])
async function load() {
try {
let {status} = await Locatiion.requestForegroundPermissionsAsync()
if (status !== 'granted') {
setErrorMessage('Access to loaction is required')
return
}
const location = await Locatiion.getCurrentPositionAsync()
const {latitude, longitude} = location.coords
const weatherUrl = `${baseUrl}weather?lat=${latitude}&lon=${longitude}&units=${unitsSystem}&appid=${apiKey}`
const forecastUrl = `${baseUrl}forecast?lat=${latitude}&lon=${longitude}&units=${unitsSystem}&appid=${apiKey}`
response = await fetch(weatherUrl)
const weather = await response.json()
if (response.ok) {
setCurrentWeather(weather)
} else {
setErrorMessage(weather.message)
}
response = await fetch(forecastUrl)
const forecast = await response.json()
if (response.ok) {
console.log(forecast.message)
} else {
setErrorMessage(forecast.message)
}
} catch (error) {
setErrorMessage(error.message)
}
}
if (currentWeather) {
const {main: {temp}} = currentWeather
return (
<SafeAreaView style={styles.container}>
<ScrollView
refreshControl={
<RefreshControl
refreshing={refresh}
onRefresh={onRefresh}
/>
}
>
<StatusBar style="auto"/>
<View style={styles.main}>
<Searchbar/>
<WeatherInfo currentWeather={currentWeather}/>
</View>
</ScrollView>
<View style={styles.main}>
<Units unitsSystem={unitsSystem} setUnitsSystem={setUnitsSystem}/>
</View>
</SafeAreaView>
)
} else {
return (
<View style={styles.container}>
<Text>{errorMessage}</Text>
<StatusBar style="auto"/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
main: {
justifyContent: 'center',
flex: 1,
}
});