Skip to content

Commit

Permalink
feat(advanced-settings-panel): add support for onAllSubmodesDisabled …
Browse files Browse the repository at this point in the history
…prop
  • Loading branch information
josh-willis-arcadis committed Sep 10, 2024
1 parent f24376b commit f452700
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 60 deletions.
12 changes: 6 additions & 6 deletions lib/components/form/advanced-settings-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import PageTitle from '../util/page-title'
import {
addCustomSettingLabels,
addModeButtonIcon,
onAdvancedModeSubsettingsUpdate,
onSettingsUpdate,
pipe,
populateSettingWithIcon,
Expand Down Expand Up @@ -186,6 +185,10 @@ const AdvancedSettingsPanel = ({
onSettingsUpdate(setQueryParam)
)

const handleAllSubmodesDisabled = (modeButton: ModeButtonDefinition) => {
handleModeButtonToggle(modeButton.key, false)
}

return (
<PanelOverlay className="advanced-settings" ref={innerRef}>
<HeaderContainer>
Expand Down Expand Up @@ -223,11 +226,8 @@ const AdvancedSettingsPanel = ({
fillModeIcons
label="test"
modeButtons={processedModeButtons}
onSettingsUpdate={onAdvancedModeSubsettingsUpdate(
setQueryParam,
processedModeButtons,
handleModeButtonToggle
)}
onAllSubmodesDisabled={handleAllSubmodesDisabled}
onSettingsUpdate={onSettingsUpdate(setQueryParam)}
onToggleModeButton={handleModeButtonToggle}
/>
<ReturnToTripPlanButton
Expand Down
54 changes: 0 additions & 54 deletions lib/components/form/util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,60 +56,6 @@ 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) => {
Expand Down

0 comments on commit f452700

Please sign in to comment.