Merge branch 'develop' into feature/tracking

This commit is contained in:
Linus Flood
2024-10-24 12:39:34 +02:00
221 changed files with 5789 additions and 1491 deletions

View File

@@ -12,6 +12,8 @@ import { StepEnum } from "@/types/components/enterDetails/step"
import { bedTypeEnum } from "@/types/enums/bedType"
import { breakfastEnum } from "@/types/enums/breakfast"
const SESSION_STORAGE_KEY = "enterDetails"
interface EnterDetailsState {
data: {
bedType: bedTypeEnum | undefined
@@ -24,16 +26,18 @@ interface EnterDetailsState {
completeStep: (updatedData: Partial<EnterDetailsState["data"]>) => void
navigate: (
step: StepEnum,
searchParams?: Record<string, string | boolean>
updatedData?: Record<string, string | boolean>
) => void
setCurrentStep: (step: StepEnum) => void
openSidePeek: (key: SidePeekEnum | null) => void
closeSidePeek: () => void
}
export function initEditDetailsState(currentStep: StepEnum) {
const isBrowser = typeof window !== "undefined"
const sessionData = isBrowser ? sessionStorage.getItem("editDetails") : null
const search = isBrowser ? new URLSearchParams(window.location.search) : null
const sessionData = isBrowser
? sessionStorage.getItem(SESSION_STORAGE_KEY)
: null
const defaultData: EnterDetailsState["data"] = {
bedType: undefined,
@@ -50,19 +54,8 @@ export function initEditDetailsState(currentStep: StepEnum) {
}
let inputData = {}
if (search?.size) {
const searchParams: Record<string, string | boolean> = {}
search.forEach((value, key) => {
// Handle boolean values
if (value === "true" || value === "false") {
searchParams[key] = JSON.parse(value) as true | false
} else {
searchParams[key] = value
}
})
inputData = searchParams
if (sessionData) {
inputData = JSON.parse(sessionData)
}
const validPaths = [StepEnum.selectBed]
@@ -98,25 +91,34 @@ export function initEditDetailsState(currentStep: StepEnum) {
if (!validPaths.includes(currentStep)) {
currentStep = validPaths.pop()! // We will always have at least one valid path
if (isBrowser) {
window.history.pushState({}, "", currentStep + window.location.search)
window.history.pushState(
{ step: currentStep },
"",
currentStep + window.location.search
)
}
}
return create<EnterDetailsState>()((set, get) => ({
data: initialData,
steps: Object.values(StepEnum),
navigate: (step, searchParams) =>
setCurrentStep: (step) => set({ currentStep: step }),
navigate: (step, updatedData) =>
set(
produce((state) => {
const query = new URLSearchParams(window.location.search)
if (searchParams) {
Object.entries(searchParams).forEach(([key, value]) => {
query.set(key, value ? value.toString() : "")
})
}
const sessionStorage = window.sessionStorage
const previousDataString = sessionStorage.getItem(SESSION_STORAGE_KEY)
const previousData = JSON.parse(previousDataString || "{}")
sessionStorage.setItem(
SESSION_STORAGE_KEY,
JSON.stringify({ ...previousData, ...updatedData })
)
state.currentStep = step
window.history.pushState({}, "", step + "?" + query.toString())
window.history.pushState({ step }, "", step + window.location.search)
})
),
openSidePeek: (key) => set({ activeSidePeek: key }),