Skip to content
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

Deselect all submodes #1265

Merged
merged 15 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions lib/components/form/advanced-settings-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
ModeSetting,
ModeSettingValues
} from '@opentripplanner/types'
import React, { RefObject, useContext, useState } from 'react'
import { QueryParamChangeEvent } from '@opentripplanner/trip-form/lib/types'
import React, { RefObject, useContext, useEffect, useState } from 'react'
import styled from 'styled-components'

import * as formActions from '../../actions/form'
Expand All @@ -28,6 +29,7 @@ import PageTitle from '../util/page-title'
import {
addCustomSettingLabels,
addModeButtonIcon,
onAdvancedModeSubsettingsUpdate,
onSettingsUpdate,
pipe,
populateSettingWithIcon,
Expand Down Expand Up @@ -118,7 +120,8 @@ const AdvancedSettingsPanel = ({
modeButtonOptions,
modeSettingDefinitions,
modeSettingValues,
setQueryParam
setQueryParam,
urlSearchParams
}: {
closeAdvancedSettings: () => void
enabledModeButtons: string[]
Expand All @@ -127,7 +130,8 @@ const AdvancedSettingsPanel = ({
modeSettingDefinitions: ModeSetting[]
modeSettingValues: ModeSettingValues
onPlanTripClick: () => void
setQueryParam: (evt: any) => void
setQueryParam: (evt: QueryParamChangeEvent) => void
josh-willis-arcadis marked this conversation as resolved.
Show resolved Hide resolved
urlSearchParams: URLSearchParams
}): JSX.Element => {
const [closingBySave, setClosingBySave] = useState(false)
const [closingByX, setClosingByX] = useState(false)
Expand Down Expand Up @@ -168,6 +172,7 @@ const AdvancedSettingsPanel = ({
)

const processedModeSettings = processSettings(modeSettingDefinitions)

josh-willis-arcadis marked this conversation as resolved.
Show resolved Hide resolved
const processedModeButtons = modeButtonOptions.map(
pipe(
addModeButtonIcon(ModeIcon),
Expand All @@ -176,6 +181,11 @@ const AdvancedSettingsPanel = ({
)
)

const handleModeButtonToggle = setModeButton(
enabledModeButtons,
onSettingsUpdate(setQueryParam)
)

return (
<PanelOverlay className="advanced-settings" ref={innerRef}>
<HeaderContainer>
Expand Down Expand Up @@ -213,11 +223,12 @@ const AdvancedSettingsPanel = ({
fillModeIcons
label="test"
modeButtons={processedModeButtons}
onSettingsUpdate={onSettingsUpdate(setQueryParam)}
onToggleModeButton={setModeButton(
enabledModeButtons,
onSettingsUpdate(setQueryParam)
onSettingsUpdate={onAdvancedModeSubsettingsUpdate(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I am uncomfortable with here is that the advanced settings panel already has the mode button that corresponds with the mode setting that is being updated. I don't think we should do a bunch of work to get that mode button item again. It will require an update to the OTP UI component but I think there's a better way to go about this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, maybe it would be possible to do this change almost entirely within OTP UI? Could we just add a prop for transportModesDisableCascade? Weird name idk but something like that

setQueryParam,
processedModeButtons,
handleModeButtonToggle
)}
onToggleModeButton={handleModeButtonToggle}
/>
<ReturnToTripPlanButton
className="save-settings-button"
Expand Down Expand Up @@ -259,7 +270,8 @@ const mapStateToProps = (state: AppReduxState) => {
[],
modeButtonOptions: state.otp.config?.modes?.modeButtons || [],
modeSettingDefinitions: state.otp?.modeSettingDefinitions || [],
modeSettingValues
modeSettingValues,
urlSearchParams
}
}

Expand Down
61 changes: 60 additions & 1 deletion lib/components/form/util.tsx
josh-willis-arcadis marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,75 @@ export const onSettingsUpdate =
setQueryParam({ queryParamData: params, ...params })
}

const getModeButtonFromSubmode = (
key: string,
modeButtons: ModeButtonDefinition[]
): ModeButtonDefinition | undefined => {
return modeButtons.find((button: ModeButtonDefinition) => {
if (button.modeSettings) {
return button.modeSettings.some((setting: ModeSetting) => {
// check if transport mode
if (
(setting.type === 'CHECKBOX' || setting.type === 'SUBMODE') &&
setting.addTransportMode
) {
return setting.key === key
}
return false
})
}
return false
})
}

export const onAdvancedModeSubsettingsUpdate =
(
setQueryParam: (evt: any) => void,
processedModeButtons: ModeButtonDefinition[],
handleModeButtonToggle: (buttonId: string, newState: boolean) => void
) =>
(params: any) => {
// check if setting is a transport mode and get the mode button that contains it
const modeButton = getModeButtonFromSubmode(
Object.keys(params)[0],
processedModeButtons
)
if (modeButton && modeButton.modeSettings && !Object.values(params)[0]) {
const transportModeSettings = modeButton.modeSettings.filter(
(setting: ModeSetting) =>
(setting.type === 'CHECKBOX' || setting.type === 'SUBMODE') &&
setting.addTransportMode
)
// if we're disabling a transport mode, we need to check if the mode button needs to be disabled (all of its subsettings are false)
const allFalse = transportModeSettings.every((setting: ModeSetting) => {
if (setting.key === Object.keys(params)[0]) {
return !Object.values(params)[0]
}
return !setting.value
})
if (allFalse) {
transportModeSettings.forEach((setting) => (params[setting.key] = true))
handleModeButtonToggle(modeButton.key, false)
}
}
setQueryParam({ queryParamData: params, ...params })
}

export const setModeButton =
(enabledModeButtons: string[], updateHandler: (params: any) => void) =>
(buttonId: string, newState: boolean) => {
console.log('SET MODE BUTTON IS BEING CALLED. NEW STATE:::: ', newState)
daniel-heppner-ibigroup marked this conversation as resolved.
Show resolved Hide resolved
let newButtons

if (newState) {
// enable modeButton
// we need to add it to the list of enabled buttons
newButtons = [...enabledModeButtons, buttonId]
} else {
// disable modeButton
// we need to remove it from the list of enabled buttons
newButtons = enabledModeButtons.filter((c) => c !== buttonId)
}

// encodeQueryParams serializes the mode buttons for the URL
// to get nice looking URL params and consistency
updateHandler(
Expand Down