31 lines
746 B
TypeScript
31 lines
746 B
TypeScript
import { myToast } from "./toastify";
|
|
|
|
export const fetchWeather = async (city: string, units: string) => {
|
|
try {
|
|
const response = await fetch(
|
|
`http://localhost:7001/api/fetchWeather?city=${encodeURIComponent(
|
|
city
|
|
)}&units=${encodeURIComponent(units)}`,
|
|
{
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
}
|
|
);
|
|
|
|
const responseData = await response.json();
|
|
|
|
if (!response.ok) {
|
|
myToast(responseData.error, "error");
|
|
return;
|
|
}
|
|
localStorage.setItem("weather", JSON.stringify(responseData.data));
|
|
return;
|
|
} catch (error) {
|
|
const errorMsg = JSON.stringify(error);
|
|
myToast(errorMsg, "error");
|
|
return null;
|
|
}
|
|
};
|