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-06-09 00:59:39 +02:00
|
|
|
import { connect } from 'react-redux';
|
2021-06-18 13:42:55 +02:00
|
|
|
import { createNotification, updateConfig, sortApps, sortCategories } from '../../../store/actions';
|
2021-06-13 23:21:35 +02:00
|
|
|
|
|
|
|
// Typescript
|
|
|
|
import { GlobalState, NewNotification, SettingsForm } from '../../../interfaces';
|
2021-06-09 00:59:39 +02:00
|
|
|
|
2021-06-13 23:21:35 +02:00
|
|
|
// UI
|
2021-06-09 00:59:39 +02:00
|
|
|
import InputGroup from '../../UI/Forms/InputGroup/InputGroup';
|
|
|
|
import Button from '../../UI/Buttons/Button/Button';
|
|
|
|
|
2021-06-13 23:21:35 +02:00
|
|
|
// Utils
|
|
|
|
import { searchConfig } from '../../../utility';
|
2021-06-09 00:59:39 +02:00
|
|
|
|
|
|
|
interface ComponentProps {
|
|
|
|
createNotification: (notification: NewNotification) => void;
|
2021-06-13 23:21:35 +02:00
|
|
|
updateConfig: (formData: SettingsForm) => void;
|
2021-06-18 10:38:05 +02:00
|
|
|
sortApps: () => void;
|
2021-06-18 13:42:55 +02:00
|
|
|
sortCategories: () => void;
|
2021-06-13 23:21:35 +02:00
|
|
|
loading: boolean;
|
2021-06-09 00:59:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const OtherSettings = (props: ComponentProps): JSX.Element => {
|
2021-06-13 23:21:35 +02:00
|
|
|
// Initial state
|
|
|
|
const [formData, setFormData] = useState<SettingsForm>({
|
2021-06-09 10:58:45 +02:00
|
|
|
customTitle: document.title,
|
2021-06-13 23:21:35 +02:00
|
|
|
pinAppsByDefault: 1,
|
|
|
|
pinCategoriesByDefault: 1,
|
2021-06-18 10:38:05 +02:00
|
|
|
hideHeader: 0,
|
|
|
|
useOrdering: 'createdAt'
|
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({
|
|
|
|
customTitle: searchConfig('customTitle', 'Flame'),
|
|
|
|
pinAppsByDefault: searchConfig('pinAppsByDefault', 1),
|
|
|
|
pinCategoriesByDefault: searchConfig('pinCategoriesByDefault', 1),
|
2021-06-18 10:38:05 +02:00
|
|
|
hideHeader: searchConfig('hideHeader', 0),
|
|
|
|
useOrdering: searchConfig('useOrdering', 'createdAt')
|
2021-06-13 23:21:35 +02:00
|
|
|
})
|
|
|
|
}, [props.loading]);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
await props.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-06-18 10:38:05 +02:00
|
|
|
|
2021-06-18 13:42:55 +02:00
|
|
|
// Sort apps and categories with new settings
|
2021-06-18 10:38:05 +02:00
|
|
|
props.sortApps();
|
2021-06-18 13:42:55 +02:00
|
|
|
props.sortCategories();
|
2021-06-09 00:59:39 +02:00
|
|
|
}
|
|
|
|
|
2021-06-13 23:21:35 +02:00
|
|
|
// Input handler
|
2021-06-09 10:58:45 +02:00
|
|
|
const inputChangeHandler = (e: ChangeEvent<HTMLInputElement | HTMLSelectElement>, isNumber?: boolean) => {
|
|
|
|
let value: string | number = e.target.value;
|
|
|
|
|
|
|
|
if (isNumber) {
|
|
|
|
value = parseFloat(value);
|
|
|
|
}
|
|
|
|
|
2021-06-09 00:59:39 +02:00
|
|
|
setFormData({
|
|
|
|
...formData,
|
2021-06-09 10:58:45 +02:00
|
|
|
[e.target.name]: value
|
2021-06-09 00:59:39 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form onSubmit={(e) => formSubmitHandler(e)}>
|
|
|
|
<InputGroup>
|
2021-06-13 23:21:35 +02:00
|
|
|
<label htmlFor='customTitle'>Custom page title</label>
|
2021-06-09 00:59:39 +02:00
|
|
|
<input
|
|
|
|
type='text'
|
|
|
|
id='customTitle'
|
|
|
|
name='customTitle'
|
|
|
|
placeholder='Flame'
|
|
|
|
value={formData.customTitle}
|
|
|
|
onChange={(e) => inputChangeHandler(e)}
|
|
|
|
/>
|
|
|
|
</InputGroup>
|
2021-06-09 10:58:45 +02:00
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor='pinAppsByDefault'>Pin new applications by default</label>
|
|
|
|
<select
|
|
|
|
id='pinAppsByDefault'
|
|
|
|
name='pinAppsByDefault'
|
|
|
|
value={formData.pinAppsByDefault}
|
|
|
|
onChange={(e) => inputChangeHandler(e, true)}
|
|
|
|
>
|
|
|
|
<option value={1}>True</option>
|
|
|
|
<option value={0}>False</option>
|
|
|
|
</select>
|
|
|
|
</InputGroup>
|
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor='pinCategoriesByDefault'>Pin new categories by default</label>
|
|
|
|
<select
|
|
|
|
id='pinCategoriesByDefault'
|
|
|
|
name='pinCategoriesByDefault'
|
|
|
|
value={formData.pinCategoriesByDefault}
|
|
|
|
onChange={(e) => inputChangeHandler(e, true)}
|
|
|
|
>
|
|
|
|
<option value={1}>True</option>
|
|
|
|
<option value={0}>False</option>
|
|
|
|
</select>
|
|
|
|
</InputGroup>
|
2021-06-13 23:21:35 +02:00
|
|
|
<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>
|
2021-06-18 10:38:05 +02:00
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor='useOrdering'>Sorting type</label>
|
|
|
|
<select
|
|
|
|
id='useOrdering'
|
|
|
|
name='useOrdering'
|
|
|
|
value={formData.useOrdering}
|
|
|
|
onChange={(e) => inputChangeHandler(e)}
|
|
|
|
>
|
|
|
|
<option value='createdAt'>By creation date</option>
|
|
|
|
<option value='name'>Alphabetical order</option>
|
|
|
|
<option value='orderId'>Custom order</option>
|
|
|
|
</select>
|
|
|
|
</InputGroup>
|
2021-06-09 00:59:39 +02:00
|
|
|
<Button>Save changes</Button>
|
|
|
|
</form>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-13 23:21:35 +02:00
|
|
|
const mapStateToProps = (state: GlobalState) => {
|
|
|
|
return {
|
|
|
|
loading: state.config.loading
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 13:42:55 +02:00
|
|
|
const actions = {
|
|
|
|
createNotification,
|
|
|
|
updateConfig,
|
|
|
|
sortApps,
|
|
|
|
sortCategories
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, actions)(OtherSettings);
|