Skip to content

Commit

Permalink
refactor(MobileProfileSelector): Remove state from component, use que…
Browse files Browse the repository at this point in the history
…ry state instead.
  • Loading branch information
binh-dam-ibigroup committed Dec 19, 2024
1 parent a940a57 commit 9014843
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/components/form/advanced-settings-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ const AdvancedSettingsPanel = ({
<FormattedMessage id="components.MobilityProfile.MobilityPane.planTripDescription" />
<MobilityProfileSelector
name="forEmail"
// Don't use onSettingsUpdate, so that the dependent's mobility profile doesn't appear in the URL.
onSettingsUpdate={setQueryParam}
options={[
{
Expand Down
2 changes: 2 additions & 0 deletions lib/components/form/call-taker/advanced-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,10 @@ class AdvancedOptions extends Component {
{hasMobilityProfile && (
<MobilityProfileSelector
name="mobilityProfile"
// Use onSettingsUpdate to include the mobility profile in the URL
onSettingsUpdate={onSettingsUpdate(setQueryParam)}
options={getMobilityProfileOptions(intl)}
value={currentQuery.mobilityProfile}
/>
)}
<Suspense fallback={<span>...</span>}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DropdownSelector } from '@opentripplanner/trip-form'
import { QueryParamChangeEvent } from '@opentripplanner/trip-form/lib/types'
import { useIntl } from 'react-intl'
import React, { useCallback, useState } from 'react'
import React, { useCallback } from 'react'
import styled from 'styled-components'

const MobilityProfileDropdown = styled(DropdownSelector)`
Expand All @@ -15,26 +15,24 @@ const MobilityProfileSelector = ({
name,
onSettingsUpdate,
options,
value = 'None'
value
}: {
name: string
onSettingsUpdate: (args: Record<string, unknown>) => void
options: {
text: string
value: string
}[]
value?: string
value: string
}): JSX.Element => {
const intl = useIntl()
const [selectedProfile, setSelectedProfile] = useState<string>(value)

const onMobilityProfileChange = useCallback(
(evt: QueryParamChangeEvent) => {
const value = evt[name]
setSelectedProfile(value as string)
onSettingsUpdate({ [name]: value })
const paramValue = evt[name]
onSettingsUpdate({ [name]: paramValue })
},
[name, setSelectedProfile, onSettingsUpdate]
[name, onSettingsUpdate]
)

return (
Expand All @@ -45,7 +43,7 @@ const MobilityProfileSelector = ({
name={name}
onChange={onMobilityProfileChange}
options={options}
value={selectedProfile}
value={value}
/>
)
}
Expand Down

0 comments on commit 9014843

Please sign in to comment.