Skip to content

Commit 11c5042

Browse files
committed
Legger til funksjon for å finne forrige tittel
1 parent c9e3482 commit 11c5042

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

packages/nextjs/src/components/pages/form-intermediate-step-page/FormIntermediateStepPage.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const FormIntermediateStepPage = (props: FormIntermediateStepPageProps) =
1414
const { language, type, displayName, modifiedTime, data } = props;
1515
const { title, illustration } = data;
1616

17-
const { currentStepData, backUrl } = useFormIntermediateStepPage(props);
17+
const { currentStepData, backUrl, previousStepTitle } = useFormIntermediateStepPage(props);
1818

1919
const getTranslations = translator('form', language);
2020

@@ -37,6 +37,7 @@ export const FormIntermediateStepPage = (props: FormIntermediateStepPageProps) =
3737
<div className={style.content}>
3838
<div className={style.stepOptionsWrapper}>
3939
<ParsedHtml htmlProps={currentStepData.editorial} />
40+
{previousStepTitle && <div>Forrige steg: {previousStepTitle}</div>}
4041
{currentStepData.stepsHeadline && (
4142
<Heading level={'2'} size={'medium'} spacing>
4243
{currentStepData.stepsHeadline}

packages/nextjs/src/components/pages/form-intermediate-step-page/useFormIntermediateStepPage.tsx

+36-9
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ export type FormIntermediateStep_StepLinkData = SelectableStep & {
1515
};
1616

1717
type StepPath = number[];
18-
export const buildStepUrl = (basePath: string, stepPath: StepPath) =>
18+
const buildStepUrl = (basePath: string, stepPath: StepPath) =>
1919
`${basePath}?${STEP_PARAM}=${stepPath.join(',')}`;
2020

21-
export const resolveStepUrl = ({
21+
const resolveStepUrl = ({
2222
step,
2323
nextStepPath,
2424
basePath,
@@ -50,10 +50,7 @@ export const resolveStepUrl = ({
5050
}
5151
};
5252

53-
export const getStepData = (
54-
data: FormIntermediateStepPageProps['data'],
55-
stepPath: StepPath
56-
): StepBase => {
53+
const getStepData = (data: FormIntermediateStepPageProps['data'], stepPath: StepPath): StepBase => {
5754
// No steps selected (meaning the user is on first step)
5855
if (stepPath.length === 0) {
5956
return {
@@ -84,7 +81,7 @@ export const getStepData = (
8481
};
8582
};
8683

87-
export const buildCurrentStepData = (
84+
const buildCurrentStepData = (
8885
allData: FormIntermediateStepPageProps['data'],
8986
basePath: string,
9087
stepPath: StepPath
@@ -99,7 +96,7 @@ export const buildCurrentStepData = (
9996
};
10097
};
10198

102-
export const buildBackUrl = (basePath: string, stepPath: StepPath): string | null => {
99+
const buildBackUrl = (basePath: string, stepPath: StepPath): string | null => {
103100
if (stepPath.length === 0) {
104101
return null; // No back URL if on the first step
105102
}
@@ -110,7 +107,7 @@ export const buildBackUrl = (basePath: string, stepPath: StepPath): string | nul
110107
return buildStepUrl(basePath, stepPath.slice(0, -1));
111108
};
112109

113-
export const getStepPathFromParam = (url: string): StepPath => {
110+
const getStepPathFromParam = (url: string): StepPath => {
114111
const stepQuery = new URL(url, window.location.origin).searchParams.get(STEP_PARAM);
115112
const stepPath = stepQuery ? stepQuery.split(',').map(Number) : [];
116113
if (stepPath.some(isNaN)) {
@@ -119,6 +116,34 @@ export const getStepPathFromParam = (url: string): StepPath => {
119116
return stepPath;
120117
};
121118

119+
const getPreviousStepTitle = (
120+
stepPath: StepPath,
121+
allData: FormIntermediateStepPageProps['data']
122+
) => {
123+
if (stepPath.length === 0) {
124+
return null; // No previous step title if on the first step
125+
}
126+
127+
const previousStepPath = stepPath.slice(0, -1);
128+
129+
// Previous step was the first page, so just get the
130+
// headline from the data root.
131+
if (previousStepPath.length === 0) {
132+
return allData.stepsHeadline;
133+
}
134+
135+
// Traverse the tree to find the previous step.
136+
let step: StepBase = allData;
137+
previousStepPath.forEach((index) => {
138+
const foundStep = step.steps[index];
139+
if (foundStep) {
140+
step = foundStep.nextStep?.next;
141+
}
142+
});
143+
144+
return step.stepsHeadline;
145+
};
146+
122147
export const useFormIntermediateStepPage = (props: FormIntermediateStepPageProps) => {
123148
const [stepPath, setStepPath] = useState<StepPath>([]);
124149
const router = useRouter();
@@ -127,6 +152,7 @@ export const useFormIntermediateStepPage = (props: FormIntermediateStepPageProps
127152
const currentStepData = buildCurrentStepData(props.data, pagePath, stepPath);
128153

129154
const backUrl = buildBackUrl(pagePath, stepPath);
155+
const previousStepTitle = getPreviousStepTitle(stepPath, props.data);
130156

131157
useEffect(() => {
132158
if (!router) {
@@ -148,5 +174,6 @@ export const useFormIntermediateStepPage = (props: FormIntermediateStepPageProps
148174
return {
149175
currentStepData,
150176
backUrl,
177+
previousStepTitle,
151178
};
152179
};

0 commit comments

Comments
 (0)