Skip to content

Commit

Permalink
fix(DailySchedule): improve identification of today's service (#2274)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecristen authored Dec 16, 2024
1 parent 5ac3f2a commit 4aabef8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
8 changes: 3 additions & 5 deletions assets/ts/helpers/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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 = (
Expand Down Expand Up @@ -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)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,8 +46,10 @@ const ScheduleModalContent = ({
}: Props): ReactElement<HTMLElement> | 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<HTMLElement> =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { shortDate, stringToDateObject } from "../../../../helpers/date";
import {
ServiceGroupNames,
dedupeServices,
isInAddedDates,
isInCurrentService
} from "../../../../helpers/service";

Expand Down Expand Up @@ -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")})`;
Expand Down

0 comments on commit 4aabef8

Please sign in to comment.