import React, { useState, useEffect } from "react"; import { UserPlus, LogIn, BadgePlus, RadioTower, Search, Cloud, CloudRain, Droplets, Wind, Thermometer, Sun, Moon, MapPin, CalendarClock, ArrowRight, BellRing, LayoutDashboard, TowerControl, } from "lucide-react"; import { useAuth } from "../AuthContext"; import axios from "axios"; import { useTranslation } from "react-i18next"; import { API_BASE_URL } from "../config"; function EnhancedWeatherHome() { const { t } = useTranslation(); const [searchQuery, setSearchQuery] = useState(""); const [locations, setLocations] = useState([]); const [infoMeteo, setInfoMeteo] = useState([]); const [activeFilter, setActiveFilter] = useState("all"); const [currentTime, setCurrentTime] = useState(new Date()); const { user, token } = useAuth(); const [ville, setVille] = useState("Paris, France"); const heure = currentTime.getHours(); const isDayTime = heure > 6 && heure < 20; useEffect(() => { const timer = setInterval(() => { setCurrentTime(new Date()); }, 60000); return () => clearInterval(timer); }, []); useEffect(() => { axios .post(`${API_BASE_URL}/getMeteoHome`, { location: ville, }) .then((response) => { if (response.data.length === 0) { console.warn("Aucun infos disponible."); } else { console.log(response.data[0]); setInfoMeteo(response.data[0]); } }) .catch((error) => { console.error("Erreur lors de la récupération des infos :", error); }); }, [ville]); useEffect(() => { axios .get(`${API_BASE_URL}/getLocations`) .then((response) => { if (response.data.length === 0) { console.warn("Aucun lieu disponible."); } else { console.log(response.data); setLocations(response.data); } }) .catch((error) => { console.error("Erreur lors de la récupération des catégories :", error); }); }, []); const filteredLocations = locations.filter((loc) => loc.toLowerCase().includes(searchQuery.toLowerCase()) ); const formatDate = (date) => { const options = { weekday: "long", day: "numeric", month: "long" }; return date.toLocaleDateString("fr-FR", options); }; const formatHeure = (h) => { const heureFormatee = (h % 24).toString().padStart(2, "0"); return `${heureFormatee}:00`; }; const hourlyForecast = [ { time: "Maintenant", temp: "22°", icon: , }, { time: formatHeure(heure + 1), temp: "22°", icon: , }, { time: formatHeure(heure + 2), temp: "22°", icon: , }, { time: formatHeure(heure + 3), temp: "21°", icon: , }, { time: formatHeure(heure + 4), temp: "20°", icon: , }, { time: formatHeure(heure + 5), temp: "19°", icon: , }, ]; const dailyForecast = [ { day: "Lun", temp: "21°/15°", icon: , }, { day: "Mar", temp: "23°/16°", icon: , }, { day: "Mer", temp: "24°/17°", icon: , }, { day: "Jeu", temp: "22°/16°", icon: , }, { day: "Ven", temp: "20°/14°", icon: , }, ]; return (
{" "}
{isDayTime ? ( <>
) : ( <>
)}

{t('home.title')}

{t('home.subtitle')}

setSearchQuery(e.target.value)} />
{searchQuery.length > 0 && (
    {filteredLocations.length > 0 ? ( filteredLocations.map((loc, idx) => (
  • { setVille(loc); setSearchQuery(""); }} > {loc}
  • )) ) : (
  • {t('home.noCityFound')}
  • )}
)}

{ville}

{formatDate(currentTime)}

{isDayTime ? ( ) : ( )}

{infoMeteo.temperature}°C

{t('home.partlyCloudy')}

{t('home.feelsLike')}

{infoMeteo.temperature - 2}°C

{t('home.humidity')}

{infoMeteo.humidity}%

{t('home.wind')}

{infoMeteo.wind_speed} km/h

{t('home.rain')}

30%

{t('home.sunrise')}

06:42

{t('home.sunset')}

20:51

{t('home.hourlyForecast')}

{hourlyForecast.map((item, index) => (

{item.time}

{item.icon}

{item.temp}

))}

{t('home.dailyForecast')}

{dailyForecast.map((item, index) => (
{item.day} {item.icon}
{item.temp}
))}

{t('home.weatherServices')}

{user?.role === "user" && (

{t('home.viewObjectsTitle')}

{t('home.viewObjectsDesc')}

{t('home.viewObjectsBtn')}
)} {!token && (

{t('home.signupTitle')}

{t('home.signupDesc')}

{t('home.signupBtn')}

{t('home.loginTitle')}

{t('home.loginDesc')}

{t('home.loginBtn')}
)} {user?.role === "complexe" && (

{t('home.consultObjectsTitle')}

{t('home.consultObjectsDesc')}

{t('home.consultObjectsBtn')}

{t('home.addObjTitle')}

{t('home.addObjDesc')}

{t('home.addObjBtn')}
)} {user?.role === "admin" && (

{t('home.adminDashTitle')}

{t('home.adminDashDesc')}

{t('home.adminDashBtn')}

{t('home.manageObjTitle')}

{t('home.manageObjDesc')}

{t('home.manageObjBtn')}
)}
); } export default EnhancedWeatherHome;