import { useState, useEffect } from "react"; import styles from "../styles/Home.module.css"; import Image from "next/image"; export default function Home() { const [input, setInput] = useState(); const [weatherData, setWeatherData] = useState(); const clickHandler = async () => { const res = await fetch("/api/data", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ input }), }); const data = await res.json(); console.log(data); setWeatherData({ ...data }); setInput(""); }; const something = (event) => { if (event.keyCode === 13) { clickHandler(); } }; function degToCompass(num) { var val = Math.floor(num / 22.5 + 0.5); var arr = [ "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", ]; return arr[val % 16]; } return (
{weatherData && (

{weatherData.name}, {weatherData.sys.country}

{weatherData.weather[0].description}

weatherIcon

{Math.round(weatherData.main.temp)}°C

Feels like {Math.round(weatherData.main.feels_like)}°C

Time:{" "} {new Date( weatherData.dt * 1000 + weatherData.timezone * 1000 ).toLocaleString("en-US")}

)}
(e.target.value = "")} onChange={(e) => setInput(e.target.value)} onKeyDown={(e) => something(e)} /> {weatherData && (

Humidity

weatherIcon

{weatherData.main.humidity}

%

Wind speed

weatherIcon

{weatherData.wind.speed}

Km/h

Wind direction

weatherIcon

{degToCompass(weatherData.wind.deg)}

Visibility

weatherIcon

{weatherData.visibility / 1000}

Km

{/*
Visibility: {weatherData.visibility / 1000} km
*/} {/*

Wind:{" "} {` ${weatherData.wind.speed} ${degToCompass( weatherData.wind.deg )}`}

weatherIcon
*/}

Sunrise

weatherIcon

4:31

AM

{/*

Sunrise:{" "} {new Date( weatherData.sys.sunrise * 1000 + weatherData.timezone * 1000 ).toLocaleString("en-US")}

*/} {/*

Sunset:{" "} {new Date(weatherData.sys.sunset * 1000).toLocaleString( "en-US" )}

*/}

Sunset

weatherIcon

10:02

PM

)}
); }