refactor weather
This commit is contained in:
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user