mirror of
https://github.com/pawelmalak/flame.git
synced 2026-03-11 06:53:12 +08:00
Reworked OtherSettings to work with global config state. Fixed bug with certain settings not being synchronized
This commit is contained in:
@@ -1,69 +1,56 @@
|
||||
import { useState, useEffect, ChangeEvent, FormEvent } from 'react';
|
||||
import axios from 'axios';
|
||||
import { connect } from 'react-redux';
|
||||
|
||||
// Redux
|
||||
import { connect } from 'react-redux';
|
||||
import { createNotification, updateConfig } from '../../../store/actions';
|
||||
|
||||
// Typescript
|
||||
import { GlobalState, NewNotification, SettingsForm } from '../../../interfaces';
|
||||
|
||||
// UI
|
||||
import InputGroup from '../../UI/Forms/InputGroup/InputGroup';
|
||||
import Button from '../../UI/Buttons/Button/Button';
|
||||
import { createNotification } from '../../../store/actions';
|
||||
import { ApiResponse, Config, NewNotification } from '../../../interfaces';
|
||||
|
||||
interface FormState {
|
||||
customTitle: string;
|
||||
pinAppsByDefault: number;
|
||||
pinCategoriesByDefault: number;
|
||||
}
|
||||
// Utils
|
||||
import { searchConfig } from '../../../utility';
|
||||
|
||||
interface ComponentProps {
|
||||
createNotification: (notification: NewNotification) => void;
|
||||
updateConfig: (formData: SettingsForm) => void;
|
||||
loading: boolean;
|
||||
}
|
||||
|
||||
const OtherSettings = (props: ComponentProps): JSX.Element => {
|
||||
const [formData, setFormData] = useState<FormState>({
|
||||
// Initial state
|
||||
const [formData, setFormData] = useState<SettingsForm>({
|
||||
customTitle: document.title,
|
||||
pinAppsByDefault: 0,
|
||||
pinCategoriesByDefault: 0
|
||||
pinAppsByDefault: 1,
|
||||
pinCategoriesByDefault: 1,
|
||||
hideHeader: 0
|
||||
})
|
||||
|
||||
// get initial config
|
||||
// Get config
|
||||
useEffect(() => {
|
||||
axios.get<ApiResponse<Config[]>>('/api/config?keys=customTitle,pinAppsByDefault,pinCategoriesByDefault')
|
||||
.then(data => {
|
||||
let tmpFormData = { ...formData };
|
||||
setFormData({
|
||||
customTitle: searchConfig('customTitle', 'Flame'),
|
||||
pinAppsByDefault: searchConfig('pinAppsByDefault', 1),
|
||||
pinCategoriesByDefault: searchConfig('pinCategoriesByDefault', 1),
|
||||
hideHeader: searchConfig('hideHeader', 0)
|
||||
})
|
||||
}, [props.loading]);
|
||||
|
||||
data.data.data.forEach((config: Config) => {
|
||||
let value: string | number = config.value;
|
||||
if (config.valueType === 'number') {
|
||||
value = parseFloat(value);
|
||||
}
|
||||
|
||||
tmpFormData = {
|
||||
...tmpFormData,
|
||||
[config.key]: value
|
||||
}
|
||||
})
|
||||
|
||||
setFormData(tmpFormData);
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
}, [])
|
||||
|
||||
const formSubmitHandler = (e: FormEvent) => {
|
||||
// Form handler
|
||||
const formSubmitHandler = async (e: FormEvent) => {
|
||||
e.preventDefault();
|
||||
|
||||
axios.put<ApiResponse<{}>>('/api/config', formData)
|
||||
.then(() => {
|
||||
props.createNotification({
|
||||
title: 'Success',
|
||||
message: 'Settings updated'
|
||||
})
|
||||
})
|
||||
.catch((err) => console.log(err));
|
||||
// Save settings
|
||||
await props.updateConfig(formData);
|
||||
|
||||
// update local page title
|
||||
localStorage.setItem('customTitle', formData.customTitle);
|
||||
document.title = formData.customTitle;
|
||||
}
|
||||
|
||||
// Input handler
|
||||
const inputChangeHandler = (e: ChangeEvent<HTMLInputElement | HTMLSelectElement>, isNumber?: boolean) => {
|
||||
let value: string | number = e.target.value;
|
||||
|
||||
@@ -80,7 +67,7 @@ const OtherSettings = (props: ComponentProps): JSX.Element => {
|
||||
return (
|
||||
<form onSubmit={(e) => formSubmitHandler(e)}>
|
||||
<InputGroup>
|
||||
<label htmlFor='customTitle'>Custom Page Title</label>
|
||||
<label htmlFor='customTitle'>Custom page title</label>
|
||||
<input
|
||||
type='text'
|
||||
id='customTitle'
|
||||
@@ -114,9 +101,27 @@ const OtherSettings = (props: ComponentProps): JSX.Element => {
|
||||
<option value={0}>False</option>
|
||||
</select>
|
||||
</InputGroup>
|
||||
<InputGroup>
|
||||
<label htmlFor='hideHeader'>Hide greeting and date</label>
|
||||
<select
|
||||
id='hideHeader'
|
||||
name='hideHeader'
|
||||
value={formData.hideHeader}
|
||||
onChange={(e) => inputChangeHandler(e, true)}
|
||||
>
|
||||
<option value={1}>True</option>
|
||||
<option value={0}>False</option>
|
||||
</select>
|
||||
</InputGroup>
|
||||
<Button>Save changes</Button>
|
||||
</form>
|
||||
)
|
||||
}
|
||||
|
||||
export default connect(null, { createNotification })(OtherSettings);
|
||||
const mapStateToProps = (state: GlobalState) => {
|
||||
return {
|
||||
loading: state.config.loading
|
||||
}
|
||||
}
|
||||
|
||||
export default connect(mapStateToProps, { createNotification, updateConfig })(OtherSettings);
|
||||
@@ -22,6 +22,7 @@ interface ComponentProps {
|
||||
}
|
||||
|
||||
const WeatherSettings = (props: ComponentProps): JSX.Element => {
|
||||
// Initial state
|
||||
const [formData, setFormData] = useState<WeatherForm>({
|
||||
WEATHER_API_KEY: '',
|
||||
lat: 0,
|
||||
@@ -29,19 +30,7 @@ const WeatherSettings = (props: ComponentProps): JSX.Element => {
|
||||
isCelsius: 1
|
||||
})
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
|
||||
// Get config
|
||||
useEffect(() => {
|
||||
setFormData({
|
||||
WEATHER_API_KEY: searchConfig('WEATHER_API_KEY', ''),
|
||||
@@ -51,6 +40,7 @@ const WeatherSettings = (props: ComponentProps): JSX.Element => {
|
||||
})
|
||||
}, [props.loading]);
|
||||
|
||||
// Form handler
|
||||
const formSubmitHandler = async (e: FormEvent) => {
|
||||
e.preventDefault();
|
||||
|
||||
@@ -58,7 +48,7 @@ const WeatherSettings = (props: ComponentProps): JSX.Element => {
|
||||
if ((formData.lat || formData.long) && !formData.WEATHER_API_KEY) {
|
||||
props.createNotification({
|
||||
title: 'Warning',
|
||||
message: 'API Key is missing. Weather Module will NOT work'
|
||||
message: 'API key is missing. Weather Module will NOT work'
|
||||
})
|
||||
}
|
||||
|
||||
@@ -81,10 +71,24 @@ const WeatherSettings = (props: ComponentProps): JSX.Element => {
|
||||
});
|
||||
}
|
||||
|
||||
// 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
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<form onSubmit={(e) => formSubmitHandler(e)}>
|
||||
<InputGroup>
|
||||
<label htmlFor='WEATHER_API_KEY'>API Key</label>
|
||||
<label htmlFor='WEATHER_API_KEY'>API key</label>
|
||||
<input
|
||||
type='text'
|
||||
id='WEATHER_API_KEY'
|
||||
@@ -104,7 +108,7 @@ const WeatherSettings = (props: ComponentProps): JSX.Element => {
|
||||
</span>
|
||||
</InputGroup>
|
||||
<InputGroup>
|
||||
<label htmlFor='lat'>Location Latitude</label>
|
||||
<label htmlFor='lat'>Location latitude</label>
|
||||
<input
|
||||
type='number'
|
||||
id='lat'
|
||||
@@ -123,7 +127,7 @@ const WeatherSettings = (props: ComponentProps): JSX.Element => {
|
||||
</span>
|
||||
</InputGroup>
|
||||
<InputGroup>
|
||||
<label htmlFor='long'>Location Longitude</label>
|
||||
<label htmlFor='long'>Location longitude</label>
|
||||
<input
|
||||
type='number'
|
||||
id='long'
|
||||
@@ -134,7 +138,7 @@ const WeatherSettings = (props: ComponentProps): JSX.Element => {
|
||||
/>
|
||||
</InputGroup>
|
||||
<InputGroup>
|
||||
<label htmlFor='isCelsius'>Temperature Unit</label>
|
||||
<label htmlFor='isCelsius'>Temperature unit</label>
|
||||
<select
|
||||
id='isCelsius'
|
||||
name='isCelsius'
|
||||
|
||||
Reference in New Issue
Block a user