diff --git a/pages/index.js b/pages/index.js index a4d33cb..b001000 100644 --- a/pages/index.js +++ b/pages/index.js @@ -6,6 +6,7 @@ export default function Home() { const [input, setInput] = useState("mumbai"); const [systemUsed, setSystemUsed] = useState("metric"); const [weatherData, setWeatherData] = useState(); + // const [isLoading, setIsLoading] = useState(true); const getData = async () => { const res = await fetch("/api/data", { @@ -100,64 +101,62 @@ export default function Home() { "Saturday", ]; - return ( + return weatherData ? (
- {weatherData && ( -
-

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

+
+

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

-

- {weatherData.weather[0].description} -

- weatherIcon +

+ {weatherData.weather[0].description} +

+ weatherIcon + +

+ {systemUsed == "metric" + ? Math.round(weatherData.main.temp) + : Math.round(ctoF(weatherData.main.temp))} + °{systemUsed == "metric" ? "C" : "F"} +

+

+ Feels like{" "} + {systemUsed == "metric" + ? Math.round(weatherData.main.feels_like) + : Math.round(ctoF(weatherData.main.feels_like))} + °{systemUsed == "metric" ? "C" : "F"} +

+
-

- {systemUsed == "metric" - ? Math.round(weatherData.main.temp) - : Math.round(ctoF(weatherData.main.temp))} - °{systemUsed == "metric" ? "C" : "F"} -

-

- Feels like{" "} - {systemUsed == "metric" - ? Math.round(weatherData.main.feels_like) - : Math.round(ctoF(weatherData.main.feels_like))} - °{systemUsed == "metric" ? "C" : "F"} -

-
- )}
- {weatherData && ( -

- { - weekday[ - new Date( - convertTime(weatherData.dt, weatherData.timezone).input - ).getUTCDay() - ] - } - ,{" "} - {systemUsed == "metric" - ? convertTime(weatherData.dt, weatherData.timezone)[0].split( - ":" - )[0] - : timeToAMPM( - convertTime(weatherData.dt, weatherData.timezone)[0] - ).split(":")[0]} - :00{" "} - {systemUsed == "imperial" - ? isPM(convertTime(weatherData.dt, weatherData.timezone)[0]) - : ""} -

- )} +

+ { + weekday[ + new Date( + convertTime(weatherData.dt, weatherData.timezone).input + ).getUTCDay() + ] + } + ,{" "} + {systemUsed == "metric" + ? convertTime(weatherData.dt, weatherData.timezone)[0].split( + ":" + )[0] + : timeToAMPM( + convertTime(weatherData.dt, weatherData.timezone)[0] + ).split(":")[0]} + :00{" "} + {systemUsed == "imperial" + ? isPM(convertTime(weatherData.dt, weatherData.timezone)[0]) + : ""} +

+
- {weatherData && ( -
-
-

Humidity

-
- weatherIcon -
-

{weatherData.main.humidity}

-

%

-
-
-
-
-

Wind speed

-
- weatherIcon -
-

- {systemUsed == "metric" - ? weatherData.wind.speed - : mpsToMph(weatherData.wind.speed)} -

-

{systemUsed == "metric" ? "m/s" : "m/h"}

-
-
-
-
-

Wind direction

-
- weatherIcon -
-

{degToCompass(weatherData.wind.deg)}

-
-
-
-
-

Visibility

-
- weatherIcon -
-

- {systemUsed == "metric" - ? (weatherData.visibility / 1000).toPrecision(2) - : kmToM(weatherData.visibility / 1000)} -

-

{systemUsed == "metric" ? "km" : "miles"}

-
-
-
-
-

Sunrise

-
- weatherIcon -
-

- {systemUsed == "metric" - ? convertTime( - weatherData.sys.sunrise, - weatherData.timezone - )[0] - : timeToAMPM( - convertTime( - weatherData.sys.sunrise, - weatherData.timezone - )[0] - )} -

-

- {systemUsed == "imperial" - ? isPM( - convertTime( - weatherData.sys.sunrise, - weatherData.timezone - )[0] - ) - : ""} -

-
-
-
-
-

Sunset

-
- weatherIcon -
-

- {systemUsed == "metric" - ? convertTime( - weatherData.sys.sunset, - weatherData.timezone - )[0] - : timeToAMPM( - convertTime( - weatherData.sys.sunset, - weatherData.timezone - )[0] - )} -

-

- {systemUsed == "imperial" - ? isPM( - convertTime( - weatherData.sys.sunset, - weatherData.timezone - )[0] - ) - : ""} -

-
+
+
+

Humidity

+
+ weatherIcon +
+

{weatherData.main.humidity}

+

%

- )} +
+

Wind speed

+
+ weatherIcon +
+

+ {systemUsed == "metric" + ? weatherData.wind.speed + : mpsToMph(weatherData.wind.speed)} +

+

{systemUsed == "metric" ? "m/s" : "m/h"}

+
+
+
+
+

Wind direction

+
+ weatherIcon +
+

{degToCompass(weatherData.wind.deg)}

+
+
+
+
+

Visibility

+
+ weatherIcon +
+

+ {systemUsed == "metric" + ? (weatherData.visibility / 1000).toPrecision(2) + : kmToM(weatherData.visibility / 1000)} +

+

{systemUsed == "metric" ? "km" : "miles"}

+
+
+
+
+

Sunrise

+
+ weatherIcon +
+

+ {systemUsed == "metric" + ? convertTime( + weatherData.sys.sunrise, + weatherData.timezone + )[0] + : timeToAMPM( + convertTime( + weatherData.sys.sunrise, + weatherData.timezone + )[0] + )} +

+

+ {systemUsed == "imperial" + ? isPM( + convertTime( + weatherData.sys.sunrise, + weatherData.timezone + )[0] + ) + : ""} +

+
+
+
+
+

Sunset

+
+ weatherIcon +
+

+ {systemUsed == "metric" + ? convertTime( + weatherData.sys.sunset, + weatherData.timezone + )[0] + : timeToAMPM( + convertTime( + weatherData.sys.sunset, + weatherData.timezone + )[0] + )} +

+

+ {systemUsed == "imperial" + ? isPM( + convertTime( + weatherData.sys.sunset, + weatherData.timezone + )[0] + ) + : ""} +

+
+
+
+
+

Metric System

Imperial System

+ ) : ( +

Loading results

); } diff --git a/styles/Home.module.css b/styles/Home.module.css index 4416912..be91276 100644 --- a/styles/Home.module.css +++ b/styles/Home.module.css @@ -11,6 +11,10 @@ margin: 0 20px; } +.wrapperHide { + display: none; +} + .weatherWrapper { padding: 30px; }