-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: date time use url storage #1208
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { connect } from 'react-redux' | ||
import { decodeQueryParams, StringParam } from 'serialize-query-params' | ||
import coreUtils from '@opentripplanner/core-utils' | ||
import React, { useCallback } from 'react' | ||
|
||
import { AppConfig } from '../../util/config-types' | ||
import { AppReduxState } from '../../util/state-types' | ||
import { setQueryParam } from '../../actions/form' | ||
|
||
import { StyledDateTimeSelector } from './styled' | ||
|
||
export type DepartArriveValue = 'NOW' | 'DEPART' | 'ARRIVE' | ||
|
||
type Props = { | ||
config: AppConfig | ||
date: any | ||
dateFormatLegacy: string | ||
departArrive: DepartArriveValue | ||
setQueryParam: (params: any) => void | ||
time: any | ||
timeFormatLegacy: string | ||
} | ||
|
||
function DateTimeModal({ | ||
config, | ||
date, | ||
dateFormatLegacy, | ||
departArrive, | ||
setQueryParam, | ||
time, | ||
timeFormatLegacy | ||
}: Props) { | ||
/** | ||
* Stores parameters in both the Redux `currentQuery` and URL | ||
* @param params Params to store | ||
*/ | ||
const _onSettingsUpdate = useCallback( | ||
(params: any) => { | ||
console.log('setting') | ||
setQueryParam({ queryParamData: params, ...params }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we really inserting everything twice? That's so annoying |
||
}, | ||
[setQueryParam] | ||
) | ||
|
||
const { homeTimezone, isTouchScreenOnDesktop } = config | ||
const touchClassName = isTouchScreenOnDesktop | ||
? 'with-desktop-touchscreen' | ||
: '' | ||
|
||
return ( | ||
<div className="date-time-modal"> | ||
<div className="main-panel"> | ||
<StyledDateTimeSelector | ||
className={`date-time-selector ${touchClassName}`} | ||
date={date} | ||
dateFormatLegacy={dateFormatLegacy} | ||
departArrive={departArrive} | ||
onQueryParamChange={_onSettingsUpdate} | ||
time={time} | ||
// These props below are for Safari on MacOS, and legacy browsers | ||
// that don't support `<input type="time|date">`. | ||
// These props are not relevant in modern browsers, | ||
// where `<input type="time|date">` already | ||
// formats the time|date according to the OS settings. | ||
// eslint-disable-next-line react/jsx-sort-props | ||
timeFormatLegacy={timeFormatLegacy} | ||
timeZone={homeTimezone} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
const queryParamConfig = { | ||
date: StringParam, | ||
departArrive: StringParam, | ||
time: StringParam | ||
} | ||
|
||
const mapStateToProps = (state: AppReduxState) => { | ||
const config = state.otp.config | ||
const urlSearchParams = new URLSearchParams(state.router.location.search) | ||
const { date, departArrive, time } = decodeQueryParams(queryParamConfig, { | ||
date: urlSearchParams.get('date'), | ||
departArrive: urlSearchParams.get('departArrive'), | ||
time: urlSearchParams.get('time') | ||
}) | ||
|
||
return { | ||
config, | ||
date: date || coreUtils.time.getCurrentDate(), | ||
// This prop is for legacy browsers (see render method above). | ||
// @ts-expect-error why do we have two config types? | ||
dateFormatLegacy: coreUtils.time.getDateFormat(config), | ||
departArrive: (departArrive as DepartArriveValue) || 'NOW', | ||
time: time || coreUtils.time.getCurrentTime(), | ||
// This prop is for legacy browsers (see render method above). | ||
// @ts-expect-error why do we have two config types? | ||
timeFormatLegacy: coreUtils.time.getTimeFormat(config) | ||
} | ||
} | ||
|
||
const mapDispatchToProps = { | ||
setQueryParam | ||
} | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(DateTimeModal) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
import { CalendarAlt } from '@styled-icons/fa-solid/CalendarAlt' | ||
import { Clock } from '@styled-icons/fa-regular/Clock' | ||
import { connect } from 'react-redux' | ||
import { decodeQueryParams, StringParam } from 'serialize-query-params' | ||
import { toDate } from 'date-fns-tz' | ||
import coreUtils from '@opentripplanner/core-utils' | ||
import React from 'react' | ||
|
||
import { IconWithText } from '../util/styledIcon' | ||
import FormattedCalendarString from '../util/formatted-calendar-string' | ||
import FormattedDateTimePreview from '../util/formatted-date-time-preview' | ||
|
||
import { DepartArriveValue } from './date-time-modal' | ||
|
||
interface Props { | ||
date: string | ||
departArrive: string | ||
|
@@ -38,13 +42,24 @@ const DateTimePreview = ({ | |
) | ||
} | ||
|
||
const queryParamConfig = { | ||
date: StringParam, | ||
departArrive: StringParam, | ||
time: StringParam | ||
} | ||
|
||
const mapStateToProps = (state: any) => { | ||
const { date, departArrive, time } = state.otp.currentQuery | ||
const { homeTimezone: timeZone } = state.otp.config | ||
const urlSearchParams = new URLSearchParams(state.router.location.search) | ||
const { date, departArrive, time } = decodeQueryParams(queryParamConfig, { | ||
date: urlSearchParams.get('date'), | ||
departArrive: urlSearchParams.get('departArrive'), | ||
time: urlSearchParams.get('time') | ||
Comment on lines
+55
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is simple enough I don't mind the repetition |
||
}) | ||
return { | ||
date, | ||
departArrive, | ||
time, | ||
date: date || coreUtils.time.getCurrentDate(), | ||
departArrive: (departArrive as DepartArriveValue) || 'NOW', | ||
time: time || coreUtils.time.getCurrentTime(), | ||
timeZone | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops!