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
|
2021-10-22 13:31:02 +02:00
|
|
|
import {
|
|
|
|
ApiResponse,
|
|
|
|
Config,
|
|
|
|
GlobalState,
|
|
|
|
NewNotification,
|
|
|
|
Weather,
|
|
|
|
WeatherForm,
|
|
|
|
} from '../../../interfaces';
|
2021-06-13 00:16:57 +02:00
|
|
|
|
|
|
|
// 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
|
2021-10-22 13:31:02 +02:00
|
|
|
import { inputHandler, weatherSettingsTemplate } 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-10-22 13:31:02 +02:00
|
|
|
config: Config;
|
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-10-22 13:31:02 +02:00
|
|
|
const [formData, setFormData] = useState<WeatherForm>(
|
|
|
|
weatherSettingsTemplate
|
|
|
|
);
|
2021-05-27 12:30:09 +02:00
|
|
|
|
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({
|
2021-10-22 13:31:02 +02:00
|
|
|
...props.config,
|
|
|
|
});
|
2021-06-13 00:16:57 +02:00
|
|
|
}, [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-10-22 13:31:02 +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);
|
2021-10-22 13:31:02 +02:00
|
|
|
|
2021-06-13 00:16:57 +02:00
|
|
|
// Update weather
|
2021-10-22 13:31:02 +02:00
|
|
|
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-10-22 13:31:02 +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',
|
2021-10-22 13:31:02 +02:00
|
|
|
message: err.response.data.error,
|
|
|
|
});
|
2021-06-13 00:16:57 +02:00
|
|
|
});
|
2021-10-22 13:31:02 +02:00
|
|
|
};
|
2021-05-27 13:21:11 +02:00
|
|
|
|
2021-06-13 23:21:35 +02:00
|
|
|
// Input handler
|
2021-10-22 13:31:02 +02:00
|
|
|
const inputChangeHandler = (
|
|
|
|
e: ChangeEvent<HTMLInputElement | HTMLSelectElement>,
|
|
|
|
options?: { isNumber?: boolean; isBool?: boolean }
|
|
|
|
) => {
|
|
|
|
inputHandler<WeatherForm>({
|
|
|
|
e,
|
|
|
|
options,
|
|
|
|
setStateHandler: setFormData,
|
|
|
|
state: formData,
|
|
|
|
});
|
|
|
|
};
|
2021-06-13 23:21:35 +02:00
|
|
|
|
2021-05-27 12:30:09 +02:00
|
|
|
return (
|
2021-05-27 13:21:11 +02:00
|
|
|
<form onSubmit={(e) => formSubmitHandler(e)}>
|
|
|
|
<InputGroup>
|
2021-10-22 13:31:02 +02:00
|
|
|
<label htmlFor="WEATHER_API_KEY">API key</label>
|
2021-05-27 13:21:11 +02:00
|
|
|
<input
|
2021-10-22 13:31:02 +02:00
|
|
|
type="text"
|
|
|
|
id="WEATHER_API_KEY"
|
|
|
|
name="WEATHER_API_KEY"
|
|
|
|
placeholder="secret"
|
2021-05-27 13:21:11 +02:00
|
|
|
value={formData.WEATHER_API_KEY}
|
|
|
|
onChange={(e) => inputChangeHandler(e)}
|
|
|
|
/>
|
|
|
|
<span>
|
|
|
|
Using
|
2021-10-22 13:31:02 +02:00
|
|
|
<a href="https://www.weatherapi.com/pricing.aspx" target="blank">
|
|
|
|
{' '}
|
|
|
|
Weather API
|
2021-05-27 13:21:11 +02:00
|
|
|
</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-10-22 13:31:02 +02:00
|
|
|
<label htmlFor="lat">Location latitude</label>
|
2021-05-27 13:21:11 +02:00
|
|
|
<input
|
2021-10-22 13:31:02 +02:00
|
|
|
type="number"
|
|
|
|
id="lat"
|
|
|
|
name="lat"
|
|
|
|
placeholder="52.22"
|
2021-05-27 13:21:11 +02:00
|
|
|
value={formData.lat}
|
2021-10-22 13:31:02 +02:00
|
|
|
onChange={(e) => inputChangeHandler(e, { isNumber: true })}
|
|
|
|
step="any"
|
|
|
|
lang="en-150"
|
2021-05-27 13:21:11 +02:00
|
|
|
/>
|
|
|
|
<span>
|
|
|
|
You can use
|
|
|
|
<a
|
2021-10-22 13:31:02 +02:00
|
|
|
href="https://www.latlong.net/convert-address-to-lat-long.html"
|
|
|
|
target="blank"
|
|
|
|
>
|
|
|
|
{' '}
|
|
|
|
latlong.net
|
2021-05-27 13:21:11 +02:00
|
|
|
</a>
|
|
|
|
</span>
|
|
|
|
</InputGroup>
|
|
|
|
<InputGroup>
|
2021-10-22 13:31:02 +02:00
|
|
|
<label htmlFor="long">Location longitude</label>
|
2021-05-27 13:21:11 +02:00
|
|
|
<input
|
2021-10-22 13:31:02 +02:00
|
|
|
type="number"
|
|
|
|
id="long"
|
|
|
|
name="long"
|
|
|
|
placeholder="21.01"
|
2021-05-27 13:21:11 +02:00
|
|
|
value={formData.long}
|
2021-10-22 13:31:02 +02:00
|
|
|
onChange={(e) => inputChangeHandler(e, { isNumber: true })}
|
|
|
|
step="any"
|
|
|
|
lang="en-150"
|
2021-05-27 13:21:11 +02:00
|
|
|
/>
|
|
|
|
</InputGroup>
|
|
|
|
<InputGroup>
|
2021-10-22 13:31:02 +02:00
|
|
|
<label htmlFor="isCelsius">Temperature unit</label>
|
2021-05-27 13:21:11 +02:00
|
|
|
<select
|
2021-10-22 13:31:02 +02:00
|
|
|
id="isCelsius"
|
|
|
|
name="isCelsius"
|
|
|
|
onChange={(e) => inputChangeHandler(e, { isBool: true })}
|
|
|
|
value={formData.isCelsius ? 1 : 0}
|
2021-05-27 13:21:11 +02:00
|
|
|
>
|
|
|
|
<option value={1}>Celsius</option>
|
|
|
|
<option value={0}>Fahrenheit</option>
|
|
|
|
</select>
|
|
|
|
</InputGroup>
|
2021-10-22 13:31:02 +02:00
|
|
|
<Button>Save changes</Button>
|
2021-05-27 13:21:11 +02:00
|
|
|
</form>
|
2021-10-22 13:31:02 +02:00
|
|
|
);
|
|
|
|
};
|
2021-05-27 12:30:09 +02:00
|
|
|
|
2021-06-13 00:16:57 +02:00
|
|
|
const mapStateToProps = (state: GlobalState) => {
|
|
|
|
return {
|
2021-10-22 13:31:02 +02:00
|
|
|
loading: state.config.loading,
|
|
|
|
config: state.config.config,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, { createNotification, updateConfig })(
|
|
|
|
WeatherSettings
|
|
|
|
);
|