Skip to content

Commit

Permalink
fetching data
Browse files Browse the repository at this point in the history
  • Loading branch information
daryafurman committed Jan 23, 2024
1 parent cd5ed1c commit 3ed5b33
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
21 changes: 14 additions & 7 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { uid } from "uid";
import useLocalStorageState from "use-local-storage-state";
import List from "./components/list/List.jsx";
import { useEffect, useState } from "react";
import Weather from "./components/weather/Weather.jsx";

function App() {
const [activities, setActivities] = useLocalStorageState("activities", {
defaultValue: [],
});
const [weather, setWeather] = useState(null);
const isGoodWeather = true; // In the App.js, add a variable const isGoodWeather = true.
const [weather, setWeather] = useState("");
// const isGoodWeather = true; // In the App.js, add a variable const isGoodWeather = true.
const [isGoodWeather, setIsGoodWeather] = useState({});
const filteredActivities = activities.filter(
(activity) => activity.isGoodWeather === isGoodWeather
); //Filter the activities for those whose key isForGoodWeather is equal to the global isGoodWeather variable.
Expand All @@ -23,15 +25,15 @@ function App() {
const response = await fetch(weatherUrl);
if (response.ok) {
const weatherData = await response.json();

setWeatherData(weatherData);
setWeather(weatherData);
} else {
console.error("");
console.error("Failed to fetch weather data.");
}
} catch (error) {
console.error("Error!");
console.error("Error while fetching weather data:", error);
}
}
fetchWeatherData();

const intervalID = setInterval(fetchWeatherData, 5000);
return () => {
Expand All @@ -56,8 +58,13 @@ function App() {
}
return (
<>
<Weather weather={weather} />
<List
activities={filteredActivities}
deleteActivity={handleDeleteActivity}
isGoodWeather={weather}
/>
<Form onAddActivity={handleAddActivity} />
<List activities={activities} deleteActivity={handleDeleteActivity} />
</>
);
}
Expand Down
31 changes: 15 additions & 16 deletions src/components/list/List.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import "./List.css";
// import React from "react";
// import useLocalStorageState from "react";
import "./List.css";

// eslint-disable-next-line react/prop-types
export default function List({ activities, deleteActivity, isGoodWeather }) {
return (
<ul className="activity-list">
{activities.map((activity) => {
return (
<li className="list_item" key={activity.id}>
{activity.name} {activity.isGoodWeather ? "🌤" : "⚡️"} weather
activity
<button
className="delete-button"
onClick={() => deleteActivity(activity.id)}
>
x
</button>
</li>
);
})}
{activities.map((activity) => (
<li className="list_item" key={activity.id}>
{activity.name} {activity.isGoodWeather ? "🌤" : "⚡️"} weather
activity
<button
className="delete-button"
onClick={() => deleteActivity(activity.id)}
>
x
</button>
</li>
))}
</ul>
);
}
21 changes: 21 additions & 0 deletions src/components/weather/Weather.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.weather-details {
margin-top: 20px;
}

.weather-details div {
display: flex;
align-items: center;
justify-content: center;
margin-top: 10px;
}

.weather-details h1 {
font-size: 48px;
margin-right: 10px;
}

.weather-details h2 {
font-size: 20px;
color: #333;
margin-top: 10px;
}
15 changes: 15 additions & 0 deletions src/components/weather/Weather.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable react/prop-types */
import "./Weather.css";
export default function Weather({ weather }) {
return (
<div className="weather-details">
{!weather && <h2>weather is loading...</h2>}
{weather && (
<>
<h1>{weather.condition}</h1>
<h1>{weather.temperature}</h1>
</>
)}
</div>
);
}

0 comments on commit 3ed5b33

Please sign in to comment.