refactor weather

This commit is contained in:
Georgi Gardev
2023-11-19 11:26:00 +02:00
parent e871b7bc18
commit 6b0020379e

View File

@@ -1,6 +1,5 @@
import { createSignal } from 'solid-js';
import { Resource, createResource } from 'solid-js';
import { WeatherData } from '~/types';
import { Accessor } from 'solid-js';
function fetchWeather(): Promise<WeatherData> {
const lat = import.meta.env.VITE_OPEN_WEATHER_LAT;
@@ -10,22 +9,18 @@ function fetchWeather(): Promise<WeatherData> {
`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${appId}&units=metric`
)
.then((res) => res.json())
.then((data) => {
return {
...data.weather[0],
...data.main,
visibility: data.visibility,
wind_speed: data.wind.speed,
wind_deg: data.wind.deg,
sunrise: data.sys.sunrise,
sunset: data.sys.sunset,
};
});
.then((data) => ({
...data.weather[0],
...data.main,
visibility: data.visibility,
wind_speed: data.wind.speed,
wind_deg: data.wind.deg,
sunrise: data.sys.sunrise,
sunset: data.sys.sunset,
}));
}
export const createWeather = (): Accessor<WeatherData | undefined> => {
const [weather, setWeather] = createSignal<WeatherData>();
fetchWeather().then(setWeather);
export function createWeather(): Resource<WeatherData> {
const [weather] = createResource<WeatherData>(fetchWeather);
return weather;
};
}