import React, { useState, useEffect } from 'react';
import { Sun, Cloud, CloudRain, CloudSnow, CloudLightning, CloudDrizzle, Wind, Droplets, Thermometer, RefreshCw, Clock } from 'lucide-react';
export default function WeatherApp() {
const [weatherData, setWeatherData] = useState(null);
const [forecast, setForecast] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [lastUpdated, setLastUpdated] = useState(null);
const fetchWeather = async () => {
setLoading(true);
setError(null);
try {
// In a real app, you would use your own API key and proper error handling
// This is a mock implementation for demonstration purposes
// Simulate API call delay
await new Promise(resolve => setTimeout(resolve, 1000));
// Mock data for Kampala
const mockCurrentWeather = {
main: {
temp: 23,
feels_like: 24,
humidity: 65,
pressure: 1012
},
weather: [
{
main: 'Clouds',
description: 'scattered clouds',
icon: '03d'
}
],
wind: {
speed: 3.6,
deg: 220
},
sys: {
sunrise: 1617507000,
sunset: 1617550200
},
dt: Date.now() / 1000
};
// Mock forecast data
const mockForecast = [
{ day: 'Tomorrow', temp: 24, condition: 'Sunny', icon: 'Sun' },
{ day: 'Wednesday', temp: 25, condition: 'Partly Cloudy', icon: 'Cloud' },
{ day: 'Thursday', temp: 23, condition: 'Rain Showers', icon: 'CloudRain' },
{ day: 'Friday', temp: 22, condition: 'Thunderstorms', icon: 'CloudLightning' },
{ day: 'Saturday', temp: 24, condition: 'Partly Cloudy', icon: 'Cloud' }
];
setWeatherData(mockCurrentWeather);
setForecast(mockForecast);
setLastUpdated(new Date());
} catch (err) {
setError('Failed to fetch weather data. Please try again.');
console.error('Error fetching weather data:', err);
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchWeather();
// In a real app, you might want to set up a refresh interval
const interval = setInterval(fetchWeather, 30 * 60 * 1000); // Refresh every 30 minutes
return () => clearInterval(interval);
}, []);
// Helper function to get the appropriate weather icon
const getWeatherIcon = (iconCode) => {
if (!iconCode) return
{error}
{weatherData.weather[0].description}
Feels like {Math.round(weatherData.main.feels_like)}°C
Humidity
{weatherData.main.humidity}%
Wind
{weatherData.wind.speed} m/s
Pressure
{weatherData.main.pressure} hPa
Sunrise/Sunset
{formatTime(weatherData.sys.sunrise)} / {formatTime(weatherData.sys.sunset)}
{day.day}
{getForecastIcon(day.icon)}{day.temp}°C
{day.condition}