Skip to content

Commit 9b055c2

Browse files
feat: add enums for weather and animation statuses
1 parent 1151226 commit 9b055c2

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

src/app/components/AnimatedBox.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,32 @@
22

33
import { useState } from "react";
44
import styles from "./AnimatedBox.module.css";
5+
import { AnimationStatus } from "../enum";
56

67
export default function AnimatedBox() {
7-
const [isAnimating, setIsAnimating] = useState(false);
8+
const [status, setStatus] = useState<AnimationStatus>(AnimationStatus.Idle);
89

910
const toggleAnimation = () => {
10-
setIsAnimating((prev) => !prev);
11+
setStatus((prev) =>
12+
prev === AnimationStatus.Running
13+
? AnimationStatus.Completed
14+
: AnimationStatus.Running
15+
);
1116
};
1217

1318
return (
1419
<div>
1520
<button onClick={toggleAnimation} className={styles.toggleButton}>
16-
{isAnimating ? "Stop Animation" : "Start Animation"}
21+
{status === AnimationStatus.Running
22+
? "Stop Animation"
23+
: "Start Animation"}
1724
</button>
1825
<div
19-
className={`${styles.box} ${isAnimating ? styles.animate : ""}`}
26+
className={`${styles.box} ${
27+
status === AnimationStatus.Running ? styles.animate : ""
28+
}`}
2029
></div>
30+
<p>Status: {status}</p>
2131
</div>
2232
);
2333
}

src/app/enum.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// src/app/enum.ts
2+
// Enum for weather fetch status
3+
4+
export enum WeatherFetchStatus {
5+
Idle = "idle",
6+
Loading = "loading",
7+
Success = "success",
8+
Error = "error",
9+
}
10+
11+
// Enum for animation status
12+
13+
export enum AnimationStatus {
14+
Idle = "idle",
15+
Running = "running",
16+
Completed = "completed",
17+
Error = "error",
18+
}

src/app/weather-widget.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use client";
22
import { useState, useEffect } from "react";
3+
import { WeatherFetchStatus } from "./enum";
34

45
export default function WeatherWidget() {
56
const [weather, setWeather] = useState<WeatherData | null>(null);
@@ -11,6 +12,9 @@ export default function WeatherWidget() {
1112
longitude: null,
1213
});
1314
const [error, setError] = useState("");
15+
const [status, setStatus] = useState<WeatherFetchStatus>(
16+
WeatherFetchStatus.Idle
17+
);
1418

1519
useEffect(() => {
1620
if (navigator.geolocation) {
@@ -23,10 +27,12 @@ export default function WeatherWidget() {
2327
},
2428
() => {
2529
setError("Unable to retrieve your location.");
30+
setStatus(WeatherFetchStatus.Error);
2631
}
2732
);
2833
} else {
2934
setError("Geolocation is not supported by your browser.");
35+
setStatus(WeatherFetchStatus.Error);
3036
}
3137
}, []);
3238

@@ -45,16 +51,19 @@ export default function WeatherWidget() {
4551
latitude: number,
4652
longitude: number
4753
): Promise<void> => {
54+
setStatus(WeatherFetchStatus.Loading);
4855
try {
4956
const response = await fetch(
5057
`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&current_weather=true`
5158
);
5259
if (!response.ok) throw new Error("Failed to fetch weather data");
5360
const data: { current_weather: WeatherData } = await response.json();
5461
setWeather(data.current_weather);
62+
setStatus(WeatherFetchStatus.Success);
5563
} catch (err) {
5664
console.error(err);
5765
setError("Unable to fetch weather data.");
66+
setStatus(WeatherFetchStatus.Error);
5867
}
5968
};
6069

@@ -64,13 +73,15 @@ export default function WeatherWidget() {
6473
>
6574
<h3>Current Weather</h3>
6675
{error && <p style={{ color: "red" }}>{error}</p>}
67-
{weather ? (
76+
{status === WeatherFetchStatus.Loading && <p>Loading...</p>}
77+
{status === WeatherFetchStatus.Success && weather && (
6878
<div>
6979
<p>Temperature: {weather.temperature}°C</p>
7080
<p>Wind Speed: {weather.windspeed} km/h</p>
7181
</div>
72-
) : (
73-
<p>Loading...</p>
82+
)}
83+
{status === WeatherFetchStatus.Error && !weather && (
84+
<p>Failed to load weather.</p>
7485
)}
7586
</div>
7687
);

0 commit comments

Comments
 (0)