changed path
This commit is contained in:
36
src/utils/apiFunc.ts
Normal file
36
src/utils/apiFunc.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { myToast } from "./toastify";
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
export const fetchWeather = async (
|
||||
city: string,
|
||||
apiKey: string,
|
||||
units: 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) {
|
||||
if (Cookies.get("apiKey") === undefined || Cookies.get("apiKey") === "") {
|
||||
myToast("You have to enter an API key!", "error");
|
||||
} else {
|
||||
myToast(
|
||||
"You are not authorized to access this resource. Please check your API key. (Error: x4010)",
|
||||
"error"
|
||||
);
|
||||
}
|
||||
} else if (response.ok) {
|
||||
return response.json();
|
||||
} else {
|
||||
myToast("Error fetching location data. (Error: x32)", "error");
|
||||
}
|
||||
});
|
||||
const lat = location[0].lat;
|
||||
const lon = location[0].lon;
|
||||
|
||||
// Get weather data
|
||||
const weather = await fetch(
|
||||
`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}&units=${units}`
|
||||
).then((response) => response.json());
|
||||
localStorage.setItem("weather", JSON.stringify(weather));
|
||||
};
|
19
src/utils/changeAPIcookie.ts
Normal file
19
src/utils/changeAPIcookie.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import Cookies from "js-cookie";
|
||||
import { myToast } from "./toastify";
|
||||
|
||||
export const changeAPIcookie = (newApiKey: string) => {
|
||||
let apiKey15 = newApiKey.slice(0, 15);
|
||||
Cookies.set("apiKey", newApiKey);
|
||||
if (Cookies.get("apiKey") === newApiKey) {
|
||||
myToast(
|
||||
"API key updated successfully!" +
|
||||
" " +
|
||||
"Your new API key: " +
|
||||
apiKey15 +
|
||||
"...",
|
||||
"success"
|
||||
);
|
||||
} else {
|
||||
myToast("Failed to update API key. (Error: x30)", "error");
|
||||
}
|
||||
};
|
17
src/utils/toastify.ts
Normal file
17
src/utils/toastify.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { toast, type ToastOptions } from "react-toastify";
|
||||
|
||||
export type ToastType = "success" | "error" | "info" | "warning";
|
||||
|
||||
export const myToast = (message: string, msgType: ToastType) => {
|
||||
let config: ToastOptions = {
|
||||
position: "top-right",
|
||||
autoClose: 5000,
|
||||
hideProgressBar: false,
|
||||
closeOnClick: true,
|
||||
pauseOnHover: true,
|
||||
draggable: true,
|
||||
progress: undefined,
|
||||
theme: "dark",
|
||||
};
|
||||
toast[msgType](message, config);
|
||||
};
|
Reference in New Issue
Block a user