2021-05-19 18:26:57 +02:00
|
|
|
import axios from 'axios';
|
2023-08-03 23:24:56 -04:00
|
|
|
import { Fragment, useEffect, useState } from 'react';
|
2021-11-09 14:33:51 +01:00
|
|
|
import { useSelector } from 'react-redux';
|
2021-06-13 00:16:57 +02:00
|
|
|
|
2023-08-03 23:24:56 -04:00
|
|
|
import { ApiResponse, Weather } from '../../../interfaces';
|
2021-11-09 14:33:51 +01:00
|
|
|
import { State } from '../../../store/reducers';
|
2021-11-18 15:14:53 +01:00
|
|
|
import { weatherTemplate } from '../../../utility/templateObjects/weatherTemplate';
|
2023-08-03 23:24:56 -04:00
|
|
|
import { WeatherIcon } from '../../UI';
|
|
|
|
import classes from './WeatherWidget.module.css';
|
2021-06-13 00:16:57 +02:00
|
|
|
|
2023-08-03 23:24:56 -04:00
|
|
|
// Redux
|
|
|
|
// Typescript
|
|
|
|
// CSS
|
|
|
|
// UI
|
2021-11-09 14:33:51 +01:00
|
|
|
export const WeatherWidget = (): JSX.Element => {
|
2021-11-18 15:14:53 +01:00
|
|
|
const { loading: configLoading, config } = useSelector(
|
|
|
|
(state: State) => state.config
|
|
|
|
);
|
2021-06-13 00:16:57 +02:00
|
|
|
|
2021-11-18 15:14:53 +01:00
|
|
|
const [weather, setWeather] = useState<Weather>(weatherTemplate);
|
2021-05-19 18:26:57 +02:00
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
|
|
|
2021-06-01 14:54:47 +02:00
|
|
|
// Initial request to get data
|
2021-05-19 18:26:57 +02:00
|
|
|
useEffect(() => {
|
2021-10-22 13:31:02 +02:00
|
|
|
axios
|
|
|
|
.get<ApiResponse<Weather[]>>('/api/weather')
|
|
|
|
.then((data) => {
|
2021-06-01 14:54:47 +02:00
|
|
|
const weatherData = data.data.data[0];
|
|
|
|
if (weatherData) {
|
|
|
|
setWeather(weatherData);
|
|
|
|
}
|
2021-05-19 18:26:57 +02:00
|
|
|
setIsLoading(false);
|
|
|
|
})
|
2021-10-22 13:31:02 +02:00
|
|
|
.catch((err) => console.log(err));
|
2021-05-19 18:26:57 +02:00
|
|
|
}, []);
|
|
|
|
|
2021-06-01 14:54:47 +02:00
|
|
|
// Open socket for data updates
|
|
|
|
useEffect(() => {
|
2021-10-22 13:31:02 +02:00
|
|
|
const socketProtocol =
|
|
|
|
document.location.protocol === 'http:' ? 'ws:' : 'wss:';
|
2021-06-11 00:09:25 +02:00
|
|
|
const socketAddress = `${socketProtocol}//${window.location.host}/socket`;
|
|
|
|
const webSocketClient = new WebSocket(socketAddress);
|
2021-06-01 14:54:47 +02:00
|
|
|
|
|
|
|
webSocketClient.onmessage = (e) => {
|
|
|
|
const data = JSON.parse(e.data);
|
|
|
|
setWeather({
|
|
|
|
...weather,
|
2021-10-22 13:31:02 +02:00
|
|
|
...data,
|
|
|
|
});
|
|
|
|
};
|
2021-06-01 14:54:47 +02:00
|
|
|
|
|
|
|
return () => webSocketClient.close();
|
|
|
|
}, []);
|
|
|
|
|
2021-05-19 18:26:57 +02:00
|
|
|
return (
|
|
|
|
<div className={classes.WeatherWidget}>
|
2021-11-18 15:14:53 +01:00
|
|
|
{configLoading ||
|
2021-11-09 14:33:51 +01:00
|
|
|
(config.WEATHER_API_KEY && weather.id > 0 && (
|
2021-10-22 13:31:02 +02:00
|
|
|
<Fragment>
|
|
|
|
<div className={classes.WeatherIcon}>
|
|
|
|
<WeatherIcon
|
|
|
|
weatherStatusCode={weather.conditionCode}
|
|
|
|
isDay={weather.isDay}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className={classes.WeatherDetails}>
|
2021-11-18 15:14:53 +01:00
|
|
|
{/* TEMPERATURE */}
|
2021-11-09 14:33:51 +01:00
|
|
|
{config.isCelsius ? (
|
2021-10-22 13:31:02 +02:00
|
|
|
<span>{weather.tempC}°C</span>
|
|
|
|
) : (
|
2021-11-22 12:29:47 +01:00
|
|
|
<span>{Math.round(weather.tempF)}°F</span>
|
2021-10-22 13:31:02 +02:00
|
|
|
)}
|
2021-11-18 15:14:53 +01:00
|
|
|
|
|
|
|
{/* ADDITIONAL DATA */}
|
|
|
|
<span>{weather[config.weatherData]}%</span>
|
2021-10-22 13:31:02 +02:00
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
))}
|
2021-05-19 18:26:57 +02:00
|
|
|
</div>
|
2021-10-22 13:31:02 +02:00
|
|
|
);
|
|
|
|
};
|