From e1cc3edd6519bc8ce78fe67814e9829d3b49ba2b Mon Sep 17 00:00:00 2001 From: Cristen Jones Date: Tue, 14 May 2024 10:09:52 -0400 Subject: [PATCH] refactor(RapidTransitHoursOfOperation): check date before formatting (#2046) * refactor(RapidTransitHoursOfOperation): check date before formatting time * remove unused variable --- .../RapidTransitHoursOfOperation.tsx | 50 +++++++++---------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/assets/ts/schedule/components/RapidTransitHoursOfOperation.tsx b/assets/ts/schedule/components/RapidTransitHoursOfOperation.tsx index 31ff18b5f8..824771063e 100644 --- a/assets/ts/schedule/components/RapidTransitHoursOfOperation.tsx +++ b/assets/ts/schedule/components/RapidTransitHoursOfOperation.tsx @@ -1,5 +1,5 @@ -import { isSaturday, isSunday, parseISO } from "date-fns"; -import { map, min } from "lodash"; +import { isDate, isSaturday, isSunday, parseISO } from "date-fns"; +import { min } from "lodash"; import React, { ReactElement } from "react"; import { formatToBostonTime } from "../../helpers/date"; import useHoursOfOperation from "../../hooks/useHoursOfOperation"; @@ -35,25 +35,17 @@ const getScheduleNoteForDate = ( return trainsEveryHTML(scheduleNote.peak_service); }; -const getEarliestTrain = (dataArray: StopHours[] | undefined): string => { - if (!dataArray || dataArray.length === 0) { - return ""; - } - - const firstDepartureTimes = map(dataArray, d => parseISO(d.first_departure)); - const firstDeparture = min(firstDepartureTimes); - return firstDeparture ? formatToBostonTime(firstDeparture) : ""; -}; - -const getLatestTrain = (dataArray: StopHours[] | undefined): string => { - // get the earliest last departure - if (!dataArray || dataArray.length === 0) { - return ""; - } - - const lastDepartureTimes = map(dataArray, d => parseISO(d.last_departure)); - const lastDeparture = min(lastDepartureTimes); - return lastDeparture ? formatToBostonTime(lastDeparture) : ""; +const getFirstTrainTime = ( + dataArray: StopHours[] | undefined, + property: "first_departure" | "last_departure" +): string | undefined => { + const departureTimes = (dataArray || []) + .filter(d => d[property]) + .map(d => parseISO(d[property])); + const firstDeparture = min(departureTimes); + return firstDeparture && isDate(firstDeparture) + ? formatToBostonTime(firstDeparture) + : undefined; }; const getHoursForDate = ( @@ -94,8 +86,8 @@ const RapidTransitHoursOfOperation = ({ }; const todaysHours = getHoursForDate(hours, date); - const earliestTrain = getEarliestTrain(todaysHours); - const latestTrain = getLatestTrain(todaysHours); + const earliestTrain = getFirstTrainTime(todaysHours, "first_departure"); + const latestTrain = getFirstTrainTime(todaysHours, "last_departure"); const todaysScheduleNoteHtml = getScheduleNoteForDate(scheduleNote, date); @@ -104,10 +96,14 @@ const RapidTransitHoursOfOperation = ({

Today's Service


-
First & last trains
-
- {earliestTrain} | {latestTrain} -
+ {earliestTrain && latestTrain && ( + <> +
First & last trains
+
+ {earliestTrain} | {latestTrain} +
+ + )}
{todaysScheduleNoteHtml}