Skip to content

Commit

Permalink
refactor(RapidTransitHoursOfOperation): check date before formatting (#…
Browse files Browse the repository at this point in the history
…2046)

* refactor(RapidTransitHoursOfOperation): check date before formatting time

* remove unused variable
  • Loading branch information
thecristen authored May 14, 2024
1 parent 5f97417 commit e1cc3ed
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions assets/ts/schedule/components/RapidTransitHoursOfOperation.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 = (
Expand Down Expand Up @@ -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);

Expand All @@ -104,10 +96,14 @@ const RapidTransitHoursOfOperation = ({
<div className="u-bg-primary-light-contrast p-16 mt-16">
<h3 style={{ marginTop: "0rem" }}>Today&#39;s Service</h3>
<br />
<div className="fs-16">First & last trains</div>
<div className="fs-18">
<b>{earliestTrain}</b> | <b>{latestTrain}</b>
</div>
{earliestTrain && latestTrain && (
<>
<div className="fs-16">First & last trains</div>
<div className="fs-18">
<b>{earliestTrain}</b> | <b>{latestTrain}</b>
</div>
</>
)}
<br />
{todaysScheduleNoteHtml}
<br />
Expand Down

0 comments on commit e1cc3ed

Please sign in to comment.