feat: app settings

- user can change the service ports
!!! we cannot change the express server port yet
This commit is contained in:
cv
2021-10-27 22:52:43 +02:00
parent 6b5fb186cc
commit 0a5fa12c21
+93 -34
View File
@@ -1,37 +1,78 @@
import { ModalBody } from '@chakra-ui/modal'; import { ModalBody } from '@chakra-ui/modal';
import { FormLabel, FormControl, Input, Button } from '@chakra-ui/react'; import { FormLabel, FormControl, Input, Button } from '@chakra-ui/react';
import { fetchEvent } from 'app/api/eventApi'; import { getInfo, ontimePlaceholderInfo, postInfo } from 'app/api/ontimeApi';
import { useState } from 'react'; import { useEffect, useState } from 'react';
import { useFetch } from 'app/hooks/useFetch'; import { useFetch } from 'app/hooks/useFetch';
import { EVENT_TABLE } from 'app/api/apiConstants'; import { APP_TABLE } from 'app/api/apiConstants';
import { showErrorToast } from 'common/helpers/toastManager';
import style from './Modals.module.css'; import style from './Modals.module.css';
export default function AppSettingsModal() { export default function AppSettingsModal() {
const { data, status, isError } = useFetch(EVENT_TABLE, fetchEvent); const { data, status } = useFetch(APP_TABLE, getInfo);
const [formData, setFormData] = useState(ontimePlaceholderInfo);
const [changed, setChanged] = useState(false);
const [submitting, setSubmitting] = useState(false); const [submitting, setSubmitting] = useState(false);
useEffect(() => {
if (data == null) return;
setFormData({
oscInPort: data.oscInPort,
oscOutPort: data.oscOutPort,
oscOutIP: data.oscOutIP,
});
}, [data]);
const submitHandler = async (event) => { const submitHandler = async (event) => {
event.preventDefault(); event.preventDefault();
// NOTHING HERE YET
const f = formData;
let e = { status: false, message: '' };
// Validate fields
if (f.oscInPort < 1024 || f.oscInPort > 65535) {
// Port in incorrect range
e.status = true;
e.message += 'OSC IN Port in incorrect range (1024 - 65535)';
} else if (f.oscOutPort < 1024 || f.oscOutPort > 65535) {
// Port in incorrect range
e.status = true;
e.message += 'OSC OUT Port in incorrect range (1024 - 65535)';
} else if (f.oscInPort === f.oscOutPort) {
// Cant use the same port
e.status = true;
e.message += 'OSC IN and OUT Ports cant be the same';
}
// set fields with error
if (e.status) {
showErrorToast('Invalid Input', e.message);
return;
}
// Post here
postInfo(formData);
setChanged(false);
setSubmitting(false);
}; };
return ( return (
<> <>
<form onSubmit={submitHandler}> <form onSubmit={submitHandler}>
<ModalBody className={style.modalBody}> <ModalBody className={style.modalBody}>
{status === 'success' && (
<> <>
<p className={style.notes}> <p className={style.notes}>
Options related to the application Options related to the application
<br /> <br />
!!! Changing of these options is not yet implemented !!! !!! Changes take effect after app restart !!!
</p> </p>
<FormControl id='viewerPort'> <FormControl id='serverPort'>
<FormLabel htmlFor='viewerPort'> <FormLabel htmlFor='serverPort'>
Viewer Port Viewer Port
<span className={style.notes}> <span className={style.notes}>Port to access viewers</span>
Port to access viewers - Default 4001
</span>
</FormLabel> </FormLabel>
<Input <Input
size='sm' size='sm'
@@ -39,19 +80,17 @@ export default function AppSettingsModal() {
placeholder='4001' placeholder='4001'
autoComplete='off' autoComplete='off'
value={4001} value={4001}
onChange={(event) => { readOnly
// Nothing here yet style={{ width: '6em', textAlign: 'center' }}
}}
isDisabled={true}
style={{ width: '6em' }}
/> />
<span className={style.notes}>(Read Only Value)</span>
</FormControl> </FormControl>
<FormControl id='oscInPort'> <FormControl id='oscInPort'>
<FormLabel htmlFor='oscInPort'> <FormLabel htmlFor='oscInPort'>
OSC In Port OSC In Port
<span className={style.notes}> <span className={style.notes}>
<br /> <br />
App control - Default 8888 App Control - Default 8888
</span> </span>
</FormLabel> </FormLabel>
<Input <Input
@@ -59,17 +98,24 @@ export default function AppSettingsModal() {
name='oscInPort' name='oscInPort'
placeholder='8888' placeholder='8888'
autoComplete='off' autoComplete='off'
value={8888} type='number'
value={formData.oscInPort}
min='1024'
max='65535'
onChange={(event) => { onChange={(event) => {
// Nothing here yet setChanged(true);
setFormData({
...formData,
oscInPort: parseInt(event.target.value),
});
}} }}
isDisabled={true} isDisabled={submitting}
style={{ width: '6em' }} style={{ width: '6em', textAlign: 'center' }}
/> />
</FormControl> </FormControl>
<div className={style.modalInline}> <div className={style.modalInline}>
<FormControl id='oscOutTarget'> <FormControl id='oscOutIP' width='auto'>
<FormLabel htmlFor='oscOutTarget'> <FormLabel htmlFor='oscOutIP'>
OSC Out Target IP OSC Out Target IP
<span className={style.notes}> <span className={style.notes}>
<br /> <br />
@@ -78,17 +124,22 @@ export default function AppSettingsModal() {
</FormLabel> </FormLabel>
<Input <Input
size='sm' size='sm'
name='oscOutPort' name='oscOutIP'
placeholder='9999' placeholder='127.0.0.1'
autoComplete='off' autoComplete='off'
value={'127.0.0.1'} value={formData.oscOutIP}
onChange={(event) => { onChange={(event) => {
// Nothing here yet setChanged(true);
setFormData({
...formData,
oscOutIP: event.target.value,
});
}} }}
isDisabled={true} isDisabled={submitting}
style={{ width: '12em', textAlign: 'right' }}
/> />
</FormControl> </FormControl>
<FormControl id='oscOutPort'> <FormControl id='oscOutPort' width='auto'>
<FormLabel htmlFor='oscOutPort'> <FormLabel htmlFor='oscOutPort'>
OSC Out Port OSC Out Port
<span className={style.notes}> <span className={style.notes}>
@@ -101,21 +152,29 @@ export default function AppSettingsModal() {
name='oscOutPort' name='oscOutPort'
placeholder='9999' placeholder='9999'
autoComplete='off' autoComplete='off'
value={9999} type='number'
value={formData.oscOutPort}
min='1024'
max='65535'
onChange={(event) => { onChange={(event) => {
// Nothing here yet setChanged(true);
setFormData({
...formData,
oscOutPort: parseInt(event.target.value),
});
}} }}
isDisabled={true} isDisabled={submitting}
style={{ width: '6em' }} style={{ width: '6em', textAlign: 'left' }}
/> />
</FormControl> </FormControl>
</div> </div>
</> </>
)}
<Button <Button
colorScheme='blue' colorScheme='blue'
type='submit' type='submit'
isLoading={submitting} isLoading={submitting}
disabled={true} disabled={!changed}
> >
Save Save
</Button> </Button>