1- import React , { useCallback } from 'react' ;
2- import { useState , useEffect } from 'react' ;
1+ import React , { useCallback , useState , useEffect } from 'react' ;
32
43import Weather from './components/Weather' ;
54import LoadingIndicator from './UI/LoadingIndicator' ;
@@ -14,17 +13,22 @@ function App() {
1413 const [ isLoading , setIsLoading ] = useState ( false ) ;
1514
1615 function handleSearch ( e ) {
16+ setLocation ( '' ) ;
1717 setSearch ( e . target . value ) ;
1818 }
1919
2020 async function geoHandler ( ) {
2121 setSearch ( '' ) ;
2222 setIsLoading ( true ) ;
23+
2324 await navigator . geolocation . getCurrentPosition ( ( position ) => {
2425 const crd = position . coords ;
2526 const latitude = crd . latitude ;
2627 const longitude = crd . longitude ;
28+
2729 setLocation ( `${ latitude } ,${ longitude } ` ) ;
30+
31+ setIsLoading ( false ) ;
2832 } ) ;
2933 }
3034
@@ -45,12 +49,22 @@ function App() {
4549 } , [ ] ) ;
4650
4751 useEffect ( ( ) => {
48- fetchData ( search , location ) . then ( ( responseData ) => {
52+ const delay = setTimeout ( ( ) => {
53+ fetchData ( search ) . then ( ( responseData ) => {
54+ setPlace ( responseData . location ) ;
55+ setData ( responseData . current ) ;
56+ setIsLoading ( false ) ;
57+ } ) ;
58+ } , 500 ) ;
59+ return ( ) => clearTimeout ( delay ) ;
60+ } , [ search , fetchData ] ) ;
61+
62+ useEffect ( ( ) => {
63+ fetchData ( location ) . then ( ( responseData ) => {
4964 setPlace ( responseData . location ) ;
5065 setData ( responseData . current ) ;
51- setIsLoading ( false ) ;
5266 } ) ;
53- } , [ location , search , fetchData ] ) ;
67+ } , [ location , fetchData ] ) ;
5468
5569 let display = data ? (
5670 < Weather place = { place } data = { data } />
@@ -74,7 +88,9 @@ function App() {
7488 < p > or</ p >
7589 </ div >
7690 < div >
77- < button onClick = { geoHandler } > Find me!</ button >
91+ < button onClick = { geoHandler } disabled = { isLoading } >
92+ Find me!
93+ </ button >
7894 </ div >
7995 < br />
8096 < br />
0 commit comments