@@ -29,6 +29,7 @@ export default function Weather() {
2929 const [ data , setData ] = useState ( null ) ;
3030 const [ loading , setLoading ] = useState ( false ) ;
3131 const [ error , setError ] = useState ( null ) ;
32+ const [ unit , setUnit ] = useState ( 'C' ) ; // °C by default
3233
3334 useEffect ( ( ) => {
3435 fetchWeather ( city ) ;
@@ -52,32 +53,44 @@ export default function Weather() {
5253 const current = data ?. current_condition ?. [ 0 ] ;
5354 const forecast = data ?. weather ?. slice ( 0 , 3 ) || [ ] ;
5455
56+ // Helper to convert °C to °F
57+ const displayTemp = ( c ) => unit === 'C' ? c : Math . round ( ( c * 9 / 5 ) + 32 ) ;
58+
5559 return (
5660 < div >
5761 < h2 > Weather Dashboard</ h2 >
5862 < form onSubmit = { e => { e . preventDefault ( ) ; fetchWeather ( city ) ; } } className = "inline-form" >
5963 < input value = { city } onChange = { e => setCity ( e . target . value ) } placeholder = "Enter city" />
6064 < button type = "submit" > Fetch</ button >
6165 </ form >
66+
67+ { /* Toggle button */ }
68+ < div style = { { margin : '10px 0' } } >
69+ < button onClick = { ( ) => setUnit ( unit === 'C' ? 'F' : 'C' ) } >
70+ Switch to °{ unit === 'C' ? 'F' : 'C' }
71+ </ button >
72+ </ div >
73+
6274 { loading && < Loading /> }
6375 < ErrorMessage error = { error } />
76+
6477 { current && (
6578 < Card title = { `Current in ${ city } ` } >
66- < p > Temperature: { current . temp_C } °C </ p >
79+ < p > Temperature: { displayTemp ( Number ( current . temp_C ) ) } ° { unit } </ p >
6780 < p > Humidity: { current . humidity } %</ p >
6881 < p > Desc: { current . weatherDesc ?. [ 0 ] ?. value } </ p >
69- { /* TODO: Dynamic background based on weather condition */ }
7082 </ Card >
7183 ) }
84+
7285 < div className = "grid" >
7386 { forecast . map ( day => (
7487 < Card key = { day . date } title = { day . date } >
75- < p > Avg Temp: { day . avgtempC } °C </ p >
88+ < p > Avg Temp: { displayTemp ( Number ( day . avgtempC ) ) } ° { unit } </ p >
7689 < p > Sunrise: { day . astronomy ?. [ 0 ] ?. sunrise } </ p >
7790 </ Card >
7891 ) ) }
7992 </ div >
80- { /* TODO: Add search history & favorites */ }
8193 </ div >
8294 ) ;
8395}
96+
0 commit comments