feat: enhance weather fetching experience with loading state and notifications

This commit is contained in:
2025-07-29 23:40:24 +02:00
parent bf8c270171
commit dcfbbb2f22
7 changed files with 47 additions and 12 deletions

View File

@@ -1,12 +1,9 @@
import { myToast } from "./toastify";
export const fetchWeather = async (city: string, apiKey: string) => {
// Get location data
const location = await fetch(
`http://api.openweathermap.org/geo/1.0/direct?q=${city}&appid=${apiKey}`
).then((response) => {
if (response.status === 401) {
myToast("Request Failed! Check API key and city name.", "error");
throw new Error("Network response was not ok");
} else if (response.ok) {
return response.json();
@@ -20,5 +17,4 @@ export const fetchWeather = async (city: string, apiKey: string) => {
`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}`
).then((response) => response.json());
console.log(weather);
myToast("Successfully fetched weather data", "success");
};

View File

@@ -5,7 +5,7 @@ export type ToastType = "success" | "error" | "info" | "warning";
export const myToast = (message: string, msgType: ToastType) => {
let config: ToastOptions = {
position: "top-right",
autoClose: 5000,
autoClose: 3000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,