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

Fixing inconsistent prev/next navigation buttons #657

Closed
wants to merge 4 commits into from
Closed
Changes from all 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
40 changes: 16 additions & 24 deletions src/components/presentation/NavigationButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 'src/components/shared';

Expand All @@ -24,29 +25,20 @@ export function NavigationButtons(props: INavigationButtons) {

const keepScrollPos = useAppSelector((state) => state.formLayout.uiConfig.keepScrollPos);

const [disableBack, setDisableBack] = React.useState<boolean>(false);
const [disableNext, setDisableNext] = React.useState<boolean>(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]);
Expand Down Expand Up @@ -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),
};
}