mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-08 22:45:49 +01:00
🐛 Fixing issues with weahter module
This commit is contained in:
@@ -27,6 +27,10 @@ export const WeatherModule: IModule = {
|
|||||||
name: 'Display in Fahrenheit',
|
name: 'Display in Fahrenheit',
|
||||||
value: false,
|
value: false,
|
||||||
},
|
},
|
||||||
|
location: {
|
||||||
|
name: 'Current location',
|
||||||
|
value: '',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -128,27 +132,30 @@ export function WeatherIcon(props: any) {
|
|||||||
|
|
||||||
export default function WeatherComponent(props: any) {
|
export default function WeatherComponent(props: any) {
|
||||||
// Get location from browser
|
// Get location from browser
|
||||||
const [location, setLocation] = useState({ lat: 0, lng: 0 });
|
|
||||||
const { config } = useConfig();
|
const { config } = useConfig();
|
||||||
const [weather, setWeather] = useState({} as WeatherResponse);
|
const [weather, setWeather] = useState({} as WeatherResponse);
|
||||||
|
const cityInput: string =
|
||||||
|
(config?.modules?.[WeatherModule.title]?.options?.location?.value as string) ?? '';
|
||||||
const isFahrenheit: boolean =
|
const isFahrenheit: boolean =
|
||||||
config?.modules?.[WeatherModule.title]?.options?.freedomunit?.value ?? false;
|
(config?.modules?.[WeatherModule.title]?.options?.freedomunit?.value as boolean) ?? false;
|
||||||
|
|
||||||
if ('geolocation' in navigator && location.lat === 0 && location.lng === 0) {
|
|
||||||
navigator.geolocation.getCurrentPosition((position) => {
|
|
||||||
setLocation({ lat: position.coords.latitude, lng: position.coords.longitude });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
axios
|
axios
|
||||||
.get(
|
.get(`https://geocoding-api.open-meteo.com/v1/search?name=${cityInput}`)
|
||||||
`https://api.open-meteo.com/v1/forecast?latitude=${location.lat}&longitude=${location.lng}&daily=weathercode,temperature_2m_max,temperature_2m_min¤t_weather=true&timezone=Europe%2FLondon`
|
.then((response) => {
|
||||||
)
|
// Check if results exists
|
||||||
.then((res) => {
|
const { latitude, longitude } = response.data.results
|
||||||
setWeather(res.data);
|
? response.data.results[0]
|
||||||
|
: { latitude: 0, longitude: 0 };
|
||||||
|
axios
|
||||||
|
.get(
|
||||||
|
`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&daily=weathercode,temperature_2m_max,temperature_2m_min¤t_weather=true&timezone=Europe%2FLondon`
|
||||||
|
)
|
||||||
|
.then((res) => {
|
||||||
|
setWeather(res.data);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}, []);
|
}, [cityInput]);
|
||||||
if (!weather.current_weather) {
|
if (!weather.current_weather) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user