|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import { useRouter } from 'next/compat/router'; |
| 3 | +import { |
| 4 | + FormIntermediateStepPageProps, |
| 5 | + SelectableStep, |
| 6 | + StepBase, |
| 7 | +} from 'types/content-props/form-intermediate-step'; |
| 8 | +import { stripXpPathPrefix } from 'utils/urls'; |
| 9 | + |
| 10 | +const STEP_PARAM = 'stegvalg'; |
| 11 | + |
| 12 | +export type FormIntermediateStep_StepLinkData = SelectableStep & { |
| 13 | + href?: string; |
| 14 | + isStepNavigation?: boolean; |
| 15 | +}; |
| 16 | + |
| 17 | +type StepPath = number[]; |
| 18 | +export const buildStepUrl = (basePath: string, stepPath: StepPath) => |
| 19 | + `${basePath}?${STEP_PARAM}=${stepPath.join(',')}`; |
| 20 | + |
| 21 | +export const resolveStepUrl = ({ |
| 22 | + step, |
| 23 | + nextStepPath, |
| 24 | + basePath, |
| 25 | +}: { |
| 26 | + step: SelectableStep; |
| 27 | + nextStepPath: StepPath; |
| 28 | + basePath: string; |
| 29 | +}): FormIntermediateStep_StepLinkData => { |
| 30 | + switch (step.nextStep?._selected) { |
| 31 | + case 'external': { |
| 32 | + return { |
| 33 | + ...step, |
| 34 | + href: step.nextStep.external?.externalUrl, |
| 35 | + }; |
| 36 | + } |
| 37 | + case 'internal': { |
| 38 | + return { |
| 39 | + ...step, |
| 40 | + href: stripXpPathPrefix(step.nextStep.internal?.internalContent._path), |
| 41 | + }; |
| 42 | + } |
| 43 | + default: { |
| 44 | + return { |
| 45 | + ...step, |
| 46 | + href: buildStepUrl(basePath, nextStepPath), |
| 47 | + isStepNavigation: true, |
| 48 | + }; |
| 49 | + } |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +export const getStepData = ( |
| 54 | + data: FormIntermediateStepPageProps['data'], |
| 55 | + stepPath: StepPath |
| 56 | +): StepBase => { |
| 57 | + // No steps selected (meaning the user is on first step) |
| 58 | + if (stepPath.length === 0) { |
| 59 | + return { |
| 60 | + editorial: data.editorial, |
| 61 | + stepsHeadline: data.stepsHeadline, |
| 62 | + steps: data.steps, |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + let tmp: any = data; |
| 67 | + |
| 68 | + stepPath.forEach((index) => { |
| 69 | + const foundStep = tmp.steps[index]; |
| 70 | + if (foundStep) { |
| 71 | + tmp = foundStep.nextStep?.next; |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + const stepDetails = tmp.nextStep; |
| 76 | + if (stepDetails?._selected === 'next') { |
| 77 | + return stepDetails.next; |
| 78 | + } |
| 79 | + |
| 80 | + return { |
| 81 | + editorial: tmp.editorial, |
| 82 | + stepsHeadline: tmp.stepsHeadline, |
| 83 | + steps: tmp.steps, |
| 84 | + }; |
| 85 | +}; |
| 86 | + |
| 87 | +export const buildCurrentStepData = ( |
| 88 | + allData: FormIntermediateStepPageProps['data'], |
| 89 | + basePath: string, |
| 90 | + stepPath: StepPath |
| 91 | +): StepBase => { |
| 92 | + const stepData = getStepData(allData, stepPath); |
| 93 | + |
| 94 | + return { |
| 95 | + ...stepData, |
| 96 | + steps: stepData.steps.map((step, index) => |
| 97 | + resolveStepUrl({ step, nextStepPath: [...stepPath, index], basePath }) |
| 98 | + ), |
| 99 | + }; |
| 100 | +}; |
| 101 | + |
| 102 | +export const buildBackUrl = (basePath: string, stepPath: StepPath): string | null => { |
| 103 | + if (stepPath.length === 0) { |
| 104 | + return null; // No back URL if on the first step |
| 105 | + } |
| 106 | + |
| 107 | + if (stepPath.length === 1) { |
| 108 | + return basePath; // Back to the first step |
| 109 | + } |
| 110 | + return buildStepUrl(basePath, stepPath.slice(0, -1)); |
| 111 | +}; |
| 112 | + |
| 113 | +export const getStepPathFromParam = (url: string): StepPath => { |
| 114 | + const stepQuery = new URL(url, window.location.origin).searchParams.get(STEP_PARAM); |
| 115 | + const stepPath = stepQuery ? stepQuery.split(',').map(Number) : []; |
| 116 | + if (stepPath.some(isNaN)) { |
| 117 | + return []; |
| 118 | + } |
| 119 | + return stepPath; |
| 120 | +}; |
| 121 | + |
| 122 | +export const useFormIntermediateStepPage = (props: FormIntermediateStepPageProps) => { |
| 123 | + const [stepPath, setStepPath] = useState<StepPath>([]); |
| 124 | + const router = useRouter(); |
| 125 | + |
| 126 | + const pagePath = stripXpPathPrefix(props._path); |
| 127 | + const currentStepData = buildCurrentStepData(props.data, pagePath, stepPath); |
| 128 | + |
| 129 | + const backUrl = buildBackUrl(pagePath, stepPath); |
| 130 | + |
| 131 | + useEffect(() => { |
| 132 | + if (!router) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + const handleRouteChange = (url: string) => { |
| 137 | + setStepPath(getStepPathFromParam(url)); |
| 138 | + }; |
| 139 | + |
| 140 | + handleRouteChange(router.asPath); |
| 141 | + |
| 142 | + router.events.on('routeChangeComplete', handleRouteChange); |
| 143 | + return () => { |
| 144 | + router.events.off('routeChangeComplete', handleRouteChange); |
| 145 | + }; |
| 146 | + }, [router]); |
| 147 | + |
| 148 | + return { |
| 149 | + currentStepData, |
| 150 | + backUrl, |
| 151 | + }; |
| 152 | +}; |
0 commit comments