import React, {FC, useEffect, useState} from "react" import PageLayout from "../components/PageLayout" import useAxiosTools from "../hooks/AxiosTools" import {WeatherValue} from "../types" import Card from "../components/Card" import Img from "../components/Img" const Weather = () => { const [currentWeather, setCurrentWeather] = useState(null) const [fetchTime, setFetchTime] = useState(0) const [weatherDays, setWeatherDays] = useState<[string, WeatherValue[]][]|null>(null) const {loading, setLoading, errorLabel, errorCatch, cleanErrors, axiosGet} = useAxiosTools() useEffect(() => { const timer = setInterval(() => { if (!loading && (fetchTime + 5 * 60) < getCurrentTime()) { fetchWeather() } }, 1000) return () => clearInterval(timer) }, [fetchTime]) const getCurrentTime = () => Number(((new Date).getTime() /1000).toFixed()) const fetchWeather = async () => { try { setLoading(true) cleanErrors() const res = await axiosGet(`/api/weather`) const currentWeather = res.data.list[0] const weatherDays: [string, WeatherValue[]][] = [] let objectEntries = {index: -1, date: ''} res.data.list.forEach((item: WeatherValue) => { const date = item.dt_txt.split(' ')[0] if (date === (new Date).toSQLDate()) { if (currentWeather.main.temp_min > item.main.temp_min) { currentWeather.main.temp_min = item.main.temp_min } if (currentWeather.main.temp_max < item.main.temp_max) { currentWeather.main.temp_max = item.main.temp_max } } if (date !== objectEntries.date) { objectEntries = {index: objectEntries.index + 1, date} } if (!weatherDays[objectEntries.index]) { weatherDays[objectEntries.index] = [date, []] } weatherDays[objectEntries.index][1] = [...weatherDays[objectEntries.index][1], item] }) setWeatherDays(weatherDays) setCurrentWeather(currentWeather) setFetchTime(getCurrentTime()) } catch (e) { errorCatch(e) } finally { setLoading(false) } } return {errorLabel()}
{currentWeather?.main.temp.toFixed()} °C {currentWeather?.weather[0].description}
{/*{currentWeather && {currentWeather?.weather[0].main}}*/} {currentWeather && {currentWeather?.weather[0].main}}
{currentWeather?.main.temp_max.toFixed()} °C {currentWeather?.main.temp_min.toFixed()} °C
{weatherDays?.filter(([date]) => date !== (new Date).toSQLDate()) .map(([date, values]) => )}
} export default Weather const WeatherCard: FC<{date: string, values: WeatherValue[]}> = ({date, values= []}) => { const [weatherState, setWeatherState] = useState<{main: string, description: string, icon: string, min: number, max: number}|null>(null) useEffect(() => { const weatherState = { min: 100, max: -100, main: '', icon: '', description: '', } const result: Record = {} values.forEach(value => { if (value.main.temp_min < weatherState.min) { weatherState.min = value.main.temp_min } if (value.main.temp_max > weatherState.max) { weatherState.max = value.main.temp_max } const tag = value.weather[0].main if (! result[tag]) { result[tag] = 0 } result[tag]++ }) let itemToReturn: {name:string, value: number}|null = null for (const item in result) { if (! itemToReturn || itemToReturn.value < result[item]) { itemToReturn = {name: item, value: result[item]} } } if (itemToReturn && itemToReturn.name) { const nameToSearch = itemToReturn.name const value = values.find(item => item.weather[0].main === nameToSearch) if (value) { weatherState.main = itemToReturn.name weatherState.description = value.weather[0].description weatherState.icon = value.weather[0].icon.replace('n', 'd') setWeatherState(weatherState) } } }, []) return
{(new Date(date)).getWeekDay()} {weatherState?.description}
{weatherState?.main
{weatherState?.max.toFixed()} °C {weatherState?.min.toFixed()} °C
}