From 9b04dcd41eda98358e769fdd80e9c6f55b945a7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Mon, 16 Sep 2024 13:46:04 +0200 Subject: [PATCH] frontend: ass Task component --- frontend/src/components/GenericComponent.tsx | 12 ++++ frontend/src/components/TaskComponent.tsx | 75 ++++++++++++++++++++ frontend/src/types/SerializedObject.ts | 7 +- 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 frontend/src/components/TaskComponent.tsx diff --git a/frontend/src/components/GenericComponent.tsx b/frontend/src/components/GenericComponent.tsx index 67e442e..95211ad 100644 --- a/frontend/src/components/GenericComponent.tsx +++ b/frontend/src/components/GenericComponent.tsx @@ -17,6 +17,7 @@ import { updateValue } from "../socket"; import { DictComponent } from "./DictComponent"; import { parseFullAccessPath } from "../utils/stateUtils"; import { SerializedEnum, SerializedObject } from "../types/SerializedObject"; +import { TaskComponent, TaskStatus } from "./TaskComponent"; interface GenericComponentProps { attribute: SerializedObject; @@ -182,6 +183,17 @@ export const GenericComponent = React.memo( id={id} /> ); + } else if (attribute.type == "Task") { + return ( + + ); } else if (attribute.type === "DataService") { return ( void; + displayName: string; + id: string; +} + +export const TaskComponent = React.memo((props: TaskProps) => { + const { fullAccessPath, docString, status, addNotification, displayName, id } = props; + + const renderCount = useRenderCount(); + const formRef = useRef(null); + const [spinning, setSpinning] = useState(false); + + useEffect(() => { + let message: string; + + if (status === "RUNNING") { + message = `${fullAccessPath} was started.`; + } else { + message = `${fullAccessPath} was stopped.`; + } + + addNotification(message); + setSpinning(false); + }, [status]); + + const execute = async (event: React.FormEvent) => { + event.preventDefault(); + + const method_name = status == "RUNNING" ? "stop" : "start"; + + const accessPath = [fullAccessPath, method_name] + .filter((element) => element) + .join("."); + setSpinning(true); + runMethod(accessPath); + }; + + return ( +
+ {process.env.NODE_ENV === "development" &&
Render count: {renderCount}
} +
+ + + {displayName} + + + + +
+
+ ); +}); + +TaskComponent.displayName = "TaskComponent"; diff --git a/frontend/src/types/SerializedObject.ts b/frontend/src/types/SerializedObject.ts index 8dec4f4..cd940c4 100644 --- a/frontend/src/types/SerializedObject.ts +++ b/frontend/src/types/SerializedObject.ts @@ -77,7 +77,12 @@ type SerializedException = SerializedObjectBase & { type: "Exception"; }; -type DataServiceTypes = "DataService" | "Image" | "NumberSlider" | "DeviceConnection"; +type DataServiceTypes = + | "DataService" + | "Image" + | "NumberSlider" + | "DeviceConnection" + | "Task"; type SerializedDataService = SerializedObjectBase & { name: string;