@@ -15,10 +15,10 @@ export type FormIntermediateStep_StepLinkData = SelectableStep & {
15
15
} ;
16
16
17
17
type StepPath = number [ ] ;
18
- export const buildStepUrl = ( basePath : string , stepPath : StepPath ) =>
18
+ const buildStepUrl = ( basePath : string , stepPath : StepPath ) =>
19
19
`${ basePath } ?${ STEP_PARAM } =${ stepPath . join ( ',' ) } ` ;
20
20
21
- export const resolveStepUrl = ( {
21
+ const resolveStepUrl = ( {
22
22
step,
23
23
nextStepPath,
24
24
basePath,
@@ -50,10 +50,7 @@ export const resolveStepUrl = ({
50
50
}
51
51
} ;
52
52
53
- export const getStepData = (
54
- data : FormIntermediateStepPageProps [ 'data' ] ,
55
- stepPath : StepPath
56
- ) : StepBase => {
53
+ const getStepData = ( data : FormIntermediateStepPageProps [ 'data' ] , stepPath : StepPath ) : StepBase => {
57
54
// No steps selected (meaning the user is on first step)
58
55
if ( stepPath . length === 0 ) {
59
56
return {
@@ -84,7 +81,7 @@ export const getStepData = (
84
81
} ;
85
82
} ;
86
83
87
- export const buildCurrentStepData = (
84
+ const buildCurrentStepData = (
88
85
allData : FormIntermediateStepPageProps [ 'data' ] ,
89
86
basePath : string ,
90
87
stepPath : StepPath
@@ -99,7 +96,7 @@ export const buildCurrentStepData = (
99
96
} ;
100
97
} ;
101
98
102
- export const buildBackUrl = ( basePath : string , stepPath : StepPath ) : string | null => {
99
+ const buildBackUrl = ( basePath : string , stepPath : StepPath ) : string | null => {
103
100
if ( stepPath . length === 0 ) {
104
101
return null ; // No back URL if on the first step
105
102
}
@@ -110,7 +107,7 @@ export const buildBackUrl = (basePath: string, stepPath: StepPath): string | nul
110
107
return buildStepUrl ( basePath , stepPath . slice ( 0 , - 1 ) ) ;
111
108
} ;
112
109
113
- export const getStepPathFromParam = ( url : string ) : StepPath => {
110
+ const getStepPathFromParam = ( url : string ) : StepPath => {
114
111
const stepQuery = new URL ( url , window . location . origin ) . searchParams . get ( STEP_PARAM ) ;
115
112
const stepPath = stepQuery ? stepQuery . split ( ',' ) . map ( Number ) : [ ] ;
116
113
if ( stepPath . some ( isNaN ) ) {
@@ -119,6 +116,34 @@ export const getStepPathFromParam = (url: string): StepPath => {
119
116
return stepPath ;
120
117
} ;
121
118
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
+
122
147
export const useFormIntermediateStepPage = ( props : FormIntermediateStepPageProps ) => {
123
148
const [ stepPath , setStepPath ] = useState < StepPath > ( [ ] ) ;
124
149
const router = useRouter ( ) ;
@@ -127,6 +152,7 @@ export const useFormIntermediateStepPage = (props: FormIntermediateStepPageProps
127
152
const currentStepData = buildCurrentStepData ( props . data , pagePath , stepPath ) ;
128
153
129
154
const backUrl = buildBackUrl ( pagePath , stepPath ) ;
155
+ const previousStepTitle = getPreviousStepTitle ( stepPath , props . data ) ;
130
156
131
157
useEffect ( ( ) => {
132
158
if ( ! router ) {
@@ -148,5 +174,6 @@ export const useFormIntermediateStepPage = (props: FormIntermediateStepPageProps
148
174
return {
149
175
currentStepData,
150
176
backUrl,
177
+ previousStepTitle,
151
178
} ;
152
179
} ;
0 commit comments