-
Notifications
You must be signed in to change notification settings - Fork 0
/
weatherService.js
110 lines (91 loc) · 2.74 KB
/
weatherService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { DateTime } from "luxon";
const API_KEY = import.meta.env.VITE_REACT_APP_API_KEY;
const BASE_URL = "https://api.openweathermap.org/data/2.5";
const getWeatherData = async (infoType, searchParams) => {
const url = new URL(`${BASE_URL}/${infoType}`);
url.search = new URLSearchParams({ ...searchParams, appid: API_KEY });
const response = await fetch(url);
const data = await response.json();
if (response.ok) {
return data;
} else {
throw new Error(data.message);
}
};
const formatCurrentWeather = (data) => {
const {
coord: { lat, lon },
main: { temp, feels_like, temp_min, temp_max, humidity },
name,
dt,
sys: { country, sunrise, sunset },
weather,
timezone,
wind: { speed },
} = data;
const { main: details, icon } = weather[0];
return {
lat,
lon,
temp,
feels_like,
temp_min,
temp_max,
humidity,
name,
dt,
timezone,
country,
sunrise,
sunset,
details,
icon: iconUrlFromCode(icon),
speed,
formattedLocalTime: formatToLocalTime(dt, timezone),
};
};
// Function to format forecast weather data
const formatForecastWeather = (timezone, data) => {
const formattedDaily = data.slice(-6, -1).map((d) => ({
title: formatToLocalTime(d.dt, timezone, "ccc"),
temp: d.main.temp,
icon: d.weather[0].icon,
}));
const formattedHourly = data.slice(0, 5).map((h) => ({
title: formatToLocalTime(h.dt, timezone, "hh:mm a"),
temp: h.main.temp,
icon: h.weather[0].icon,
}));
return { timezone, daily: formattedDaily, hourly: formattedHourly };
};
// Function to get and format weather data (current and forecast)
const getFormattedWeatherData = async (searchParams) => {
try {
const currentWeatherData = await getWeatherData("weather", searchParams);
const formattedCurrentWeather = formatCurrentWeather(currentWeatherData);
const { lat, lon } = formattedCurrentWeather;
const forecastWeatherData = await getWeatherData("forecast", {
lat,
lon,
exclude: "current,minutely,alerts",
units: searchParams.units,
});
const { timezone } = formattedCurrentWeather;
const formattedForecastWeather = formatForecastWeather(
timezone,
forecastWeatherData.list
);
return { ...formattedCurrentWeather, ...formattedForecastWeather };
} catch (error) {
console.error("Error fetching weather data:", error);
}
};
const formatToLocalTime = (
secs,
offset,
format = "cccc, dd LLL yyyy' | Local time: 'hh:mm a"
) => DateTime.fromSeconds(secs + offset, { zone: "utc" }).toFormat(format);
const iconUrlFromCode = (code) =>
`http://openweathermap.org/img/wn/${code}@2x.png`;
export default getFormattedWeatherData;
export { formatToLocalTime, iconUrlFromCode };