From e4562acdaaa092f80057959b1c2931eeb152ba7d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niklas=20Schr=C3=B6tler?= <niklas@allround.digital>
Date: Sat, 9 Dec 2023 16:55:54 +0100
Subject: [PATCH] FahrplanPanel: Implemented grouping stops of the same tour
 Also cleaned up superfluous code

---
 src/panels/Fahrplan/FahrplanPanel.tsx | 59 ++++++++++-----------------
 1 file changed, 21 insertions(+), 38 deletions(-)

diff --git a/src/panels/Fahrplan/FahrplanPanel.tsx b/src/panels/Fahrplan/FahrplanPanel.tsx
index 4e47efd..dfdf3d7 100644
--- a/src/panels/Fahrplan/FahrplanPanel.tsx
+++ b/src/panels/Fahrplan/FahrplanPanel.tsx
@@ -21,8 +21,10 @@ type Route = {
   stops: {
     name: string,
     arrival: Date,
-    delay?: number
-  }[]
+    delay?: number,
+    countdown: number
+  }[],
+  countdown: number
 }
 
 const FahrplanPanel = (props: {definition: FahrplanPanelDefinition}) => {
@@ -44,12 +46,11 @@ const FahrplanPanel = (props: {definition: FahrplanPanelDefinition}) => {
         }
 
         // Find existing route with same uid
-        const existing_ind = newRoutes.findIndex(r => r.uid === departure.lineref.identifier)
+        const existing_ind = newRoutes.findIndex(r => r.uid === departure.key)
 
         // Pre-compute values that will be needed regardless
         const delay = stringToDelay(departure.delay);
         const arrival = processArrival(departure.sched_date, departure.time);
-        console.log(arrival)
 
         if(existing_ind === -1) {
           // If it does not exist, create a new route
@@ -61,21 +62,27 @@ const FahrplanPanel = (props: {definition: FahrplanPanelDefinition}) => {
               {
                 name: departure.internal.stop,
                 arrival,
-                delay
+                delay,
+                countdown: parseInt(departure.countdown)
               }
-            ]
+            ],
+            countdown: parseInt(departure.countdown)
           })
         } else {
           // If it doesn't, just add a stop to the existing route
           newRoutes[existing_ind].stops.push({
             name: departure.internal.stop,
             arrival,
-            delay: stringToDelay(departure.delay)
+            delay: stringToDelay(departure.delay),
+            countdown: parseInt(departure.countdown)
           })
+
+          newRoutes[existing_ind].stops = newRoutes[existing_ind].stops.sort((a, b) => a.countdown - b.countdown)
         }
       }
 
       // Sort the output
+      newRoutes = newRoutes.sort((a, b) => a.countdown - b.countdown)
 
       // Write to the display
       setRoutes(newRoutes);
@@ -150,42 +157,18 @@ async function getStopData(stop: string): Promise<StationResponse> {
 }
 
 function stringToDelay(input: string): number | undefined {
-  try {
-    const delay = parseInt(input);
-    if(delay === 0) {
-      return undefined;
-    }
-    return delay;
-  } catch (e) {
-    console.warn("While parsing delay, the string was not interpretable", input);
+  const delay = parseInt(input);
+  if(delay === 0) {
+    return undefined;
+  }
+  if(delay === Number.NaN) {
+    console.warn("While parsing delay, the string was not interpretable as number", input);
     return undefined;
   }
+  return delay;
 }
 
 function processArrival(date: string, time: string): Date {
   const d_parts = date.split(".");
-
-  console.log(date, time, "to", `${d_parts[2]}-${d_parts[1]}-${d_parts[0]} ${time}`);
   return new Date(`${d_parts[2]}-${d_parts[1]}-${d_parts[0]} ${time}`);
 }
-
-// function sortData(data: any) {
-//   for (var i = 0; i < data.length; ++i) {
-//     data[i]['stops'] = data[i]['stops'].sort(sortFn);
-//     data[i]['timeValue'] = data[i]['stops'][0]['timeValue'];
-//   }
-//   return data.sort(sortFn);
-// }
-//
-// function sortFn(a: any, b: any) {
-//   return a['timeValue'] - b['timeValue'];
-// }
-//
-// function calcDateValue(_year: string, _month: string, _day: string, _hour: string, _minute: string): number {
-//   const year = parseInt(_year) * 12 * 31 * 24 * 60;
-//   const month = parseInt(_month) * 31 * 24 * 60;
-//   const day = parseInt(_day) * 24 * 60;
-//   const hour = parseInt(_hour) * 60;
-//   const minute = parseInt(_minute);
-//   return year+month+day+hour+minute;
-// }
-- 
GitLab