Skip to content

Commit

Permalink
AC-258 get favorite station to show in home page
Browse files Browse the repository at this point in the history
  • Loading branch information
Minotriz02 authored and Minotriz02 committed Nov 13, 2024
1 parent 4d7a80a commit ff70b9d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/src/components/stationCard/StationCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ function StationCard({ loading, msgError, station }) {
const fetchLastDailyWeather = async () => {
try {
const response = await Services.getLastDailyWeather(station.id);
setLastDataStation(response);
console.log("response", response[0]);
setLastDataStation(response[0]);
} catch (error) {
console.error(
"Error al cargar la última información de las estaciones:",
Expand Down Expand Up @@ -83,12 +84,13 @@ function StationCard({ loading, msgError, station }) {
.find((item) => item.measure === "t_min")
?.value.toFixed(1) ?? "N/A"
: "N/A"}{" "}
-{" "}
°C -{" "}
{lastDataStation?.climaticData
? lastDataStation.climaticData
.find((item) => item.measure === "t_max")
?.value.toFixed(1) ?? "N/A"
: "N/A"}
: "N/A"}{" "}
°C
</span>
</div>
<div className="d-flex flex-column gap-1">
Expand All @@ -106,15 +108,15 @@ function StationCard({ loading, msgError, station }) {
</div>
<div className="d-flex flex-column justify-content-between align-items-end">
<div className="d-flex flex-column align-items-end">
<span>
<span className="text-capitalize">
{lastDataStation?.date
? getDayOfWeek(lastDataStation.date)
: "N/A"}
</span>
<span>{lastDataStation?.date ?? "N/A"}</span>
</div>
<div>
<span>
<span className="text-capitalize">
<IconMapPin size={24} className="me-2" />
{station.municipality}, {station.state}
</span>
Expand Down
17 changes: 16 additions & 1 deletion src/src/pages/home/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Home = () => {

const [stations, setStations] = useState([]);
const [nearestStation, setNearestStation] = useState(null);
const [favoriteStation, setFavoriteStation] = useState(null);
const [error, setError] = useState(null);
const [loading, setLoading] = useState(true);

Expand Down Expand Up @@ -66,9 +67,23 @@ const Home = () => {
}
};

const favorites = JSON.parse(localStorage.getItem("favorites")) || [];
if (favorites.length > 0) {
setFavoriteStation(
stations.find((station) => station.id === favorites[0])
);
return;
}
getUserLocation();
}, [stations]);

// useEffect(() => {
// const favorites = JSON.parse(localStorage.getItem("favorites")) || [];
// if (favorites.length > 0) {
// setFavoriteStation(favorites[0]);
// }
// }, []);

return (
<div>
{/* Sección de bienvenida */}
Expand Down Expand Up @@ -96,7 +111,7 @@ const Home = () => {
<StationCard
loading={loading}
msgError={error}
station={nearestStation}
station={favoriteStation || nearestStation}
/>
</Row>
</Container>
Expand Down

0 comments on commit ff70b9d

Please sign in to comment.