import React, { useState } from "react";
import { RqbitDesktopConfig } from "./configuration";
import { Button, Form, Modal, Row, Tab, Tabs } from "react-bootstrap";
import { ErrorComponent } from "./rqbit-webui-src/rqbit-web";
import { invokeAPI } from "./api";
import { ErrorDetails } from "./rqbit-webui-src/api-types";
const FormCheck: React.FC<{
label: string;
name: string;
checked: boolean;
onChange: (e: any) => void;
disabled?: boolean;
help?: string;
}> = ({ label, name, checked, onChange, disabled, help }) => {
return (
{label}
{help && {help}
}
);
};
const FormInput: React.FC<{
label: string;
name: string;
value: string | number;
inputType: string;
onChange: (e: any) => void;
disabled?: boolean;
help?: string;
}> = ({ label, name, value, inputType, onChange, disabled, help }) => {
return (
{label}
{help && {help}
}
);
};
export const ConfigModal: React.FC<{
show: boolean;
handleStartReconfigure: () => void;
handleConfigured: (config: RqbitDesktopConfig) => void;
handleCancel?: () => void;
initialConfig: RqbitDesktopConfig;
defaultConfig: RqbitDesktopConfig;
}> = ({
show,
handleStartReconfigure,
handleConfigured,
handleCancel,
initialConfig,
defaultConfig,
}) => {
let [config, setConfig] = useState(initialConfig);
let [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const handleInputChange = (e: any) => {
const name: string = e.target.name;
const value: any = e.target.value;
const [mainField, subField] = name.split(".", 2);
if (subField) {
setConfig((prevConfig: any) => ({
...prevConfig,
[mainField]: {
...prevConfig[mainField],
[subField]: value,
},
}));
} else {
setConfig((prevConfig) => ({
...prevConfig,
[name]: value,
}));
}
};
const handleToggleChange = (e: any) => {
const name: string = e.target.name;
const [mainField, subField] = name.split(".", 2);
if (subField) {
setConfig((prevConfig: any) => ({
...prevConfig,
[mainField]: {
...prevConfig[mainField],
[subField]: !prevConfig[mainField][subField],
},
}));
} else {
setConfig((prevConfig: any) => ({
...prevConfig,
[name]: !prevConfig[name],
}));
}
};
const handleOkClick = () => {
setError(null);
handleStartReconfigure();
setLoading(true);
invokeAPI<{}>("config_change", { config }).then(
() => {
setLoading(false);
handleConfigured(config);
},
(e: ErrorDetails) => {
setLoading(false);
setError({
text: "Error saving configuration",
details: e,
});
}
);
};
return (
Configure Rqbit desktop
{!!handleCancel && (
)}
);
};