feat: change objects to instances passed to remotes service

This commit is contained in:
Fritz Heiden 2025-04-10 20:02:10 +02:00
parent 7a20ae1536
commit 318185bd4a
4 changed files with 32 additions and 26 deletions

View File

@ -2,7 +2,13 @@ import { createSignal, mergeProps } from "solid-js";
function ValidatedTextInput(props) { function ValidatedTextInput(props) {
props = mergeProps( props = mergeProps(
{ type: "text", valid: true, onInput: () => {}, errorText: "" }, {
type: "text",
valid: true,
onInput: () => {},
errorText: "",
placeholder: "",
},
props props
); );
let [isActive, setActive] = createSignal(false); let [isActive, setActive] = createSignal(false);
@ -15,6 +21,7 @@ function ValidatedTextInput(props) {
} }
id={props.id} id={props.id}
value={props.value} value={props.value}
placeholder={props.placeholder}
onInput={props.onInput} onInput={props.onInput}
onFocusOut={() => setActive(true)} onFocusOut={() => setActive(true)}
/> />

View File

@ -26,14 +26,6 @@ function CreateCommandModal(props) {
const isCommandTypeValid = createMemo(() => commandType() !== ""); const isCommandTypeValid = createMemo(() => commandType() !== "");
const isTitleValid = createMemo(() => title().length >= MIN_TITLE_LENGTH); const isTitleValid = createMemo(() => title().length >= MIN_TITLE_LENGTH);
createEffect(() => {
let commandString = commandType()
? Command.CommandTypes[commandType()]
: "";
let protocolString = protocol() ? Command.Protocols[protocol()] : "";
setTitle(commandString + " " + protocolString);
});
const isFormValid = createMemo( const isFormValid = createMemo(
() => () =>
isProtocolValid() && isProtocolValid() &&
@ -46,13 +38,15 @@ function CreateCommandModal(props) {
async function handleCreateCommand() { async function handleCreateCommand() {
let command; let command;
try { try {
command = await RemotesService.createCommand({ command = await RemotesService.createCommand(
new Command({
protocol: protocol(), protocol: protocol(),
commandNumber: commandNumber(), commandNumber: commandNumber(),
device: device(), device: device(),
commandType: commandType(), commandType: commandType(),
title: title(), title: title(),
}); })
);
} catch (e) { } catch (e) {
setError(e.message); setError(e.message);
return; return;
@ -93,7 +87,9 @@ function CreateCommandModal(props) {
class="form-select" class="form-select"
onChange={(e) => setProtocol(e.target.value)} onChange={(e) => setProtocol(e.target.value)}
> >
<option value="" selected>Please select</option> <option value="" selected>
Please select
</option>
{Object.keys(Command.Protocols).map((protocol) => ( {Object.keys(Command.Protocols).map((protocol) => (
<option value={protocol}>{Command.Protocols[protocol]}</option> <option value={protocol}>{Command.Protocols[protocol]}</option>
))} ))}
@ -138,7 +134,9 @@ function CreateCommandModal(props) {
class="form-select" class="form-select"
onChange={(e) => setCommandType(e.target.value)} onChange={(e) => setCommandType(e.target.value)}
> >
<option value="" selected>Please select</option> <option value="" selected>
Please select
</option>
{Object.keys(Command.CommandTypes).map((commandType) => ( {Object.keys(Command.CommandTypes).map((commandType) => (
<option value={commandType}> <option value={commandType}>
{Command.CommandTypes[commandType]} {Command.CommandTypes[commandType]}

View File

@ -5,6 +5,7 @@ import EventEmitter from "../tools/event-emitter.js";
import ModalHandler from "./modal-handler.js"; import ModalHandler from "./modal-handler.js";
import Modal from "./modal.jsx"; import Modal from "./modal.jsx";
import ListManager from "../components/list-manager.jsx"; import ListManager from "../components/list-manager.jsx";
import Remote from "../data/remote.js";
const eventEmitter = new EventEmitter(); const eventEmitter = new EventEmitter();
const REMOTE_CREATED_EVENT = "success"; const REMOTE_CREATED_EVENT = "success";
@ -28,10 +29,12 @@ function CreateRemoteModal(props) {
async function handleCreateRemote() { async function handleCreateRemote() {
let remote; let remote;
try { try {
remote = await RemotesService.createRemote({ remote = await RemotesService.createRemote(
new Remote({
title: title(), title: title(),
commands: commands(), commands: commands(),
}); })
);
} catch (e) { } catch (e) {
setError(e.message); setError(e.message);
return; return;

View File

@ -34,8 +34,7 @@ function RemotesService() {
return [].concat(remotes); return [].concat(remotes);
} }
async function createRemote(remoteObject) { async function createRemote(remote) {
let remote = Serializer.deserializeRemote(remoteObject);
let id = Math.random().toString(36).substr(2, 9); let id = Math.random().toString(36).substr(2, 9);
remote.setId(id); remote.setId(id);
remotes.push(remote); remotes.push(remote);
@ -53,8 +52,7 @@ function RemotesService() {
return [].concat(_commands); return [].concat(_commands);
} }
async function createCommand(commandObject) { async function createCommand(command) {
let command = Serializer.deserializeCommand(commandObject);
let id = Math.random().toString(36).substr(2, 9); let id = Math.random().toString(36).substr(2, 9);
command.setId(id); command.setId(id);
_commands.push(command); _commands.push(command);