Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions app/components/WeatherApp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"use client";

import React, { useState, useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";
import { fetchWeatherData, setCity } from "app/redux/weatherSlice.js";
import WeatherChart from "./WeatherChart";

function WeatherApp() {
const dispatch = useDispatch();
const { city, weatherData, loading, error } = useSelector(
(state) => state.weather
);
const [inputCity, setInputCity] = useState(city);

useEffect(() => {
if (city) {
dispatch(fetchWeatherData(city));
}
}, [city, dispatch]);

const handleInputChange = (e) => {
setInputCity(e.target.value);
};

const handleFormSubmit = (e) => {
e.preventDefault();
dispatch(setCity(inputCity));
setInputCity('')
};

if (loading) return <p className="loading">Fetching weather data for {city}...</p>;
if (error) return <p className="error">Failed to fetch weather data for {city} {error}</p>;

const temperatureData = weatherData
? weatherData.list.map((item) => {
console.log(item)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not submit console logs

const celsius = item.main.temp

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const celsius = item.main.temp
const rawTemperatureInCelsius = item.main.temp

const fahrenheit = (celsius * (9 / 5) + 32); // Convert Celsius to Fahrenheit

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export to a util function


return fahrenheit
})
: [];
const pressureData = weatherData
? weatherData.list.map((item) => item.main.pressure)
: [];
const humidityData = weatherData
? weatherData.list.map((item) => item.main.humidity)
Comment on lines +43 to +47

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dry, you can use a function for this

: [];

return (
<>
<h1 className="heading">Weather Data</h1>
<form className="form" onSubmit={handleFormSubmit}>
<input
type="text"
value={inputCity}
onChange={handleInputChange}
placeholder="Enter City"
/>
<button className="btn" type="submit">
Get Weather
</button>
</form>

{weatherData && (
<>
<div className="content">
<div className="chartsDiv">
<WeatherChart
data={temperatureData}
title="Temperature"
units="°F"
color="red"
/>

<WeatherChart
data={pressureData}
title="Pressure"
units="hPa"
color="blue"
/>
<WeatherChart
data={humidityData}
title="Humidity"
units="%"
color="green"
/>
</div>
</div>
</>
)}
</>
);
}

export default WeatherApp;
53 changes: 53 additions & 0 deletions app/components/WeatherChart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use client";

import React from "react";
import {
Sparklines,
SparklinesLine,
SparklinesReferenceLine,
} from "react-sparklines";

function calculateAverage(data) {
if (!data || data.length === 0) {
return 0; // Return 0 for empty or invalid data
}

let sum = 0;
let validCount = 0;
for (const point of data) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use forEach, safer than this

if (typeof point === "number" && !isNaN(point)) {
sum += point;
validCount++;
} else {
console.warn("Invalid data point:", point);
}

}

if (validCount === 0) {
return 0; // Return 0 if no valid data points
}

return sum / validCount;
}

function WeatherChart({ data, title, units, color }) {
const average = calculateAverage(data)
console.log(average)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

return (
<div className="weatherChart">
<h3 className="chartHeading">
{title} ({units})
</h3>
<Sparklines data={data} height={120} width={200}>
<SparklinesLine color={color} />
<SparklinesReferenceLine value={average} type="mean" />
</Sparklines>
<p className="avg">
Average: {average.toFixed(2)} {units}
</p>
</div>
);
}

export default WeatherChart;
Binary file removed app/favicon.ico
Binary file not shown.
153 changes: 65 additions & 88 deletions app/globals.css
Original file line number Diff line number Diff line change
@@ -1,107 +1,84 @@
:root {
--max-width: 1100px;
--border-radius: 12px;
--font-mono: ui-monospace, Menlo, Monaco, 'Cascadia Mono', 'Segoe UI Mono',
'Roboto Mono', 'Oxygen Mono', 'Ubuntu Monospace', 'Source Code Pro',
'Fira Mono', 'Droid Sans Mono', 'Courier New', monospace;

--foreground-rgb: 0, 0, 0;
--background-start-rgb: 214, 219, 220;
--background-end-rgb: 255, 255, 255;

--primary-glow: conic-gradient(
from 180deg at 50% 50%,
#16abff33 0deg,
#0885ff33 55deg,
#54d6ff33 120deg,
#0071ff33 160deg,
transparent 360deg
);
--secondary-glow: radial-gradient(
rgba(255, 255, 255, 1),
rgba(255, 255, 255, 0)
);
background-color: #000000;
color: blanchedalmond;
font-family: "Lucida Console", Monaco, monospace;
font-size: 16px;
letter-spacing: 2px;
word-spacing: 2px;
color: #000000;
font-weight: normal;
text-decoration: none;
font-style: normal;
font-variant: small-caps;
text-transform: uppercase;
}

--tile-start-rgb: 239, 245, 249;
--tile-end-rgb: 228, 232, 233;
--tile-border: conic-gradient(
#00000080,
#00000040,
#00000030,
#00000020,
#00000010,
#00000010,
#00000080
);
.content {
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 3px solid #ccc;
border-radius: 5px;
}

--callout-rgb: 238, 240, 241;
--callout-border-rgb: 172, 175, 176;
--card-rgb: 180, 185, 188;
--card-border-rgb: 131, 134, 135;
.loading,
.error {
text-align: center;
margin-top: 20px;
}

@media (prefers-color-scheme: dark) {
:root {
--foreground-rgb: 255, 255, 255;
--background-start-rgb: 0, 0, 0;
--background-end-rgb: 0, 0, 0;
.error {
color: red;
}

--primary-glow: radial-gradient(rgba(1, 65, 255, 0.4), rgba(1, 65, 255, 0));
--secondary-glow: linear-gradient(
to bottom right,
rgba(1, 65, 255, 0),
rgba(1, 65, 255, 0),
rgba(1, 65, 255, 0.3)
);
.form {
margin-bottom: 20px;
text-align: center;
}

--tile-start-rgb: 2, 13, 46;
--tile-end-rgb: 2, 5, 19;
--tile-border: conic-gradient(
#ffffff80,
#ffffff40,
#ffffff30,
#ffffff20,
#ffffff10,
#ffffff10,
#ffffff80
);
.heading {
text-align: center;
margin-bottom: 20px;
color: #595757;
}

--callout-rgb: 20, 20, 20;
--callout-border-rgb: 108, 108, 108;
--card-rgb: 100, 100, 100;
--card-border-rgb: 200, 200, 200;
}
.btn {
margin-top: 20px;
padding: 4px 10px;
background-color: #425263;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
}

* {
box-sizing: border-box;
padding: 0;
margin: 0;
.weatherChart {
padding: 20px;
border: 1px solid #595757;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
background-color: #e2e1e1;
}

html,
body {
max-width: 100vw;
overflow-x: hidden;
.chartHeading {
margin-bottom: 10px;
text-align: center;
color: #333;
}

body {
color: rgb(var(--foreground-rgb));
background: linear-gradient(
to bottom,
transparent,
rgb(var(--background-end-rgb))
)
rgb(var(--background-start-rgb));
Sparklines {
height: 120px;
width: 200px;
}

a {
color: inherit;
text-decoration: none;
.avg {
margin-top: 10px;
text-align: center;
color: #555;
}

@media (prefers-color-scheme: dark) {
html {
color-scheme: dark;
}
.chartsDiv {
display: flex;
flex-direction: column;
gap: 20px;
}
Loading