Skip to content

Commit

Permalink
Prevent autoplan if there are validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
amy-corson-ibigroup committed Oct 16, 2024
1 parent 4b0ff35 commit 8a245b8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
12 changes: 9 additions & 3 deletions lib/components/form/advanced-settings-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
onSettingsUpdate,
pipe,
populateSettingWithIcon,
setModeButton
setModeButton,
tripPlannerValidationErrors
} from './util'
import { setModeButtonEnabled } from './batch-settings'
import { styledCheckboxCss } from './styled'
Expand Down Expand Up @@ -113,6 +114,7 @@ const DtSelectorContainer = styled.div`
const AdvancedSettingsPanel = ({
autoPlan,
closeAdvancedSettings,
currentQuery,
enabledModeButtons,
handlePlanTrip,
innerRef,
Expand All @@ -125,6 +127,7 @@ const AdvancedSettingsPanel = ({
}: {
autoPlan: boolean
closeAdvancedSettings: () => void
currentQuery: any
enabledModeButtons: string[]
handlePlanTrip: () => void
innerRef: RefObject<HTMLDivElement>
Expand Down Expand Up @@ -181,10 +184,13 @@ const AdvancedSettingsPanel = ({
)
)

const tripFormErrors = tripPlannerValidationErrors(currentQuery, intl)

const closePanel = useCallback(() => {
autoPlan && handlePlanTrip()
// Only autoplan if there are no validation errors
tripFormErrors.length === 0 && autoPlan && handlePlanTrip()
closeAdvancedSettings()
}, [autoPlan, closeAdvancedSettings, handlePlanTrip])
}, [autoPlan, closeAdvancedSettings, handlePlanTrip, tripFormErrors.length])

const onSaveAndReturnClick = useCallback(async () => {
await setCloseAdvancedSettingsWithDelay()
Expand Down
20 changes: 14 additions & 6 deletions lib/components/form/util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,10 @@ export const setModeButton =
)
}

export const alertUserTripPlan = (
intl: IntlShape,
export const tripPlannerValidationErrors = (
currentQuery: any,
onPlanTripClick: () => void,
routingQuery: () => any
): void => {
// Check for any validation issues in query.
intl: IntlShape
): string[] => {
const issues: string[] = []
if (!hasValidLocation(currentQuery, 'from')) {
issues.push(intl.formatMessage({ id: 'components.BatchSettings.origin' }))
Expand All @@ -88,6 +85,17 @@ export const alertUserTripPlan = (
intl.formatMessage({ id: 'components.BatchSettings.destination' })
)
}
return issues
}

export const alertUserTripPlan = (
intl: IntlShape,
currentQuery: any,
onPlanTripClick: () => void,
routingQuery: () => any
): void => {
// Check for any validation issues in query and alert user.
const issues = tripPlannerValidationErrors(currentQuery, intl)
onPlanTripClick()
if (issues.length > 0) {
// TODO: replace with less obtrusive validation.
Expand Down

0 comments on commit 8a245b8

Please sign in to comment.