Skip to content

Commit dc20267

Browse files
committed
Refactor Weather component to use updated font utility and remove unused imports
1 parent 2057992 commit dc20267

File tree

5 files changed

+31
-31
lines changed

5 files changed

+31
-31
lines changed

src/screens/Weather/Main/Weather.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Canvas, LinearGradient, Rect, vec } from '@shopify/react-native-skia'
44
import { useMutation } from '@tanstack/react-query'
55
import { WeatherColors } from '@utils/colors'
66
import { H, W } from '@utils/dimensions'
7-
import { F, Medium, Regular, SemiBold } from '@utils/fonts'
7+
import { F, SemiBold } from '@utils/fonts'
88
import type { NavProp, Theme } from '@utils/types'
99
import React, { useCallback, useEffect, useMemo } from 'react'
1010
import { StatusBar, View } from 'react-native'
@@ -57,7 +57,7 @@ export default function WeatherScreen({ navigation }: NavProp) {
5757

5858
const { isPending, error, data, mutate } = useMutation({
5959
mutationKey: ['currentWeather'],
60-
mutationFn: () => fetchResult(),
60+
mutationFn: () => getWeather(currentCity?.lat || 0, currentCity?.lon || 0),
6161
onError: (err) => console.log(err),
6262
onSuccess: (d) => {
6363
setCurrentWeather(d)
@@ -66,18 +66,18 @@ export default function WeatherScreen({ navigation }: NavProp) {
6666
})
6767
const w = data || currentWeather
6868

69-
useEffect(() => {
70-
if (currentCity) mutate()
71-
// eslint-disable-next-line react-hooks/exhaustive-deps
72-
}, [currentCity])
73-
74-
const fetchResult = useCallback(async (): Promise<Weather> => {
69+
const fetchResult = useCallback(async () => {
7570
const now = new Date().getTime()
7671
if (now - lastUpdated > weatherCacheTime) {
77-
return (await getWeather(currentCity?.lat || 0, currentCity?.lon || 0)) as Weather
72+
console.log('Fetching from API')
73+
mutate()
7874
}
79-
return currentWeather
80-
}, [currentWeather, lastUpdated, weatherCacheTime, currentCity])
75+
// eslint-disable-next-line react-hooks/exhaustive-deps
76+
}, [lastUpdated, weatherCacheTime, currentWeather])
77+
78+
useEffect(() => {
79+
currentCity && fetchResult()
80+
}, [currentCity, fetchResult])
8181

8282
const bottom = useSafeAreaInsets().bottom
8383
const top = useSafeAreaInsets().top
@@ -98,8 +98,8 @@ export default function WeatherScreen({ navigation }: NavProp) {
9898
</View>
9999
<ScrollView contentContainerStyle={{ paddingBottom: bottom + 20, gap: 12 }}>
100100
<WeatherTopInfo color={color} w={w} />
101-
<HourlyForecast color={color} w={w} hourly={data?.hourly} />
102-
<DailyForecast color={color} daily={data?.daily} theme={theme} />
101+
<HourlyForecast color={color} w={w} />
102+
<DailyForecast color={color} w={w} theme={theme} />
103103
<Boxes w={w} theme={theme} />
104104
<PaddingBottom />
105105
</ScrollView>

src/screens/Weather/Main/components/DailyForecast.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { weatherStore, type TemperatureUnit } from '@/zustand/weatherStore'
22
import { Calendar03SolidIcon } from '@assets/icons/icons'
33
import Btn from '@components/Button'
44
import Gradient from '@components/Gradient'
5-
import type { Daily } from '@screens/Weather/types'
5+
import type { Daily, Weather } from '@screens/Weather/types'
66
import { Icons } from '@screens/Weather/utils'
77
import { F, Medium, Regular } from '@utils/fonts'
88
import type { Theme } from '@utils/types'
@@ -16,13 +16,14 @@ type WeatherForecastProps = {
1616
color: {
1717
color: string
1818
}
19-
daily: Daily[] | undefined
2019
theme: Theme
20+
w: Weather
2121
}
2222

23-
export default function DailyForecast({ color, daily, theme }: WeatherForecastProps) {
23+
export default function DailyForecast({ color, w, theme }: WeatherForecastProps) {
2424
const currentUnit = weatherStore((state) => state.temperatureUnit)
2525
// const [data, setData] = useState<Daily[] | undefined>()
26+
const daily = w?.daily
2627
const currentTemp = weatherStore((state) => state.currentWeather?.current.temp)
2728
const weeklyMinMax = useMemo(() => calculateWeeklyMinMax(daily), [daily])
2829
const dotPosition = useMemo(() => calculateDotPosition(currentTemp, daily && daily[0]), [currentTemp, daily])

src/screens/Weather/Main/components/HourlyForecast.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ type HourlyWeather = {
1717
color: string
1818
}
1919
w: Weather
20-
hourly: Current[] | undefined
2120
}
2221

23-
const HourlyForecast = React.memo<HourlyWeather>(({ color, w, hourly }) => {
22+
const HourlyForecast = React.memo<HourlyWeather>(({ color, w }) => {
2423
const currentUnit = weatherStore((state) => state.temperatureUnit)
2524
const timeFormat = weatherStore((state) => state.weatherTimeFormat)
2625
const Icon = Icons[w?.current.weather[0]!.icon || '02d']
2726
// const [data, setData] = useState<Current[] | undefined>()
28-
const data = hourly
27+
const data = w?.hourly
2928

3029
// useEffect(() => {
3130
// const timer = screenDelay(() => setData(hourly))

src/screens/Weather/Widget/WeatherWidget.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const WeatherWidget = React.memo<{ navigation: StackNav }>(({ navigation }) => {
4646

4747
const { isPending, error, data, mutate } = useMutation({
4848
mutationKey: ['currentWeather'],
49-
mutationFn: () => fetchResult(),
49+
mutationFn: () => getWeather(currentCity?.lat || 0, currentCity?.lon || 0),
5050
onError: (err) => console.log(err),
5151
onSuccess: (d) => {
5252
setCurrentWeather(d)
@@ -55,18 +55,18 @@ const WeatherWidget = React.memo<{ navigation: StackNav }>(({ navigation }) => {
5555
})
5656
const w = data || currentWeather
5757

58-
useEffect(() => {
59-
if (currentCity) mutate()
60-
// eslint-disable-next-line react-hooks/exhaustive-deps
61-
}, [currentCity])
62-
63-
const fetchResult = useCallback(async (): Promise<Weather> => {
58+
const fetchResult = useCallback(async () => {
6459
const now = new Date().getTime()
6560
if (now - lastUpdated > weatherCacheTime) {
66-
return (await getWeather(currentCity?.lat || 0, currentCity?.lon || 0)) as Weather
61+
console.log('Fetching from API')
62+
mutate()
6763
}
68-
return currentWeather
69-
}, [currentWeather, lastUpdated, weatherCacheTime, currentCity])
64+
// eslint-disable-next-line react-hooks/exhaustive-deps
65+
}, [lastUpdated, weatherCacheTime, currentWeather])
66+
67+
useEffect(() => {
68+
currentCity && fetchResult()
69+
}, [currentCity, fetchResult])
7070

7171
if (!weatherWidgetIsActive) return null
7272
if (!currentCity)

src/screens/Weather/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import S from '@utils/storage'
22

3-
export const ACCUWEATHER_API_KEY = 'FqLdDZQkQofVcwsdCHX7uKdPVgWcPGHI'
4-
// export const ACCUWEATHER_API_KEY = 'C42OAAd8AANIyi8SLoqvq3mm59vADvRA'
3+
// export const ACCUWEATHER_API_KEY = 'FqLdDZQkQofVcwsdCHX7uKdPVgWcPGHI'
4+
export const ACCUWEATHER_API_KEY = 'C42OAAd8AANIyi8SLoqvq3mm59vADvRA'
55

66
export const OPENWEATHER_API_KEY = '0e376e0750966cdba160fc85a4bb0427'
77

0 commit comments

Comments
 (0)