1
1
import React , { Fragment , useState , useEffect } from 'react' ;
2
2
import { fetchWeather } from './api/fetchWeather' ;
3
-
4
-
3
+ import ReactLoading from 'react-loading' ;
5
4
6
5
function App ( ) {
7
6
const [ location , setLocation ] = useState ( false ) ;
8
7
const [ query , setQuery ] = useState ( '' ) ;
9
8
const [ weather , setWeather ] = useState ( { } ) ;
9
+ const [ loading , setLoading ] = useState ( false ) ;
10
10
11
11
useEffect ( ( ) => {
12
12
navigator . geolocation . getCurrentPosition ( ( position ) => {
13
13
setLocation ( true ) ;
14
14
const { latitude, longitude } = position . coords ;
15
15
const searchGeo = async ( e ) => {
16
+ setLoading ( true ) ;
16
17
const data = await fetchWeather ( false , latitude , longitude ) ;
18
+ setLoading ( false ) ;
17
19
setWeather ( data ) ;
18
20
}
19
21
searchGeo ( ) ;
@@ -23,12 +25,14 @@ function App() {
23
25
24
26
const search = async ( e ) => {
25
27
if ( e . key === 'Enter' ) {
28
+ setLoading ( true ) ;
26
29
const data = await fetchWeather ( query ) ;
30
+ setLoading ( false ) ;
27
31
setWeather ( data ) ;
28
32
setQuery ( '' ) ;
29
33
}
30
34
}
31
-
35
+
32
36
return (
33
37
< Fragment >
34
38
< a href = "https://github.com/raminhuk/react-weather" className = "icon-github" target = "_blanck" >
@@ -41,12 +45,16 @@ function App() {
41
45
< span className = "form-search" >
42
46
< input type = "text" className = "search" placeholder = "Digite uma cidade" value = { query } onChange = { ( e ) => setQuery ( e . target . value ) } onKeyPress = { search } />
43
47
</ span >
48
+
49
+ { loading && (
50
+ < ReactLoading color = "#FFF" type = 'spin' />
51
+ ) }
44
52
{ weather . cod == 404 && (
45
53
< p className = "not" >
46
54
Cidade ou localização não encontrado
47
55
</ p >
48
56
) }
49
- { ( typeof weather . main != "undefined" ) ? (
57
+ { ( typeof weather . main != "undefined" ) && ! loading ? (
50
58
< div className = "box-city" >
51
59
< h2 className = "city-name" >
52
60
< span > { weather . name } </ span >
0 commit comments