Skip to content

Commit db40532

Browse files
committed
Refactor DailyForecast component to display either a 7-day or 3-day forecast
1 parent cb43838 commit db40532

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

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

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { weatherStore, type TemperatureUnit } from '@/zustand/weatherStore'
22
import { Calendar03SolidIcon } from '@assets/icons/icons'
3+
import Btn from '@components/Button'
34
import Gradient from '@components/Gradient'
45
import type { Daily } from '@screens/Weather/types'
56
import { Icons } from '@screens/Weather/utils'
67
import { F, Medium, Regular } from '@utils/fonts'
78
import type { Theme } from '@utils/types'
89
import { getDay, tempConverter } from '@utils/utils'
9-
import React, { useEffect, useMemo } from 'react'
10+
import React, { useMemo } from 'react'
1011
import { View } from 'react-native'
1112
import Animated, { FadeIn } from 'react-native-reanimated'
1213
import WeatherLabel, { Underline } from './WeatherLabel'
@@ -25,40 +26,54 @@ export default function DailyForecast({ color, daily, theme }: WeatherForecastPr
2526
const currentTemp = weatherStore((state) => state.currentWeather?.current.temp)
2627
const weeklyMinMax = useMemo(() => calculateWeeklyMinMax(daily), [daily])
2728
const dotPosition = useMemo(() => calculateDotPosition(currentTemp, daily && daily[0]), [currentTemp, daily])
28-
const data = daily
29+
const [is7Day, setIs7Day] = React.useState(false)
30+
31+
const data = useMemo(() => {
32+
if (is7Day) return daily
33+
else return daily?.slice(0, 3)
34+
}, [daily, is7Day])
2935

3036
// useEffect(() => {
3137
// const timer = screenDelay(() => setData(daily))
3238
// return () => clearTimeout(timer)
3339
// }, [daily])
3440

3541
return (
36-
<Animated.View className='px-4' entering={FadeIn.duration(700).delay(100)}>
37-
<View className='rounded-3xl bg-black/10 pb-2'>
38-
<WeatherLabel color={color} label='7-Day Forecast' Icon={Calendar03SolidIcon} />
39-
<Underline />
40-
{!data && <View style={{ height: 56 * 5.855 }}></View>}
41-
<View>
42-
{data?.map((d, i) => {
43-
return (
44-
<DailyWeather
45-
day={i === 0 ? 'Today' : getDay(d.dt || new Date().getTime())}
46-
dotPosition={i === 0 ? dotPosition : null}
47-
key={i}
48-
d={d}
49-
currentUnit={currentUnit}
50-
min={weeklyMinMax.min}
51-
max={weeklyMinMax.max}
52-
theme={theme}
53-
/>
54-
)
55-
})}
42+
<>
43+
<Animated.View className='px-4' entering={FadeIn.duration(700).delay(100)}>
44+
<View className='rounded-3xl bg-black/10 pb-2'>
45+
<WeatherLabel color={color} label='7-Day Forecast' Icon={Calendar03SolidIcon} />
46+
<Underline />
47+
{!data && <View style={{ height: 56 * 5.855 }}></View>}
48+
<View>
49+
{data?.map((d, i) => {
50+
return (
51+
<DailyWeather
52+
day={i === 0 ? 'Today' : getDay(d.dt || new Date().getTime())}
53+
dotPosition={i === 0 ? dotPosition : null}
54+
key={i}
55+
d={d}
56+
currentUnit={currentUnit}
57+
min={weeklyMinMax.min}
58+
max={weeklyMinMax.max}
59+
theme={theme}
60+
/>
61+
)
62+
})}
63+
</View>
64+
<Regular className='mb-1 mt-3 px-5 text-center opacity-60' style={[color, F.F9]}>
65+
Percentage represents the probability of precipitation.
66+
</Regular>
67+
<View className='mb-2 mt-2 px-4'>
68+
<Btn
69+
title={is7Day ? '3-Day Forecast' : '7-Day Forecast'}
70+
onPress={() => setIs7Day(!is7Day)}
71+
className='bg-white/10'
72+
/>
73+
</View>
5674
</View>
57-
<Regular className='mb-1 mt-3 px-5 text-center opacity-60' style={[color, F.F9]}>
58-
Percentage represents the probability of precipitation.
59-
</Regular>
60-
</View>
61-
</Animated.View>
75+
</Animated.View>
76+
</>
6277
)
6378
}
6479

0 commit comments

Comments
 (0)