From c555a7ced65467a25d151c3201169ca80c2e870e Mon Sep 17 00:00:00 2001 From: Ole Martin Handeland Date: Tue, 15 Nov 2022 13:54:10 +0100 Subject: [PATCH] Allowing the next/previous buttons to figure out which view is the next one even if navigationConfig is not set/configured --- .../presentation/NavigationButtons.tsx | 40 ++++++++----------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/altinn-app-frontend/src/components/presentation/NavigationButtons.tsx b/src/altinn-app-frontend/src/components/presentation/NavigationButtons.tsx index f39b418ca8..c6474acdd1 100644 --- a/src/altinn-app-frontend/src/components/presentation/NavigationButtons.tsx +++ b/src/altinn-app-frontend/src/components/presentation/NavigationButtons.tsx @@ -6,11 +6,12 @@ import type { PropsFromGenericComponent } from '..'; import { useAppDispatch, useAppSelector } from 'src/common/hooks'; import { FormLayoutActions } from 'src/features/form/layout/formLayoutSlice'; -import { selectLayoutOrder } from 'src/selectors/getLayoutOrder'; +import { getLayoutOrderFromTracks, selectLayoutOrder } from 'src/selectors/getLayoutOrder'; import { Triggers } from 'src/types'; +import { getNextView } from 'src/utils/formLayout'; import { getTextFromAppOrDefault } from 'src/utils/textResource'; import type { IKeepComponentScrollPos } from 'src/features/form/layout/formLayoutTypes'; -import type { ILayoutNavigation, INavigationConfig } from 'src/types'; +import type { ILayoutNavigation, IRuntimeState } from 'src/types'; import { AltinnButton } from 'altinn-shared/components'; @@ -24,29 +25,20 @@ export function NavigationButtons(props: INavigationButtons) { const keepScrollPos = useAppSelector((state) => state.formLayout.uiConfig.keepScrollPos); - const [disableBack, setDisableBack] = React.useState(false); - const [disableNext, setDisableNext] = React.useState(false); const currentView = useAppSelector((state) => state.formLayout.uiConfig.currentView); const orderedLayoutKeys = useAppSelector(selectLayoutOrder); const returnToView = useAppSelector((state) => state.formLayout.uiConfig.returnToView); const textResources = useAppSelector((state) => state.textResources.resources); const language = useAppSelector((state) => state.language.language); const pageTriggers = useAppSelector((state) => state.formLayout.uiConfig.pageTriggers); - const { next, previous } = useAppSelector((state) => - getNavigationConfigForCurrentView( - state.formLayout.uiConfig.navigationConfig, - state.formLayout.uiConfig.currentView, - ), - ); + const { next, previous } = useAppSelector((state) => getNavigationConfigForCurrentView(state)); const triggers = props.triggers || pageTriggers; const nextTextKey = returnToView ? 'form_filler.back_to_summary' : props.textResourceBindings?.next || 'next'; const backTextKey = props.textResourceBindings?.back || 'back'; - React.useEffect(() => { - const currentViewIndex = orderedLayoutKeys?.indexOf(currentView); - setDisableBack(!!returnToView || (!previous && currentViewIndex === 0)); - setDisableNext(!returnToView && !next && currentViewIndex === (orderedLayoutKeys?.length || 0) - 1); - }, [currentView, orderedLayoutKeys, next, previous, returnToView]); + const currentViewIndex = orderedLayoutKeys?.indexOf(currentView); + const disableBack = !!returnToView || (!previous && currentViewIndex === 0); + const disableNext = !returnToView && !next && currentViewIndex === (orderedLayoutKeys?.length || 0) - 1; const onClickPrevious = () => { const goToView = previous || (orderedLayoutKeys && orderedLayoutKeys[orderedLayoutKeys.indexOf(currentView) - 1]); @@ -138,14 +130,14 @@ export function NavigationButtons(props: INavigationButtons) { ); } -function getNavigationConfigForCurrentView( - navigationConfig: INavigationConfig | undefined, - currentView: string, -): ILayoutNavigation { - const out = navigationConfig && navigationConfig[currentView]; - if (out) { - return out; - } +function getNavigationConfigForCurrentView(state: IRuntimeState): ILayoutNavigation { + const currentView = state.formLayout.uiConfig.currentView; + const navConfig = + state.formLayout.uiConfig.navigationConfig && state.formLayout.uiConfig.navigationConfig[currentView]; + const order = getLayoutOrderFromTracks(state.formLayout.uiConfig.tracks); - return {}; + return { + previous: getNextView(navConfig, order, currentView, true), + next: getNextView(navConfig, order, currentView), + }; }