@@ -76,8 +76,9 @@ export default function WeatherScreen({ navigation }: NavProp) {
7676 } , [ lastUpdated , weatherCacheTime , currentWeather ] )
7777
7878 useEffect ( ( ) => {
79- currentCity && fetchResult ( )
80- } , [ currentCity , fetchResult ] )
79+ currentCity && ! isPending && fetchResult ( )
80+ // eslint-disable-next-line react-hooks/exhaustive-deps
81+ } , [ currentCity ] )
8182
8283 const bottom = useSafeAreaInsets ( ) . bottom
8384 const top = useSafeAreaInsets ( ) . top
@@ -121,12 +122,13 @@ function Boxes({ w, theme }: { w: Weather; theme: Theme }) {
121122 } ) ,
122123 )
123124
124- const { mutate } = useMutation ( {
125+ const { mutate, isPending } = useMutation ( {
125126 mutationKey : [ 'currentWeatherAQI' ] ,
126- mutationFn : ( ) => fetchResult ( ) ,
127+ mutationFn : ( ) => getAQI ( currentCity ?. lat || 0 , currentCity ?. lon || 0 ) ,
127128 onError : ( err ) => console . log ( err ) ,
128129 onSuccess : ( d : AQI ) => {
129130 setCurrentAQI ( d )
131+ setLastUpdatedAQI ( new Date ( ) . getTime ( ) )
130132 } ,
131133 } )
132134 const aqiStatus = useMemo ( ( ) => getAQIStatus ( currentAQI ?. list ?. [ 0 ] ?. main ?. aqi || 0 ) , [ currentAQI ] )
@@ -138,14 +140,17 @@ function Boxes({ w, theme }: { w: Weather; theme: Theme }) {
138140 const fetchResult = useCallback ( async ( ) : Promise < AQI > => {
139141 const now = new Date ( ) . getTime ( )
140142 if ( now - lastUpdatedAQI > weatherCacheTime ) {
141- setLastUpdatedAQI ( now )
142- return ( await getAQI ( currentCity ?. lat || 0 , currentCity ?. lon || 0 ) ) as AQI
143+ console . log ( 'Fetching AQI from API' )
144+ mutate ( )
143145 }
144146 return currentAQI
145- } , [ lastUpdatedAQI , weatherCacheTime , currentAQI , setLastUpdatedAQI , currentCity ?. lat , currentCity ?. lon ] )
147+ // eslint-disable-next-line react-hooks/exhaustive-deps
148+ } , [ lastUpdatedAQI , weatherCacheTime , currentAQI ] )
146149
147- // eslint-disable-next-line react-hooks/exhaustive-deps
148- useEffect ( ( ) => mutate , [ currentCity ] )
150+ useEffect ( ( ) => {
151+ currentCity && ! isPending && fetchResult ( )
152+ // eslint-disable-next-line react-hooks/exhaustive-deps
153+ } , [ currentCity ] )
149154
150155 return (
151156 < View style = { { flexDirection : 'row' , justifyContent : 'space-between' , rowGap : 12 } } className = 'flex-wrap px-4' >
0 commit comments