Skip to content

Commit

Permalink
improve navigation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
raquelmsmith committed Sep 4, 2024
1 parent c0e0d6f commit 9f32c0c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
25 changes: 24 additions & 1 deletion frontend/src/scenes/dashboard/dashboardTemplateVariablesLogic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { actions, kea, path, props, propsChanged, reducers, selectors } from 'kea'
import { actions, kea, listeners, path, props, propsChanged, reducers, selectors } from 'kea'
import { isEmptyObject } from 'lib/utils'

import { DashboardTemplateVariableType, FilterType, Optional } from '~/types'
Expand Down Expand Up @@ -26,7 +26,9 @@ export const dashboardTemplateVariablesLogic = kea<dashboardTemplateVariablesLog
}),
setActiveVariableIndex: (index: number) => ({ index }),
incrementActiveVariableIndex: true,
possiblyIncrementActiveVariableIndex: true,
resetVariable: (variableId: string) => ({ variableId }),
goToNextUntouchedActiveVariableIndex: true,
}),
reducers({
variables: [
Expand Down Expand Up @@ -81,6 +83,27 @@ export const dashboardTemplateVariablesLogic = kea<dashboardTemplateVariablesLog
},
],
})),
listeners(({ actions, props, values }) => ({
possiblyIncrementActiveVariableIndex: () => {
if (props.variables.length > 0 && values.activeVariableIndex < props.variables.length - 1) {
actions.incrementActiveVariableIndex()
}
},
goToNextUntouchedActiveVariableIndex: () => {
let nextIndex = values.variables.findIndex((v, i) => !v.touched && i > values.activeVariableIndex)
if (nextIndex !== -1) {
actions.setActiveVariableIndex(nextIndex)
return
}
if (nextIndex == -1) {
nextIndex = values.variables.findIndex((v) => !v.touched)
if (nextIndex == -1) {
nextIndex = values.activeVariableIndex
}
}
actions.setActiveVariableIndex(nextIndex)
},
})),
propsChanged(({ actions, props }, oldProps) => {
if (props.variables !== oldProps.variables) {
actions.setVariables(props.variables)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ function VariableSelector({
const theDashboardTemplateVariablesLogic = dashboardTemplateVariablesLogic({
variables: activeDashboardTemplate?.variables || [],
})
const { setVariable, resetVariable, incrementActiveVariableIndex } = useActions(theDashboardTemplateVariablesLogic)
const { setVariable, resetVariable, goToNextUntouchedActiveVariableIndex, incrementActiveVariableIndex } =
useActions(theDashboardTemplateVariablesLogic)
const { allVariablesAreTouched, variables, activeVariableIndex } = useValues(theDashboardTemplateVariablesLogic)
const [customEventName, setCustomEventName] = useState<string | null>(null)
const [showCustomEventField, setShowCustomEventField] = useState(false)

Expand Down Expand Up @@ -104,9 +106,24 @@ function VariableSelector({
) : (
<div className="flex">
{variable.touched ? (
<LemonButton type="primary" status="alt" onClick={incrementActiveVariableIndex}>
Continue
</LemonButton>
<>
{!allVariablesAreTouched ||
(allVariablesAreTouched && variables.length !== activeVariableIndex + 1) ? (
<LemonButton
type="primary"
status="alt"
onClick={() =>
!allVariablesAreTouched
? goToNextUntouchedActiveVariableIndex()
: variables.length !== activeVariableIndex + 1
? incrementActiveVariableIndex()
: null
}
>
Continue
</LemonButton>
) : null}
</>
) : (
<div className="flex gap-x-2">
<LemonButton
Expand Down

0 comments on commit 9f32c0c

Please sign in to comment.