From cf64794f365eedf30916247405e31eba095a52c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Mon, 12 Jan 2026 12:21:17 +0100 Subject: [PATCH] refactor: change the code for better readability --- src/pages/MonitoringPage/MonitoringPage.tsx | 187 +++++++++++--------- 1 file changed, 105 insertions(+), 82 deletions(-) diff --git a/src/pages/MonitoringPage/MonitoringPage.tsx b/src/pages/MonitoringPage/MonitoringPage.tsx index ae34fd8..fe92ecf 100644 --- a/src/pages/MonitoringPage/MonitoringPage.tsx +++ b/src/pages/MonitoringPage/MonitoringPage.tsx @@ -9,6 +9,107 @@ type Trigger = { id?: string | number; label?: string ; achieved?: boolean }; type Norm = { id?: string | number; norm?: string }; +/** + * Helper function for handling the experiment controls buttons. + * @param buttonType - Type of button that was pressed. + * @param setLoading - Reference to the function that sets the current loading value. + * @param _context - (Optional) context parameter that could get used in an API call. + * @param _endpoint - (Optional) context parameter that could get used in an API call. + */ +const handleButton = async (buttonType: string, setLoading: React.Dispatch>, _context?: string, _endpoint?: string,) => { + try { + setLoading(true); + switch (buttonType) { + case "pause": + await pauseExperiment(); + break; + case "play": + await playExperiment(); + break; + case "nextPhase": + await nextPhase(); + break; + case "resetPhase": + await resetPhase(); + break; + case "resetExperiment": + await resetExperiment(); + break; + default: + } + } catch (err) { + console.error(err); + } finally { + setLoading(false); + } +} + +/** + * Defines the pages experiment controls section + * @returns an JSX React element + */ +const experimentControls = () => { + // Define use states for playing and loading + const [loading, setLoading] = React.useState(false); + const [isPlaying, setIsPlaying] = React.useState(false); + + return ( +
+

Experiment Controls

+
+ {/*Pause button*/} + + + {/*Play button*/} + + + {/*Next button*/} + + + {/*Restart Phase button*/} + + + {/*Restart Experiment button*/} + +
+
+ ) +} + + + const MonitoringPage: React.FC = () => { const getPhaseIds = useProgramStore((s) => s.getPhaseIds); const getNormsInPhase = useProgramStore((s) => s.getNormsInPhase); @@ -16,8 +117,7 @@ const MonitoringPage: React.FC = () => { const getTriggersInPhase = useProgramStore((s) => s.getTriggersInPhase); // Can be used to block actions until feedback from CB. - const [loading, setLoading] = React.useState(false); - const [isPlaying, setIsPlaying] = React.useState(false); + const phaseIds = getPhaseIds(); const [phaseIndex, _] = React.useState(0); @@ -32,35 +132,7 @@ const MonitoringPage: React.FC = () => { const triggers = getTriggersInPhase(phaseId) as Trigger[]; const norms = getNormsInPhase(phaseId) as Norm[]; - // Handle logic of 'next' button. - - const handleButton = async (button: string, _context?: string, _endpoint?: string) => { - try { - setLoading(true); - switch (button) { - case "pause": - await pauseExperiment(); - break; - case "play": - await playExperiment(); - break; - case "nextPhase": - await nextPhase(); - break; - case "resetPhase": - await resetPhase(); - break; - case "resetExperiment": - await resetExperiment(); - break; - default: - } - } catch (err) { - console.error(err); - } finally { - setLoading(false); - } - } + return (
{/* HEADER */} @@ -77,57 +149,8 @@ const MonitoringPage: React.FC = () => {
-
-

Experiment Controls

-
- {/*Pause button*/} - - - {/*Play button*/} - - - {/*Next button*/} - - - {/*Restart Phase button*/} - - - {/*Restart Experiment button*/} - -
-
+ {/*Experiment Controls*/} + {experimentControls()}
{RobotConnected()}