2021-06-01 14:54:47 +02:00
|
|
|
import { useState, ChangeEvent, useEffect, FormEvent } from 'react';
|
2021-05-27 12:30:09 +02:00
|
|
|
import axios from 'axios';
|
|
|
|
|
2021-06-13 00:16:57 +02:00
|
|
|
// Redux
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { createNotification, updateConfig } from '../../../store/actions';
|
|
|
|
|
|
|
|
// Typescript
|
|
|
|
import { ApiResponse, GlobalState, NewNotification, Weather, WeatherForm } from '../../../interfaces';
|
|
|
|
|
|
|
|
// UI
|
2021-05-27 12:30:09 +02:00
|
|
|
import InputGroup from '../../UI/Forms/InputGroup/InputGroup';
|
|
|
|
import Button from '../../UI/Buttons/Button/Button';
|
|
|
|
|
2021-06-13 00:16:57 +02:00
|
|
|
// Utils
|
|
|
|
import { searchConfig } from '../../../utility';
|
2021-05-27 12:30:09 +02:00
|
|
|
|
2021-06-01 14:54:47 +02:00
|
|
|
interface ComponentProps {
|
|
|
|
createNotification: (notification: NewNotification) => void;
|
2021-06-13 00:16:57 +02:00
|
|
|
updateConfig: (formData: WeatherForm) => void;
|
|
|
|
loading: boolean;
|
2021-06-01 14:54:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const WeatherSettings = (props: ComponentProps): JSX.Element => {
|
2021-06-13 23:21:35 +02:00
|
|
|
// Initial state
|
2021-06-13 00:16:57 +02:00
|
|
|
const [formData, setFormData] = useState<WeatherForm>({
|
2021-05-27 12:30:09 +02:00
|
|
|
WEATHER_API_KEY: '',
|
|
|
|
lat: 0,
|
|
|
|
long: 0,
|
|
|
|
isCelsius: 1
|
|
|
|
})
|
|
|
|
|
2021-06-13 23:21:35 +02:00
|
|
|
// Get config
|
2021-05-27 12:30:09 +02:00
|
|
|
useEffect(() => {
|
2021-06-13 00:16:57 +02:00
|
|
|
setFormData({
|
|
|
|
WEATHER_API_KEY: searchConfig('WEATHER_API_KEY', ''),
|
|
|
|
lat: searchConfig('lat', 0),
|
|
|
|
long: searchConfig('long', 0),
|
|
|
|
isCelsius: searchConfig('isCelsius', 1)
|
|
|
|
})
|
|
|
|
}, [props.loading]);
|
2021-05-27 12:30:09 +02:00
|
|
|
|
2021-06-13 23:21:35 +02:00
|
|
|
// Form handler
|
2021-06-13 00:16:57 +02:00
|
|
|
const formSubmitHandler = async (e: FormEvent) => {
|
2021-05-27 13:21:11 +02:00
|
|
|
e.preventDefault();
|
|
|
|
|
2021-06-09 22:26:39 +02:00
|
|
|
// Check for api key input
|
|
|
|
if ((formData.lat || formData.long) && !formData.WEATHER_API_KEY) {
|
|
|
|
props.createNotification({
|
|
|
|
title: 'Warning',
|
2021-06-13 23:21:35 +02:00
|
|
|
message: 'API key is missing. Weather Module will NOT work'
|
2021-06-09 22:26:39 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save settings
|
2021-06-13 00:16:57 +02:00
|
|
|
await props.updateConfig(formData);
|
|
|
|
|
|
|
|
// Update weather
|
|
|
|
axios.get<ApiResponse<Weather>>('/api/weather/update')
|
2021-06-07 12:17:10 +02:00
|
|
|
.then(() => {
|
2021-06-01 14:54:47 +02:00
|
|
|
props.createNotification({
|
|
|
|
title: 'Success',
|
2021-06-13 00:16:57 +02:00
|
|
|
message: 'Weather updated'
|
2021-06-01 14:54:47 +02:00
|
|
|
})
|
|
|
|
})
|
2021-06-13 00:16:57 +02:00
|
|
|
.catch((err) => {
|
|
|
|
props.createNotification({
|
|
|
|
title: 'Error',
|
|
|
|
message: err.response.data.error
|
|
|
|
})
|
|
|
|
});
|
2021-05-27 13:21:11 +02:00
|
|
|
}
|
|
|
|
|
2021-06-13 23:21:35 +02:00
|
|
|
// Input handler
|
|
|
|
const inputChangeHandler = (e: ChangeEvent<HTMLInputElement | HTMLSelectElement>, isNumber?: boolean) => {
|
|
|
|
let value: string | number = e.target.value;
|
|
|
|
|
|
|
|
if (isNumber) {
|
|
|
|
value = parseFloat(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
setFormData({
|
|
|
|
...formData,
|
|
|
|
[e.target.name]: value
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-27 12:30:09 +02:00
|
|
|
return (
|
2021-05-27 13:21:11 +02:00
|
|
|
<form onSubmit={(e) => formSubmitHandler(e)}>
|
|
|
|
<InputGroup>
|
2021-06-13 23:21:35 +02:00
|
|
|
<label htmlFor='WEATHER_API_KEY'>API key</label>
|
2021-05-27 13:21:11 +02:00
|
|
|
<input
|
|
|
|
type='text'
|
|
|
|
id='WEATHER_API_KEY'
|
|
|
|
name='WEATHER_API_KEY'
|
|
|
|
placeholder='secret'
|
|
|
|
value={formData.WEATHER_API_KEY}
|
|
|
|
onChange={(e) => inputChangeHandler(e)}
|
|
|
|
/>
|
|
|
|
<span>
|
|
|
|
Using
|
|
|
|
<a
|
|
|
|
href='https://www.weatherapi.com/pricing.aspx'
|
|
|
|
target='blank'>
|
|
|
|
{' '}Weather API
|
|
|
|
</a>
|
2021-06-09 22:26:39 +02:00
|
|
|
. Key is required for weather module to work.
|
2021-05-27 13:21:11 +02:00
|
|
|
</span>
|
|
|
|
</InputGroup>
|
|
|
|
<InputGroup>
|
2021-06-13 23:21:35 +02:00
|
|
|
<label htmlFor='lat'>Location latitude</label>
|
2021-05-27 13:21:11 +02:00
|
|
|
<input
|
|
|
|
type='number'
|
|
|
|
id='lat'
|
|
|
|
name='lat'
|
|
|
|
placeholder='52.22'
|
|
|
|
value={formData.lat}
|
|
|
|
onChange={(e) => inputChangeHandler(e, true)}
|
2021-06-18 14:12:17 +02:00
|
|
|
step='any'
|
|
|
|
lang='en-150'
|
2021-05-27 13:21:11 +02:00
|
|
|
/>
|
|
|
|
<span>
|
|
|
|
You can use
|
|
|
|
<a
|
|
|
|
href='https://www.latlong.net/convert-address-to-lat-long.html'
|
|
|
|
target='blank'>
|
|
|
|
{' '}latlong.net
|
|
|
|
</a>
|
|
|
|
</span>
|
|
|
|
</InputGroup>
|
|
|
|
<InputGroup>
|
2021-06-13 23:21:35 +02:00
|
|
|
<label htmlFor='long'>Location longitude</label>
|
2021-05-27 13:21:11 +02:00
|
|
|
<input
|
|
|
|
type='number'
|
|
|
|
id='long'
|
|
|
|
name='long'
|
|
|
|
placeholder='21.01'
|
|
|
|
value={formData.long}
|
|
|
|
onChange={(e) => inputChangeHandler(e, true)}
|
2021-06-18 14:12:17 +02:00
|
|
|
step='any'
|
|
|
|
lang='en-150'
|
2021-05-27 13:21:11 +02:00
|
|
|
/>
|
|
|
|
</InputGroup>
|
|
|
|
<InputGroup>
|
2021-06-13 23:21:35 +02:00
|
|
|
<label htmlFor='isCelsius'>Temperature unit</label>
|
2021-05-27 13:21:11 +02:00
|
|
|
<select
|
|
|
|
id='isCelsius'
|
|
|
|
name='isCelsius'
|
|
|
|
onChange={(e) => inputChangeHandler(e, true)}
|
|
|
|
value={formData.isCelsius}
|
|
|
|
>
|
|
|
|
<option value={1}>Celsius</option>
|
|
|
|
<option value={0}>Fahrenheit</option>
|
|
|
|
</select>
|
|
|
|
</InputGroup>
|
|
|
|
<Button>Save changes</Button>
|
|
|
|
</form>
|
2021-05-27 12:30:09 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-13 00:16:57 +02:00
|
|
|
const mapStateToProps = (state: GlobalState) => {
|
|
|
|
return {
|
|
|
|
loading: state.config.loading
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, { createNotification, updateConfig })(WeatherSettings);
|