1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-19 19:49:37 +02:00
flame/client/src/components/Settings/WeatherSettings/WeatherSettings.tsx

183 lines
4.6 KiB
TypeScript
Raw Normal View History

import { useState, ChangeEvent, useEffect, FormEvent } from 'react';
import axios from 'axios';
// Redux
import { useDispatch, useSelector } from 'react-redux';
import { bindActionCreators } from 'redux';
import { actionCreators } from '../../../store';
2021-11-10 16:45:30 +01:00
import { State } from '../../../store/reducers';
// Typescript
import { ApiResponse, Weather, WeatherForm } from '../../../interfaces';
// UI
import { InputGroup, Button, SettingsHeadline } from '../../UI';
// Utils
2021-10-22 13:31:02 +02:00
import { inputHandler, weatherSettingsTemplate } from '../../../utility';
export const WeatherSettings = (): JSX.Element => {
const { loading, config } = useSelector((state: State) => state.config);
const dispatch = useDispatch();
const { createNotification, updateConfig } = bindActionCreators(
actionCreators,
dispatch
);
// Initial state
2021-10-22 13:31:02 +02:00
const [formData, setFormData] = useState<WeatherForm>(
weatherSettingsTemplate
);
// Get config
useEffect(() => {
setFormData({
...config,
2021-10-22 13:31:02 +02:00
});
}, [loading]);
// Form handler
const formSubmitHandler = async (e: FormEvent) => {
e.preventDefault();
// Check for api key input
2024-09-10 22:41:11 +02:00
if ((formData.lat || formData.long) && !formData.weather_enabled) {
createNotification({
title: 'Warning',
2024-09-10 22:41:11 +02:00
message: 'You need to enable the weather module to use it',
2021-10-22 13:31:02 +02:00
});
}
// Save settings
await updateConfig(formData);
2021-10-22 13:31:02 +02:00
// Update weather
2021-10-22 13:31:02 +02:00
axios
.get<ApiResponse<Weather>>('/api/weather/update')
.then(() => {
createNotification({
title: 'Success',
2021-10-22 13:31:02 +02:00
message: 'Weather updated',
});
})
.catch((err) => {
createNotification({
title: 'Error',
2021-10-22 13:31:02 +02:00
message: err.response.data.error,
});
});
2021-10-22 13:31:02 +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,
});
};
// Get user location
const getLocation = () => {
window.navigator.geolocation.getCurrentPosition(
({ coords: { latitude, longitude } }) => {
setFormData({
...formData,
lat: latitude,
long: longitude,
});
}
);
};
return (
<form onSubmit={(e) => formSubmitHandler(e)}>
2024-09-10 22:41:11 +02:00
<SettingsHeadline text="Enabled" />
{/* API KEY */}
<InputGroup>
2024-09-10 22:41:11 +02:00
<label htmlFor="enabled">Enable weather module</label>
<input
2024-09-10 22:41:11 +02:00
type="checkbox"
id="weather_enabled"
name="weather_enabled"
checked={formData.weather_enabled}
onChange={(e) => inputChangeHandler(e)}
/>
<span>
2024-09-10 22:41:11 +02:00
Required for weather module to work.
</span>
</InputGroup>
<SettingsHeadline text="Location" />
{/* LAT */}
<InputGroup>
<label htmlFor="lat">Latitude</label>
<input
2021-10-22 13:31:02 +02:00
type="number"
id="lat"
name="lat"
placeholder="52.22"
value={formData.lat}
2021-10-22 13:31:02 +02:00
onChange={(e) => inputChangeHandler(e, { isNumber: true })}
step="any"
lang="en-150"
/>
<span onClick={getLocation}>
<a href="#">Click to get current location</a>
</span>
</InputGroup>
{/* LONG */}
<InputGroup>
<label htmlFor="long">Longitude</label>
<input
2021-10-22 13:31:02 +02:00
type="number"
id="long"
name="long"
placeholder="21.01"
value={formData.long}
2021-10-22 13:31:02 +02:00
onChange={(e) => inputChangeHandler(e, { isNumber: true })}
step="any"
lang="en-150"
/>
</InputGroup>
<SettingsHeadline text="Other" />
{/* TEMPERATURE */}
<InputGroup>
2021-10-22 13:31:02 +02:00
<label htmlFor="isCelsius">Temperature unit</label>
<select
2021-10-22 13:31:02 +02:00
id="isCelsius"
name="isCelsius"
onChange={(e) => inputChangeHandler(e, { isBool: true })}
value={formData.isCelsius ? 1 : 0}
>
<option value={1}>Celsius</option>
<option value={0}>Fahrenheit</option>
</select>
</InputGroup>
{/* WEATHER DATA */}
<InputGroup>
<label htmlFor="weatherData">Additional weather data</label>
<select
id="weatherData"
name="weatherData"
value={formData.weatherData}
onChange={(e) => inputChangeHandler(e)}
>
<option value="cloud">Cloud coverage</option>
<option value="humidity">Humidity</option>
</select>
</InputGroup>
2021-10-22 13:31:02 +02:00
<Button>Save changes</Button>
</form>
2021-10-22 13:31:02 +02:00
);
};