2021-05-19 18:26:57 +02:00
|
|
|
import { useState, useEffect, Fragment } from 'react';
|
2021-06-07 12:17:10 +02:00
|
|
|
import { Weather, ApiResponse, Config } from '../../../interfaces';
|
2021-05-19 18:26:57 +02:00
|
|
|
import axios from 'axios';
|
|
|
|
|
|
|
|
import WeatherIcon from '../../UI/Icons/WeatherIcon/WeatherIcon';
|
|
|
|
|
|
|
|
import classes from './WeatherWidget.module.css';
|
|
|
|
|
|
|
|
const WeatherWidget = (): JSX.Element => {
|
|
|
|
const [weather, setWeather] = useState<Weather>({
|
|
|
|
externalLastUpdate: '',
|
|
|
|
tempC: 0,
|
|
|
|
tempF: 0,
|
|
|
|
isDay: 1,
|
2021-06-07 12:17:10 +02:00
|
|
|
cloud: 0,
|
2021-05-19 18:26:57 +02:00
|
|
|
conditionText: '',
|
|
|
|
conditionCode: 1000,
|
2021-06-01 14:54:47 +02:00
|
|
|
id: -1,
|
2021-05-19 18:26:57 +02:00
|
|
|
createdAt: new Date(),
|
|
|
|
updatedAt: new Date()
|
|
|
|
});
|
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
2021-06-07 12:17:10 +02:00
|
|
|
const [isCelsius, setIsCelsius] = useState(true);
|
2021-05-19 18:26:57 +02:00
|
|
|
|
2021-06-01 14:54:47 +02:00
|
|
|
// Initial request to get data
|
2021-05-19 18:26:57 +02:00
|
|
|
useEffect(() => {
|
2021-06-07 12:17:10 +02:00
|
|
|
// get weather
|
2021-05-19 18:26:57 +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);
|
|
|
|
})
|
|
|
|
.catch(err => console.log(err));
|
2021-06-07 12:17:10 +02:00
|
|
|
|
|
|
|
// get config
|
|
|
|
if (!localStorage.isCelsius) {
|
|
|
|
axios.get<ApiResponse<Config>>('/api/config/isCelsius')
|
|
|
|
.then((data) => {
|
|
|
|
setIsCelsius(parseInt(data.data.data.value) === 1);
|
|
|
|
localStorage.setItem('isCelsius', JSON.stringify(isCelsius));
|
|
|
|
})
|
|
|
|
.catch((err) => console.log(err));
|
|
|
|
} else {
|
|
|
|
setIsCelsius(JSON.parse(localStorage.isCelsius));
|
|
|
|
}
|
2021-05-19 18:26:57 +02:00
|
|
|
}, []);
|
|
|
|
|
2021-06-01 14:54:47 +02:00
|
|
|
// Open socket for data updates
|
|
|
|
useEffect(() => {
|
2021-06-11 00:09:25 +02:00
|
|
|
const socketProtocol = document.location.protocol === 'http:' ? 'ws:' : 'wss:';
|
|
|
|
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,
|
|
|
|
...data
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => webSocketClient.close();
|
|
|
|
}, []);
|
|
|
|
|
2021-05-19 18:26:57 +02:00
|
|
|
return (
|
|
|
|
<div className={classes.WeatherWidget}>
|
|
|
|
{isLoading
|
|
|
|
? 'loading'
|
2021-06-01 14:54:47 +02:00
|
|
|
: (weather.id > 0 &&
|
|
|
|
(<Fragment>
|
|
|
|
<div className={classes.WeatherIcon}>
|
|
|
|
<WeatherIcon
|
|
|
|
weatherStatusCode={weather.conditionCode}
|
|
|
|
isDay={weather.isDay}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className={classes.WeatherDetails}>
|
2021-06-07 12:17:10 +02:00
|
|
|
{isCelsius
|
|
|
|
? <span>{weather.tempC}°C</span>
|
|
|
|
: <span>{weather.tempF}°F</span>
|
|
|
|
}
|
|
|
|
<span>{weather.cloud}%</span>
|
2021-06-01 14:54:47 +02:00
|
|
|
</div>
|
|
|
|
</Fragment>)
|
2021-05-19 18:26:57 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WeatherWidget;
|