feat: enhance weather fetching experience with loading state and notifications
This commit is contained in:
@@ -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");
|
||||
};
|
||||
|
Reference in New Issue
Block a user