From 0a5fa12c2193eb456d11c0cf57f38842b21b70eb Mon Sep 17 00:00:00 2001 From: cv <34649812+cpvalente@users.noreply.github.com> Date: Wed, 27 Oct 2021 22:52:43 +0200 Subject: [PATCH] feat: app settings - user can change the service ports !!! we cannot change the express server port yet --- .../src/features/modals/AppSettingsModal.jsx | 231 +++++++++++------- 1 file changed, 145 insertions(+), 86 deletions(-) diff --git a/client/src/features/modals/AppSettingsModal.jsx b/client/src/features/modals/AppSettingsModal.jsx index 9bae4f4b0..bdc8d1c35 100644 --- a/client/src/features/modals/AppSettingsModal.jsx +++ b/client/src/features/modals/AppSettingsModal.jsx @@ -1,121 +1,180 @@ import { ModalBody } from '@chakra-ui/modal'; import { FormLabel, FormControl, Input, Button } from '@chakra-ui/react'; -import { fetchEvent } from 'app/api/eventApi'; -import { useState } from 'react'; +import { getInfo, ontimePlaceholderInfo, postInfo } from 'app/api/ontimeApi'; +import { useEffect, useState } from 'react'; 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'; 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); + useEffect(() => { + if (data == null) return; + + setFormData({ + oscInPort: data.oscInPort, + oscOutPort: data.oscOutPort, + oscOutIP: data.oscOutIP, + }); + }, [data]); + const submitHandler = async (event) => { 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 ( <>
- <> -

- Options related to the application -
- !!! Changing of these options is not yet implemented !!! -

+ {status === 'success' && ( + <> +

+ Options related to the application +
+ !!! Changes take effect after app restart !!! +

- - - Viewer Port - - Port to access viewers - Default 4001 - - - { - // Nothing here yet - }} - isDisabled={true} - style={{ width: '6em' }} - /> - - - - OSC In Port - -
- App control - Default 8888 -
-
- { - // Nothing here yet - }} - isDisabled={true} - style={{ width: '6em' }} - /> -
-
- - - OSC Out Target IP + + + Viewer Port + Port to access viewers + + + (Read Only Value) + + + + OSC In Port
- App Feedback - Default 127.0.0.1 + App Control - Default 8888
{ - // Nothing here yet + setChanged(true); + setFormData({ + ...formData, + oscInPort: parseInt(event.target.value), + }); }} - isDisabled={true} + isDisabled={submitting} + style={{ width: '6em', textAlign: 'center' }} />
- - - OSC Out Port - -
- Default 9999 -
-
- { - // Nothing here yet - }} - isDisabled={true} - style={{ width: '6em' }} - /> -
-
- +
+ + + OSC Out Target IP + +
+ App Feedback - Default 127.0.0.1 +
+
+ { + setChanged(true); + setFormData({ + ...formData, + oscOutIP: event.target.value, + }); + }} + isDisabled={submitting} + style={{ width: '12em', textAlign: 'right' }} + /> +
+ + + OSC Out Port + +
+ Default 9999 +
+
+ { + setChanged(true); + setFormData({ + ...formData, + oscOutPort: parseInt(event.target.value), + }); + }} + isDisabled={submitting} + style={{ width: '6em', textAlign: 'left' }} + /> +
+
+ + )}