2021-06-09 00:59:39 +02:00
|
|
|
import { useState, useEffect, ChangeEvent, FormEvent } from 'react';
|
2021-06-13 23:21:35 +02:00
|
|
|
|
|
|
|
// Redux
|
2021-11-09 14:33:51 +01:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2021-11-10 16:45:30 +01:00
|
|
|
import { State } from '../../../store/reducers';
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { actionCreators } from '../../../store';
|
2021-06-13 23:21:35 +02:00
|
|
|
|
|
|
|
// Typescript
|
2022-02-04 14:59:48 +01:00
|
|
|
import { UISettingsForm } from '../../../interfaces';
|
2021-06-09 00:59:39 +02:00
|
|
|
|
2021-06-13 23:21:35 +02:00
|
|
|
// UI
|
2021-11-09 14:33:51 +01:00
|
|
|
import { InputGroup, Button, SettingsHeadline } from '../../UI';
|
2021-06-24 10:54:48 +02:00
|
|
|
|
2021-06-13 23:21:35 +02:00
|
|
|
// Utils
|
2022-02-04 14:59:48 +01:00
|
|
|
import { uiSettingsTemplate, inputHandler } from '../../../utility';
|
2021-06-09 00:59:39 +02:00
|
|
|
|
2021-11-10 16:45:30 +01:00
|
|
|
export const UISettings = (): JSX.Element => {
|
2022-02-04 14:59:48 +01:00
|
|
|
const { loading, config } = useSelector((state: State) => state.config);
|
2021-06-09 00:59:39 +02:00
|
|
|
|
2021-11-09 14:33:51 +01:00
|
|
|
const dispatch = useDispatch();
|
2022-02-04 14:59:48 +01:00
|
|
|
const { updateConfig } = bindActionCreators(actionCreators, dispatch);
|
2021-10-22 13:31:02 +02:00
|
|
|
|
2021-06-13 23:21:35 +02:00
|
|
|
// Initial state
|
2022-02-04 14:59:48 +01:00
|
|
|
const [formData, setFormData] = useState<UISettingsForm>(uiSettingsTemplate);
|
2021-06-09 00:59:39 +02:00
|
|
|
|
2021-06-13 23:21:35 +02:00
|
|
|
// Get config
|
2021-06-09 00:59:39 +02:00
|
|
|
useEffect(() => {
|
2021-06-13 23:21:35 +02:00
|
|
|
setFormData({
|
2021-10-22 13:31:02 +02:00
|
|
|
...config,
|
2021-08-06 10:36:05 +02:00
|
|
|
});
|
2021-11-09 14:33:51 +01:00
|
|
|
}, [loading]);
|
2021-06-13 23:21:35 +02:00
|
|
|
|
|
|
|
// Form handler
|
|
|
|
const formSubmitHandler = async (e: FormEvent) => {
|
2021-06-09 00:59:39 +02:00
|
|
|
e.preventDefault();
|
|
|
|
|
2021-06-13 23:21:35 +02:00
|
|
|
// Save settings
|
2021-11-09 14:33:51 +01:00
|
|
|
await updateConfig(formData);
|
2021-06-09 00:59:39 +02:00
|
|
|
|
2021-06-18 10:38:05 +02:00
|
|
|
// Update local page title
|
2021-06-09 00:59:39 +02:00
|
|
|
document.title = formData.customTitle;
|
2021-08-06 10:36:05 +02:00
|
|
|
};
|
2021-06-09 00:59:39 +02:00
|
|
|
|
2021-06-13 23:21:35 +02:00
|
|
|
// Input handler
|
2021-08-06 10:36:05 +02:00
|
|
|
const inputChangeHandler = (
|
|
|
|
e: ChangeEvent<HTMLInputElement | HTMLSelectElement>,
|
2021-10-22 13:31:02 +02:00
|
|
|
options?: { isNumber?: boolean; isBool?: boolean }
|
2021-08-06 10:36:05 +02:00
|
|
|
) => {
|
2022-02-04 14:59:48 +01:00
|
|
|
inputHandler<UISettingsForm>({
|
2021-10-22 13:31:02 +02:00
|
|
|
e,
|
|
|
|
options,
|
|
|
|
setStateHandler: setFormData,
|
|
|
|
state: formData,
|
2021-08-06 10:36:05 +02:00
|
|
|
});
|
|
|
|
};
|
2021-06-09 00:59:39 +02:00
|
|
|
|
|
|
|
return (
|
2021-09-06 12:24:01 +02:00
|
|
|
<form onSubmit={(e) => formSubmitHandler(e)}>
|
2021-11-18 16:03:44 +01:00
|
|
|
{/* === OTHER OPTIONS === */}
|
2021-10-11 13:03:31 +02:00
|
|
|
<SettingsHeadline text="Miscellaneous" />
|
2021-11-05 17:16:19 +01:00
|
|
|
{/* PAGE TITLE */}
|
2021-06-09 00:59:39 +02:00
|
|
|
<InputGroup>
|
2021-09-06 12:24:01 +02:00
|
|
|
<label htmlFor="customTitle">Custom page title</label>
|
2021-06-09 00:59:39 +02:00
|
|
|
<input
|
2021-09-06 12:24:01 +02:00
|
|
|
type="text"
|
|
|
|
id="customTitle"
|
|
|
|
name="customTitle"
|
|
|
|
placeholder="Flame"
|
2021-06-09 00:59:39 +02:00
|
|
|
value={formData.customTitle}
|
2021-09-06 12:24:01 +02:00
|
|
|
onChange={(e) => inputChangeHandler(e)}
|
2021-06-09 00:59:39 +02:00
|
|
|
/>
|
|
|
|
</InputGroup>
|
2021-11-05 17:16:19 +01:00
|
|
|
|
2022-02-04 14:59:48 +01:00
|
|
|
{/* === SEARCH OPTIONS === */}
|
|
|
|
<SettingsHeadline text="Search" />
|
|
|
|
{/* HIDE SEARCHBAR */}
|
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor="hideSearch">Hide search bar</label>
|
|
|
|
<select
|
|
|
|
id="hideSearch"
|
|
|
|
name="hideSearch"
|
|
|
|
value={formData.hideSearch ? 1 : 0}
|
|
|
|
onChange={(e) => inputChangeHandler(e, { isBool: true })}
|
|
|
|
>
|
|
|
|
<option value={1}>True</option>
|
|
|
|
<option value={0}>False</option>
|
|
|
|
</select>
|
|
|
|
</InputGroup>
|
|
|
|
|
|
|
|
{/* AUTOFOCUS SEARCHBAR */}
|
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor="disableAutofocus">Disable search bar autofocus</label>
|
|
|
|
<select
|
|
|
|
id="disableAutofocus"
|
|
|
|
name="disableAutofocus"
|
|
|
|
value={formData.disableAutofocus ? 1 : 0}
|
|
|
|
onChange={(e) => inputChangeHandler(e, { isBool: true })}
|
|
|
|
>
|
|
|
|
<option value={1}>True</option>
|
|
|
|
<option value={0}>False</option>
|
|
|
|
</select>
|
|
|
|
</InputGroup>
|
|
|
|
|
2021-11-18 16:03:44 +01:00
|
|
|
{/* === HEADER OPTIONS === */}
|
|
|
|
<SettingsHeadline text="Header" />
|
|
|
|
{/* HIDE HEADER */}
|
|
|
|
<InputGroup>
|
2021-11-20 14:18:42 +01:00
|
|
|
<label htmlFor="hideHeader">
|
|
|
|
Hide headline (greetings and weather)
|
|
|
|
</label>
|
2021-11-18 16:03:44 +01:00
|
|
|
<select
|
|
|
|
id="hideHeader"
|
|
|
|
name="hideHeader"
|
|
|
|
value={formData.hideHeader ? 1 : 0}
|
|
|
|
onChange={(e) => inputChangeHandler(e, { isBool: true })}
|
|
|
|
>
|
|
|
|
<option value={1}>True</option>
|
|
|
|
<option value={0}>False</option>
|
|
|
|
</select>
|
|
|
|
</InputGroup>
|
|
|
|
|
|
|
|
{/* HIDE DATE */}
|
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor="hideDate">Hide date</label>
|
|
|
|
<select
|
|
|
|
id="hideDate"
|
|
|
|
name="hideDate"
|
|
|
|
value={formData.hideDate ? 1 : 0}
|
|
|
|
onChange={(e) => inputChangeHandler(e, { isBool: true })}
|
|
|
|
>
|
|
|
|
<option value={1}>True</option>
|
|
|
|
<option value={0}>False</option>
|
|
|
|
</select>
|
|
|
|
</InputGroup>
|
|
|
|
|
|
|
|
{/* HIDE TIME */}
|
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor="showTime">Hide time</label>
|
|
|
|
<select
|
|
|
|
id="showTime"
|
|
|
|
name="showTime"
|
|
|
|
value={formData.showTime ? 1 : 0}
|
|
|
|
onChange={(e) => inputChangeHandler(e, { isBool: true })}
|
|
|
|
>
|
|
|
|
<option value={0}>True</option>
|
|
|
|
<option value={1}>False</option>
|
|
|
|
</select>
|
|
|
|
</InputGroup>
|
|
|
|
|
2021-11-05 17:16:19 +01:00
|
|
|
{/* DATE FORMAT */}
|
2021-10-22 15:51:11 +02:00
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor="useAmericanDate">Date formatting</label>
|
|
|
|
<select
|
|
|
|
id="useAmericanDate"
|
|
|
|
name="useAmericanDate"
|
|
|
|
value={formData.useAmericanDate ? 1 : 0}
|
|
|
|
onChange={(e) => inputChangeHandler(e, { isBool: true })}
|
|
|
|
>
|
|
|
|
<option value={1}>Friday, October 22 2021</option>
|
|
|
|
<option value={0}>Friday, 22 October 2021</option>
|
|
|
|
</select>
|
|
|
|
</InputGroup>
|
2021-06-24 10:54:48 +02:00
|
|
|
|
2021-11-18 16:03:44 +01:00
|
|
|
{/* CUSTOM GREETINGS */}
|
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor="greetingsSchema">Custom greetings</label>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
id="greetingsSchema"
|
|
|
|
name="greetingsSchema"
|
|
|
|
placeholder="Good day;Hi;Bye!"
|
|
|
|
value={formData.greetingsSchema}
|
|
|
|
onChange={(e) => inputChangeHandler(e)}
|
|
|
|
/>
|
|
|
|
<span>
|
2022-02-04 14:59:48 +01:00
|
|
|
Greetings must be separated with semicolon. All 4 messages must be
|
|
|
|
filled, even if they are the same
|
2021-11-18 16:03:44 +01:00
|
|
|
</span>
|
|
|
|
</InputGroup>
|
|
|
|
|
|
|
|
{/* CUSTOM DAYS */}
|
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor="daySchema">Custom weekday names</label>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
id="daySchema"
|
|
|
|
name="daySchema"
|
|
|
|
placeholder="Sunday;Monday;Tuesday"
|
|
|
|
value={formData.daySchema}
|
|
|
|
onChange={(e) => inputChangeHandler(e)}
|
|
|
|
/>
|
|
|
|
<span>Names must be separated with semicolon</span>
|
|
|
|
</InputGroup>
|
|
|
|
|
|
|
|
{/* CUSTOM MONTHS */}
|
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor="monthSchema">Custom month names</label>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
id="monthSchema"
|
|
|
|
name="monthSchema"
|
|
|
|
placeholder="January;February;March"
|
|
|
|
value={formData.monthSchema}
|
|
|
|
onChange={(e) => inputChangeHandler(e)}
|
|
|
|
/>
|
|
|
|
<span>Names must be separated with semicolon</span>
|
|
|
|
</InputGroup>
|
|
|
|
|
2022-02-04 14:59:48 +01:00
|
|
|
{/* === SECTIONS OPTIONS === */}
|
|
|
|
<SettingsHeadline text="Sections" />
|
2021-11-05 17:16:19 +01:00
|
|
|
{/* HIDE APPS */}
|
2021-06-24 10:54:48 +02:00
|
|
|
<InputGroup>
|
2021-09-06 12:24:01 +02:00
|
|
|
<label htmlFor="hideApps">Hide applications</label>
|
2021-06-24 10:54:48 +02:00
|
|
|
<select
|
2021-09-06 12:24:01 +02:00
|
|
|
id="hideApps"
|
|
|
|
name="hideApps"
|
2021-10-22 13:31:02 +02:00
|
|
|
value={formData.hideApps ? 1 : 0}
|
|
|
|
onChange={(e) => inputChangeHandler(e, { isBool: true })}
|
2021-06-24 10:54:48 +02:00
|
|
|
>
|
|
|
|
<option value={1}>True</option>
|
|
|
|
<option value={0}>False</option>
|
|
|
|
</select>
|
|
|
|
</InputGroup>
|
2021-11-05 17:16:19 +01:00
|
|
|
|
|
|
|
{/* HIDE CATEGORIES */}
|
2021-06-24 10:54:48 +02:00
|
|
|
<InputGroup>
|
2021-09-06 12:24:01 +02:00
|
|
|
<label htmlFor="hideCategories">Hide categories</label>
|
2021-06-24 10:54:48 +02:00
|
|
|
<select
|
2021-09-06 12:24:01 +02:00
|
|
|
id="hideCategories"
|
|
|
|
name="hideCategories"
|
2021-10-22 13:31:02 +02:00
|
|
|
value={formData.hideCategories ? 1 : 0}
|
|
|
|
onChange={(e) => inputChangeHandler(e, { isBool: true })}
|
2021-06-24 10:54:48 +02:00
|
|
|
>
|
|
|
|
<option value={1}>True</option>
|
|
|
|
<option value={0}>False</option>
|
|
|
|
</select>
|
|
|
|
</InputGroup>
|
2021-08-06 10:36:05 +02:00
|
|
|
|
|
|
|
<Button>Save changes</Button>
|
2021-06-09 00:59:39 +02:00
|
|
|
</form>
|
2021-08-06 10:36:05 +02:00
|
|
|
);
|
|
|
|
};
|