From 4aabef8a085933575ce1272d70cf2cb827d25e8a Mon Sep 17 00:00:00 2001 From: Cristen Jones Date: Mon, 16 Dec 2024 16:37:53 -0500 Subject: [PATCH] fix(DailySchedule): improve identification of today's service (#2274) --- assets/ts/helpers/service.ts | 8 +++----- .../schedule-finder/ScheduleModalContent.tsx | 8 +++++--- .../schedule-finder/daily-schedule/DailySchedule.tsx | 10 +++++----- .../schedule-finder/daily-schedule/ServiceOptGroup.tsx | 6 +++++- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/assets/ts/helpers/service.ts b/assets/ts/helpers/service.ts index d4bcc52acb..679f3932c9 100644 --- a/assets/ts/helpers/service.ts +++ b/assets/ts/helpers/service.ts @@ -24,7 +24,7 @@ export const serviceStartDateComparator = ( const isInRemovedDates = (service: Service, currentDate: Date): boolean => service.removed_dates.includes(dateObjectToString(currentDate)); -const isInAddedDates = (service: Service, currentDate: Date): boolean => +export const isInAddedDates = (service: Service, currentDate: Date): boolean => service.added_dates.includes(dateObjectToString(currentDate)); const isOnValidDay = (service: Service, currentDate: Date): boolean => { @@ -40,10 +40,7 @@ export const isInCurrentService = ( ): boolean => { const serviceStartDate = stringToDateObject(service.start_date); const serviceEndDate = stringToDateObject(service.end_date); - return ( - isInAddedDates(service, currentDate) || - (currentDate >= serviceStartDate && currentDate <= serviceEndDate) - ); + return currentDate >= serviceStartDate && currentDate <= serviceEndDate; }; export const isInCurrentRating = ( @@ -155,6 +152,7 @@ export const groupServicesByDateRating = ( // additionally, prioritize current service/rating in grouping if (!service.rating_end_date) { if ( + isInAddedDates(service, currentDate) || isInCurrentService(service, currentDate) || isInCurrentRating(service, currentDate) ) { diff --git a/assets/ts/schedule/components/schedule-finder/ScheduleModalContent.tsx b/assets/ts/schedule/components/schedule-finder/ScheduleModalContent.tsx index 58f1477178..e0b9b74fd0 100644 --- a/assets/ts/schedule/components/schedule-finder/ScheduleModalContent.tsx +++ b/assets/ts/schedule/components/schedule-finder/ScheduleModalContent.tsx @@ -15,7 +15,7 @@ import UpcomingDepartures from "./upcoming-departures/UpcomingDepartures"; import { isSubwayRoute } from "../../../models/route"; import DailyScheduleSubway from "./daily-schedule/DailyScheduleSubway"; import formattedDate, { stringToDateObject } from "../../../helpers/date"; -import { isInCurrentService } from "../../../helpers/service"; +import { isInAddedDates, isInCurrentService } from "../../../helpers/service"; interface Props { handleChangeDirection: (direction: DirectionId) => void; @@ -46,8 +46,10 @@ const ScheduleModalContent = ({ }: Props): ReactElement | null => { const { id: routeId } = route; - const serviceToday = services.some(service => - isInCurrentService(service, stringToDateObject(today)) + const serviceToday = services.some( + service => + isInCurrentService(service, stringToDateObject(today)) || + isInAddedDates(service, stringToDateObject(today)) ); const renderUpcomingDepartures = (): ReactElement => diff --git a/assets/ts/schedule/components/schedule-finder/daily-schedule/DailySchedule.tsx b/assets/ts/schedule/components/schedule-finder/daily-schedule/DailySchedule.tsx index 362c21e87c..c3e1ec990f 100644 --- a/assets/ts/schedule/components/schedule-finder/daily-schedule/DailySchedule.tsx +++ b/assets/ts/schedule/components/schedule-finder/daily-schedule/DailySchedule.tsx @@ -163,11 +163,11 @@ export const DailySchedule = ({ const currentServices = sortedServices.filter(service => isCurrentValidService(service, todayDate) ); - const todayServiceId = - currentServices.length > 0 ? currentServices[0].id : ""; - const [defaultSelectedService] = currentServices.length - ? currentServices - : sortedServices; + const todayService = + currentServices.find(service => !!service["default_service?"]) || + currentServices[0]; + const todayServiceId = todayService?.id || ""; + const defaultSelectedService = todayService || sortedServices[0]; const [selectedService, setSelectedService] = useState( defaultSelectedService diff --git a/assets/ts/schedule/components/schedule-finder/daily-schedule/ServiceOptGroup.tsx b/assets/ts/schedule/components/schedule-finder/daily-schedule/ServiceOptGroup.tsx index 74767a8073..3924d85a36 100644 --- a/assets/ts/schedule/components/schedule-finder/daily-schedule/ServiceOptGroup.tsx +++ b/assets/ts/schedule/components/schedule-finder/daily-schedule/ServiceOptGroup.tsx @@ -5,6 +5,7 @@ import { shortDate, stringToDateObject } from "../../../../helpers/date"; import { ServiceGroupNames, dedupeServices, + isInAddedDates, isInCurrentService } from "../../../../helpers/service"; @@ -57,7 +58,10 @@ const ServiceOptGroup = ({ if (service.id === todayServiceId) { // if it's the only service this rating, this service might not // technically be now - adjust text for that - if (isInCurrentService(service, nowDate)) { + if ( + isInCurrentService(service, nowDate) || + isInAddedDates(service, nowDate) + ) { optionText += " (now)"; } else { optionText += ` (Starting ${format(startDate, "MMMM d, y")})`;