Files
OpenMediaCenter/src/pages/SettingsPage/GeneralSettings.tsx
T

249 lines
11 KiB
TypeScript
Raw Normal View History

2020-10-25 18:48:23 +00:00
import React from 'react';
import {Button, Col, Form} from 'react-bootstrap';
import style from './GeneralSettings.module.css';
2020-12-17 20:53:22 +00:00
import GlobalInfos from '../../utils/GlobalInfos';
2020-10-25 18:48:23 +00:00
import InfoHeaderItem from '../../elements/InfoHeaderItem/InfoHeaderItem';
import {faArchive, faBalanceScaleLeft, faRulerVertical} from '@fortawesome/free-solid-svg-icons';
import {faAddressCard} from '@fortawesome/free-regular-svg-icons';
import {version} from '../../../package.json';
import {APINode, callAPI, setCustomBackendDomain} from '../../utils/Api';
2021-01-22 21:05:21 +00:00
import {SettingsTypes} from '../../types/ApiTypes';
import {GeneralSuccess} from '../../types/GeneralTypes';
interface state {
2021-02-23 16:01:29 +00:00
customapi: boolean
apipath: string
generalSettings: SettingsTypes.loadGeneralSettingsType
2021-01-22 21:05:21 +00:00
}
2021-02-23 16:01:29 +00:00
interface props {
}
2020-08-12 17:50:25 +00:00
/**
* Component for Generalsettings tag on Settingspage
* handles general settings of mediacenter which concerns to all pages
*/
2021-01-22 21:05:21 +00:00
class GeneralSettings extends React.Component<props, state> {
constructor(props: props) {
super(props);
2020-07-03 00:20:11 +02:00
this.state = {
2020-12-17 20:53:22 +00:00
customapi: false,
2021-01-22 21:05:21 +00:00
apipath: '',
2021-02-23 16:01:29 +00:00
generalSettings: {
DarkMode: true,
DBSize: 0,
DifferentTags: 0,
EpisodePath: "",
MediacenterName: "",
Password: "",
PasswordEnabled: false,
TagsAdded: 0,
TMDBGrabbing: false,
VideoNr: 0,
VideoPath: ""
}
2020-07-03 00:20:11 +02:00
};
}
2021-01-22 21:05:21 +00:00
componentDidMount(): void {
2020-08-12 17:50:25 +00:00
this.loadSettings();
}
2021-01-22 21:05:21 +00:00
render(): JSX.Element {
const themeStyle = GlobalInfos.getThemeStyle();
return (
<>
<div className={style.infoheader}>
<InfoHeaderItem backColor='lightblue'
2021-02-23 16:01:29 +00:00
text={this.state.generalSettings.VideoNr}
subtext='Videos in Gravity'
icon={faArchive}/>
<InfoHeaderItem backColor='yellow'
2021-02-23 16:01:29 +00:00
text={this.state.generalSettings.DBSize + ' MB'}
subtext='Database size'
icon={faRulerVertical}/>
<InfoHeaderItem backColor='green'
2021-02-23 16:01:29 +00:00
text={this.state.generalSettings.DifferentTags}
subtext='different Tags'
icon={faAddressCard}/>
<InfoHeaderItem backColor='orange'
2021-02-23 16:01:29 +00:00
text={this.state.generalSettings.TagsAdded}
subtext='tags added'
icon={faBalanceScaleLeft}/>
</div>
2020-07-27 21:14:56 +02:00
<div className={style.GeneralForm + ' ' + themeStyle.subtextcolor}>
2021-01-22 21:05:21 +00:00
<Form data-testid='mainformsettings' onSubmit={(e): void => {
2020-07-04 00:45:18 +02:00
e.preventDefault();
this.saveSettings();
}}>
2020-06-29 21:34:43 +02:00
<Form.Row>
<Form.Group as={Col} data-testid='videpathform'>
2020-06-29 21:34:43 +02:00
<Form.Label>Video Path</Form.Label>
2021-02-23 16:01:29 +00:00
<Form.Control type='text' placeholder='/var/www/html/video'
value={this.state.generalSettings.VideoPath}
onChange={(ee): void => this.setState({
generalSettings: {
...this.state.generalSettings,
VideoPath: ee.target.value
}
})}/>
2020-06-29 21:34:43 +02:00
</Form.Group>
<Form.Group as={Col} data-testid='tvshowpath'>
2020-07-03 00:20:11 +02:00
<Form.Label>TV Show Path</Form.Label>
<Form.Control type='text' placeholder='/var/www/html/tvshow'
2021-02-23 16:01:29 +00:00
value={this.state.generalSettings.EpisodePath}
onChange={(e): void => this.setState({
generalSettings: {
...this.state.generalSettings,
EpisodePath: e.target.value
}
})}/>
2020-06-29 21:34:43 +02:00
</Form.Group>
</Form.Row>
2020-12-17 20:53:22 +00:00
<Form.Check
type='switch'
id='custom-switch-api'
label='Use custom API url'
checked={this.state.customapi}
2021-01-22 21:05:21 +00:00
onChange={(): void => {
2020-12-17 20:53:22 +00:00
if (this.state.customapi) {
setCustomBackendDomain('');
}
this.setState({customapi: !this.state.customapi});
}}
/>
{this.state.customapi ?
<Form.Group className={style.customapiform} data-testid='apipath'>
<Form.Label>API Backend url</Form.Label>
<Form.Control type='text' placeholder='https://127.0.0.1'
value={this.state.apipath}
2021-01-22 21:05:21 +00:00
onChange={(e): void => {
2020-12-17 20:53:22 +00:00
this.setState({apipath: e.target.value});
setCustomBackendDomain(e.target.value);
}}/>
</Form.Group> : null}
2020-07-03 00:20:11 +02:00
<Form.Check
type='switch'
id='custom-switch'
2020-07-13 00:44:16 +02:00
data-testid='passwordswitch'
label='Enable Password support'
2021-02-23 16:01:29 +00:00
checked={this.state.generalSettings.PasswordEnabled}
2021-01-22 21:05:21 +00:00
onChange={(): void => {
2021-02-23 16:01:29 +00:00
this.setState({
generalSettings: {
...this.state.generalSettings,
PasswordEnabled: !this.state.generalSettings.PasswordEnabled
}
});
2020-07-03 00:20:11 +02:00
}}
/>
2021-02-23 16:01:29 +00:00
{this.state.generalSettings.PasswordEnabled ?
<Form.Group data-testid='passwordfield'>
2020-07-28 18:17:17 +02:00
<Form.Label>Password</Form.Label>
2021-02-23 16:01:29 +00:00
<Form.Control type='password' placeholder='**********'
value={this.state.generalSettings.Password}
onChange={(e): void => this.setState({
generalSettings: {
...this.state.generalSettings,
Password: e.target.value
}
})}/>
2020-07-28 18:17:17 +02:00
</Form.Group> : null
}
<Form.Check
type='switch'
id='custom-switch-2'
data-testid='tmdb-switch'
label='Enable TMDB video grabbing support'
2021-02-23 16:01:29 +00:00
checked={this.state.generalSettings.TMDBGrabbing}
2021-01-22 21:05:21 +00:00
onChange={(): void => {
2021-02-23 16:01:29 +00:00
this.setState({
generalSettings: {
...this.state.generalSettings,
TMDBGrabbing: !this.state.generalSettings.TMDBGrabbing
}
});
}}
/>
2020-07-28 18:17:17 +02:00
<Form.Check
type='switch'
id='custom-switch-3'
2020-07-28 18:17:17 +02:00
data-testid='darktheme-switch'
label='Enable Dark-Theme'
checked={GlobalInfos.isDarkTheme()}
2021-01-22 21:05:21 +00:00
onChange={(): void => {
GlobalInfos.enableDarkTheme(!GlobalInfos.isDarkTheme());
2020-07-28 18:17:17 +02:00
this.forceUpdate();
// todo initiate rerender
}}
/>
2020-07-03 00:20:11 +02:00
<Form.Group className={style.mediacenternameform} data-testid='nameform'>
2020-07-08 19:33:23 +02:00
<Form.Label>The name of the Mediacenter</Form.Label>
2021-02-23 16:01:29 +00:00
<Form.Control type='text' placeholder='Mediacentername'
value={this.state.generalSettings.MediacenterName}
onChange={(e): void => this.setState({
generalSettings: {
...this.state.generalSettings,
MediacenterName: e.target.value
}
})}/>
2020-07-08 19:33:23 +02:00
</Form.Group>
2020-06-29 21:34:43 +02:00
<Button variant='primary' type='submit'>
2020-06-29 21:34:43 +02:00
Submit
</Button>
</Form>
</div>
<div className={style.footer}>
Version: {version}
</div>
</>
);
}
2020-07-04 00:45:18 +02:00
2020-08-12 17:50:25 +00:00
/**
* inital load of already specified settings from backend
*/
2021-01-22 21:05:21 +00:00
loadSettings(): void {
callAPI(APINode.Settings, {action: 'loadGeneralSettings'}, (result: SettingsTypes.loadGeneralSettingsType) => {
2021-02-23 16:01:29 +00:00
this.setState({generalSettings: result});
2020-12-17 20:53:22 +00:00
});
2020-08-12 17:50:25 +00:00
}
/**
* save the selected and typed settings to the backend
*/
2021-01-22 21:05:21 +00:00
saveSettings(): void {
2021-02-23 16:01:29 +00:00
let settings = this.state.generalSettings;
if(!this.state.generalSettings.PasswordEnabled){
settings.Password = '-1';
}
settings.DarkMode = GlobalInfos.isDarkTheme()
callAPI(APINode.Settings, {
2020-12-17 20:53:22 +00:00
action: 'saveGeneralSettings',
2021-02-23 16:01:29 +00:00
Settings: settings
2021-01-22 21:05:21 +00:00
}, (result: GeneralSuccess) => {
if (result.result) {
2020-12-17 20:53:22 +00:00
console.log('successfully saved settings');
// todo 2020-07-10: popup success
} else {
console.log('failed to save settings');
// todo 2020-07-10: popup error
}
});
2020-07-04 00:45:18 +02:00
}
}
2020-07-08 19:33:23 +02:00
export default GeneralSettings;