Skip to content

Commit

Permalink
Clean up date selection and remaining small bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
devinmatte committed Mar 1, 2025
1 parent 926d9d3 commit 51df9fb
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 20 deletions.
6 changes: 3 additions & 3 deletions common/components/inputs/DateSelection/DatePickers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import Flatpickr from 'react-flatpickr';
import 'flatpickr/dist/themes/light.css';
import { useDelimitatedRoute, useUpdateQuery } from '../../../utils/router';
import { FLAT_PICKER_OPTIONS, TODAY_STRING, RANGE_PRESETS } from '../../../constants/dates';
import { TODAY_STRING, RANGE_PRESETS, getDatePickerOptions } from '../../../constants/dates';
import { buttonHighlightFocus } from '../../../styles/general';
import type { Line } from '../../../types/lines';
import { ALL_PAGES } from '../../../constants/pages';
Expand Down Expand Up @@ -108,7 +108,7 @@ export const DatePickers: React.FC<DatePickerProps> = ({ range, setRange, type,
value={startDate ?? date}
key={'start'}
placeholder={'mm/dd/yyyy'}
options={FLAT_PICKER_OPTIONS[tab]}
options={getDatePickerOptions(tab, page)}
onChange={(dates, currentDateString) => {
if (isSingleDate) {
handleDateChange(currentDateString);
Expand All @@ -135,7 +135,7 @@ export const DatePickers: React.FC<DatePickerProps> = ({ range, setRange, type,
value={endDate}
key={'end'}
placeholder={'mm/dd/yyyy'}
options={FLAT_PICKER_OPTIONS[tab]}
options={getDatePickerOptions(tab, page)}
onChange={(dates, currentDateString) => {
handleEndDateChange(currentDateString);
}}
Expand Down
2 changes: 1 addition & 1 deletion common/components/nav/MenuDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const MenuDropdown: React.FC<MenuDropdownProps> = ({ line, route, childre
case 'line-bus':
return `/bus/trips/single?busRoute=1&date=${BUS_DEFAULTS.singleTripConfig.date}`;
case 'line-commuter-rail':
return `/commuter-rail/trips/single?crRoute=CR-Lowell&date=${COMMUTER_RAIL_DEFAULTS.singleTripConfig.date}`;
return `/commuter-rail/trips/single?crRoute=CR-Fairmount&date=${COMMUTER_RAIL_DEFAULTS.singleTripConfig.date}`;
default:
return getLineSelectionItemHref(line, route);
}
Expand Down
2 changes: 1 addition & 1 deletion common/constants/dashboardTabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export const DASHBOARD_TABS: {
'Commuter Rail': {
name: 'Commuter Rail',
path: '/commuter-rail',
query: { crRoute: 'CR-Lowell', date: COMMUTER_RAIL_DEFAULTS.singleTripConfig.date },
query: { crRoute: 'CR-Fairmount', date: COMMUTER_RAIL_DEFAULTS.singleTripConfig.date },
},
};
24 changes: 21 additions & 3 deletions common/constants/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
SingleDateParams,
} from '../components/inputs/DateSelection/types/DateSelectionTypes';
import type { Tab } from '../types/router';
import type { Page } from './pages';

dayjs.extend(utc);
dayjs.extend(timezone);
Expand Down Expand Up @@ -40,13 +41,30 @@ export const BUS_MAX_DAY = dayjs(BUS_MAX_DATE);
export const BUS_MAX_DATE_MINUS_ONE_WEEK = dayjs(BUS_MAX_DATE)
.subtract(7, 'days')
.format(DATE_FORMAT);
export const COMMUTER_RAIL_MIN_DATE = '2022-06-22';

// Commuter Rail earliest Ridership date
export const COMMUTER_RAIL_RIDERSHIP_MIN_DATE = '2022-06-22';
export const COMMUTER_RAIL_DATA_MIN_DATE = '2024-01-01';

export const getESTDayjs = (date: string) => {
return dayjs(date).tz(est);
};

export const FLAT_PICKER_OPTIONS: {
export const getDatePickerOptions = (tab: Tab, page?: Page) => {
if (tab === 'Commuter Rail') {
if (page === 'ridership') {
return {
...FLAT_PICKER_OPTIONS[tab],
minDate: COMMUTER_RAIL_RIDERSHIP_MIN_DATE,
maxDate: TODAY_STRING,
};
}
}

return FLAT_PICKER_OPTIONS[tab];
};

const FLAT_PICKER_OPTIONS: {
[key in Tab]: DateTimePickerProps['options'];
} = {
Subway: {
Expand Down Expand Up @@ -75,7 +93,7 @@ export const FLAT_PICKER_OPTIONS: {
},
'Commuter Rail': {
enableTime: false,
minDate: COMMUTER_RAIL_MIN_DATE,
minDate: COMMUTER_RAIL_DATA_MIN_DATE,
maxDate: TODAY_STRING,
altInput: true,
altFormat: 'M j, Y',
Expand Down
3 changes: 1 addition & 2 deletions common/state/defaults/dateDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
TODAY_STRING,
YESTERDAY_STRING,
TODAY_SERVICE_STARTED,
COMMUTER_RAIL_MIN_DATE,
} from '../../constants/dates';
import type { WithOptional } from '../../types/general';

Expand Down Expand Up @@ -41,7 +40,7 @@ export const COMMUTER_RAIL_DEFAULTS: WithOptional<
> = {
lineConfig: { startDate: OVERVIEW_OPTIONS.year.startDate, endDate: TODAY_STRING },
multiTripConfig: {
startDate: COMMUTER_RAIL_MIN_DATE,
startDate: ONE_WEEK_AGO_STRING,
endDate: TODAY_STRING,
},
singleTripConfig: {
Expand Down
32 changes: 24 additions & 8 deletions common/utils/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import type { ReadonlyURLSearchParams } from 'next/navigation';
import { useSearchParams } from 'next/navigation';
import { useRouter } from 'next/router';
import { RAIL_LINES, type BusRoute, type LinePath, BUS_ROUTES } from '../types/lines';
import {
type CommuterRailRoute,
type BusRoute,
type LinePath,
COMMUTER_RAIL_ROUTES,
BUS_ROUTES,
RAIL_LINES,
} from '../types/lines';
import { TODAY_STRING } from '../constants/dates';

const getBusOrLine = (
const getLineOrRoute = (
lineString: string | BusRoute
): { type: 'rail' | 'bus'; value: LinePath | BusRoute } | undefined => {
): { type: 'rail' | 'bus' | 'cr'; value: LinePath | BusRoute | CommuterRailRoute } | undefined => {
if (RAIL_LINES.includes(lineString.toLowerCase()))
return { type: 'rail', value: lineString.toLowerCase() as LinePath };
if (BUS_ROUTES.includes(lineString.toString() as BusRoute))
return { type: 'bus', value: lineString as BusRoute };
if (COMMUTER_RAIL_ROUTES.includes(lineString.toString() as CommuterRailRoute))
return { type: 'cr', value: lineString as CommuterRailRoute };
};

export const configToQueryParams = (search: ReadonlyURLSearchParams | URLSearchParams) => {
Expand All @@ -21,8 +30,8 @@ export const configToQueryParams = (search: ReadonlyURLSearchParams | URLSearchP
: search.get('config')?.split(',');
if (!configArr || configArr.length !== 5) return;

const busOrLine = getBusOrLine(configArr[0]);
if (!busOrLine) return;
const lineOrRoute = getLineOrRoute(configArr[0]);
if (!lineOrRoute) return;

const query = {
from: configArr[1] || undefined,
Expand All @@ -42,13 +51,20 @@ export const configToQueryParams = (search: ReadonlyURLSearchParams | URLSearchP
string,
string,
][];
if (busOrLine.type === 'bus') queryArr.push(['busRoute', busOrLine.value]);
if (lineOrRoute.type === 'bus') queryArr.push(['busRoute', lineOrRoute.value]);
if (lineOrRoute.type === 'cr') queryArr.push(['crRoute', lineOrRoute.value]);

const newQueryParams = new URLSearchParams(queryArr);

if (busOrLine.type === 'rail')
if (lineOrRoute.type === 'rail')
return {
line: busOrLine.value,
line: lineOrRoute.value,
queryParams: newQueryParams,
tripSection: singleOrMulti,
};
if (lineOrRoute.type === 'cr')
return {
line: 'commuter-rail',
queryParams: newQueryParams,
tripSection: singleOrMulti,
};
Expand Down
5 changes: 4 additions & 1 deletion common/utils/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ export const getLineSelectionItemHref = (newLine: Line, route: Route): string =>
}
delete query.from;
delete query.to;
// Get queryParams but exclude busRoute and crRoute
const queryParams = query
? new URLSearchParams(Object.entries(query).filter(([key]) => key !== 'busRoute'))
? new URLSearchParams(
Object.entries(query).filter(([key]) => key !== 'busRoute' && key !== 'crRoute')
)
: new URLSearchParams();
href += currentPath ? `${currentPath}` : '';
const queryString = queryParams.toString();
Expand Down
2 changes: 1 addition & 1 deletion modules/landing/LineButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const LineButton: React.FC<LineButtonProps> = ({ children, line }) => {
<Link
href={
line === 'line-commuter-rail'
? `/${lineObject.path}/trips/single?crRoute=CR-Lowell&date=${COMMUTER_RAIL_DEFAULTS.singleTripConfig.date}`
? `/${lineObject.path}/trips/single?crRoute=CR-Fairmount&date=${COMMUTER_RAIL_DEFAULTS.singleTripConfig.date}`
: line === 'line-bus'
? `/${lineObject.path}?busRoute=1&date=${BUS_DEFAULTS.singleTripConfig.date}`
: `/${lineObject.path}`
Expand Down

0 comments on commit 51df9fb

Please sign in to comment.