fix: make sure all searchparams are used in redirect

This commit is contained in:
Simon Emanuelsson
2024-11-19 11:24:17 +01:00
committed by Simon.Emanuelsson
parent 1b3999a050
commit 744af22b08
9 changed files with 65 additions and 49 deletions

View File

@@ -1,6 +1,7 @@
"use client"
import merge from "deepmerge"
import { produce } from "immer"
import { AppRouterInstance } from "next/dist/shared/lib/app-router-context.shared-runtime"
import { useContext } from "react"
import { create, useStore } from "zustand"
@@ -18,17 +19,13 @@ import { StepEnum } from "@/types/enums/step"
import type { DetailsState } from "@/types/stores/details"
import type { StepState } from "@/types/stores/steps"
function push(data: Record<string, string>, url: string) {
if (typeof window !== "undefined") {
window.history.pushState(data, "", url + window.location.search)
}
}
export function createStepsStore(
currentStep: StepEnum,
isMember: boolean,
noBedChoices: boolean,
noBreakfast: boolean
noBreakfast: boolean,
searchParams: string,
push: AppRouterInstance["push"]
) {
const isBrowser = typeof window !== "undefined"
const steps = [
@@ -51,14 +48,14 @@ export function createStepsStore(
steps.splice(1, 1)
if (currentStep === StepEnum.breakfast) {
currentStep = steps[1]
push({ step: currentStep }, currentStep)
push(`${currentStep}?${searchParams}`)
}
}
if (noBedChoices) {
if (currentStep === StepEnum.selectBed) {
currentStep = steps[1]
push({ step: currentStep }, currentStep)
push(`${currentStep}?${searchParams}`)
}
}
@@ -94,7 +91,7 @@ export function createStepsStore(
if (!validPaths.includes(currentStep) && isBrowser) {
// We will always have at least one valid path
currentStep = validPaths.pop()!
push({ step: currentStep }, currentStep)
push(`${currentStep}?${searchParams}`)
}
}