diff --git a/__tests__/components/__snapshots__/date-time-options.js.snap b/__tests__/components/__snapshots__/date-time-options.js.snap index 9e8214cdc..37415059c 100644 --- a/__tests__/components/__snapshots__/date-time-options.js.snap +++ b/__tests__/components/__snapshots__/date-time-options.js.snap @@ -28,8 +28,14 @@ exports[`components > form > call-taker > date time options should correctly han > @@ -145,8 +151,14 @@ exports[`components > form > call-taker > date time options should correctly han > @@ -262,8 +274,14 @@ exports[`components > form > call-taker > date time options should correctly han > @@ -379,8 +397,14 @@ exports[`components > form > call-taker > date time options should correctly han > @@ -496,8 +520,14 @@ exports[`components > form > call-taker > date time options should correctly han > @@ -613,8 +643,14 @@ exports[`components > form > call-taker > date time options should correctly han > @@ -730,8 +766,14 @@ exports[`components > form > call-taker > date time options should render 1`] = > diff --git a/__tests__/components/date-time-options.js b/__tests__/components/date-time-options.js index d2737d38f..931e05de5 100644 --- a/__tests__/components/date-time-options.js +++ b/__tests__/components/date-time-options.js @@ -1,3 +1,4 @@ +import '../test-utils/mock-window-matchMedia' import '../test-utils/mock-window-url' import { getMockInitialState, diff --git a/__tests__/test-utils/mock-window-matchMedia.js b/__tests__/test-utils/mock-window-matchMedia.js new file mode 100644 index 000000000..5fefd4aed --- /dev/null +++ b/__tests__/test-utils/mock-window-matchMedia.js @@ -0,0 +1,7 @@ +const mockedMediaQuery = jest.fn() +mockedMediaQuery.mockReturnValue({ matches: [] }) + +Object.defineProperty(window, 'matchMedia', { + value: mockedMediaQuery, + writable: true +}) diff --git a/lib/components/form/call-taker/date-time-picker.tsx b/lib/components/form/call-taker/date-time-picker.tsx index 282c104cc..96292df86 100644 --- a/lib/components/form/call-taker/date-time-picker.tsx +++ b/lib/components/form/call-taker/date-time-picker.tsx @@ -1,5 +1,5 @@ import { connect } from 'react-redux' -import { format, toDate } from 'date-fns-tz' +import { format, OptionsWithTZ, toDate } from 'date-fns-tz' import { getCurrentTime } from '@opentripplanner/core-utils/lib/time' import { IntlShape, useIntl } from 'react-intl' import { isMatch, parse } from 'date-fns' @@ -7,6 +7,10 @@ import { OverlayTrigger, Tooltip } from 'react-bootstrap' import coreUtils from '@opentripplanner/core-utils' import React, { useEffect, useRef, useState } from 'react' +import { AppReduxState, FilterType, SortType } from '../../../util/state-types' +import { DepartArriveTypeMap, DepartArriveValue } from '../date-time-modal' +import { updateItineraryFilter } from '../../../actions/narrative' + const { getCurrentDate, OTP_API_DATE_FORMAT, OTP_API_TIME_FORMAT } = coreUtils.time @@ -56,7 +60,7 @@ const SUPPORTED_TIME_FORMATS = [ 'HH:mm' ] -const safeFormat = (date: Date | '', time: string, options: any) => { +const safeFormat = (date: Date | '', time: string, options?: OptionsWithTZ) => { if (date === '') return '' try { return format(date, time, options) @@ -68,8 +72,9 @@ const safeFormat = (date: Date | '', time: string, options: any) => { type Props = { date?: string - departArrive?: string + departArrive?: DepartArriveValue homeTimezone: string + importedUpdateItineraryFilter: (payload: FilterType) => void onKeyDown: () => void setQueryParam: ({ date, @@ -80,6 +85,8 @@ type Props = { departArrive: string time: string }) => void + sort: SortType + syncSortWithDepartArrive?: boolean time?: string timeFormat: string } @@ -101,12 +108,15 @@ const DateTimeOptions = ({ date: initialDate, departArrive: initialDepartArrive, homeTimezone, + importedUpdateItineraryFilter, onKeyDown, setQueryParam, + sort, + syncSortWithDepartArrive, time: initialTime, timeFormat }: Props) => { - const [departArrive, setDepartArrive] = useState( + const [departArrive, setDepartArrive] = useState( initialDate || initialTime ? 'DEPART' : 'NOW' ) const [date, setDate] = useState(initialDate) @@ -187,6 +197,18 @@ const DateTimeOptions = ({ }) }) } + + if ( + syncSortWithDepartArrive && + DepartArriveTypeMap[departArrive] !== sort.type + ) { + importedUpdateItineraryFilter({ + sort: { + ...sort, + type: DepartArriveTypeMap[departArrive] + } + }) + } }, [dateTime, departArrive, homeTimezone, setQueryParam]) // Handler for updating the time and date fields when NOW is selected @@ -209,8 +231,8 @@ const DateTimeOptions = ({ return ( <>