From 4a2d9bd1fc30c61c88ba43fb6de22fab5b60f24d Mon Sep 17 00:00:00 2001 From: GotthardG <51994228+GotthardG@users.noreply.github.com> Date: Fri, 10 Jan 2025 15:07:51 +0100 Subject: [PATCH] Implement polling for tracking sample updates in real-time Added a 1-second polling interval to fetch the latest sample data, ensuring the UI remains updated with real-time progress. Cleaned up code formatting for readability and consistency. Improved `getSampleStatus` logic to better distinguish sample states. --- frontend/src/components/SampleTracker.tsx | 145 ++++++++++++---------- 1 file changed, 81 insertions(+), 64 deletions(-) diff --git a/frontend/src/components/SampleTracker.tsx b/frontend/src/components/SampleTracker.tsx index 425b1ed..058edc2 100644 --- a/frontend/src/components/SampleTracker.tsx +++ b/frontend/src/components/SampleTracker.tsx @@ -26,26 +26,37 @@ interface Puck { const SampleTracker: React.FC = () => { const [pucks, setPucks] = useState([]); - const [hoveredSample, setHoveredSample] = useState<{name: string, status: string} | null>(null); + const [hoveredSample, setHoveredSample] = useState<{ name: string; status: string } | null>(null); + // Fetch latest sample data + const fetchPucks = async () => { + try { + const data: Puck[] = await SamplesService.getAllPucksWithSamplesAndEventsSamplesPucksSamplesGet(); + setPucks(data); + } catch (error) { + console.error('Error fetching pucks', error); + } + }; + + // Polling logic using a 1-second interval useEffect(() => { - const fetchPucks = async () => { - try { - const data: Puck[] = await SamplesService.getAllPucksWithSamplesAndEventsSamplesPucksSamplesGet(); - console.log(data); - setPucks(data); - } catch (error) { - console.error("Error fetching pucks", error); - } - }; + // Fetch data immediately on component mount fetchPucks(); + + // Set up polling every 1 second + const interval = setInterval(() => { + fetchPucks(); + }, 1000); + + // Clear interval on component unmount + return () => clearInterval(interval); }, []); const getSampleColor = (events: Event[] = []) => { - const hasMounted = events.some(e => e.event_type === 'Mounted'); - const hasUnmounted = events.some(e => e.event_type === 'Unmounted'); - const hasLost = events.some(e => e.event_type === 'Lost'); - const hasFailed = events.some(e => e.event_type === 'Failed'); + const hasMounted = events.some((e) => e.event_type === 'Mounted'); + const hasUnmounted = events.some((e) => e.event_type === 'Unmounted'); + const hasLost = events.some((e) => e.event_type === 'Lost'); + const hasFailed = events.some((e) => e.event_type === 'Failed'); if (hasFailed) return 'red'; if (hasLost) return 'orange'; @@ -55,66 +66,72 @@ const SampleTracker: React.FC = () => { }; const getSampleStatus = (events: Event[] = []) => { - const hasMounted = events.some(e => e.event_type === 'Mounted'); - const hasUnmounted = events.some(e => e.event_type === 'Unmounted'); - const hasLost = events.some(e => e.event_type === 'Lost'); - const hasFailed = events.some(e => e.event_type === 'Failed'); + const hasMounted = events.some((e) => e.event_type === 'Mounted'); + const hasUnmounted = events.some((e) => e.event_type === 'Unmounted'); + const hasLost = events.some((e) => e.event_type === 'Lost'); + const hasFailed = events.some((e) => e.event_type === 'Failed'); if (hasFailed) return 'Failed'; if (hasLost) return 'Lost'; + if (hasMounted && !hasUnmounted) return 'In Progress'; if (hasMounted && hasUnmounted) return 'Completed'; - if (hasMounted) return 'In Progress'; return 'Pending'; }; - return ( -
-
-
-

Sample Tracker

-
-
- {pucks.map((puck) => ( -
-
- {puck.puck_name.split('').map((char, i) => ( - {char} - ))} -
-
- {Array.from({length: 16}).map((_, index) => { - const sample = puck.samples.find(s => s.position === index + 1); - const status = sample ? getSampleStatus(sample.events) : ''; - return ( -
e.event_type === 'Lost') - ? '1px solid red' - : '1px solid lightgray', - }} - onMouseEnter={() => sample && setHoveredSample({name: sample.sample_name, status})} - onMouseLeave={() => setHoveredSample(null)} - >
- ); - })} -
-
- ))} -
- {hoveredSample && ( -
-

Name: {hoveredSample.name}

-

Status: {hoveredSample.status}

+ return ( +
+
+
+

Sample Tracker

- )} +
+ {pucks.map((puck) => ( +
+
+ {puck.puck_name.split('').map((char, i) => ( + + {char} + + ))} +
+
+ {Array.from({ length: 16 }).map((_, index) => { + const sample = puck.samples.find((s) => s.position === index + 1); + const status = sample ? getSampleStatus(sample.events) : ''; + return ( +
e.event_type === 'Lost') + ? '1px solid red' + : '1px solid lightgray', + }} + onMouseEnter={() => + sample && setHoveredSample({ name: sample.sample_name, status }) + } + onMouseLeave={() => setHoveredSample(null)} + >
+ ); + })} +
+
+ ))} +
+ {hoveredSample && ( +
+

+ Name: {hoveredSample.name} +

+

+ Status: {hoveredSample.status} +

+
+ )} +
-
); };