fix: handle back button

This commit is contained in:
Christel Westerberg
2024-10-17 16:07:30 +02:00
parent e4617d84ba
commit 2fab1fd917
4 changed files with 46 additions and 8 deletions

View File

@@ -6,6 +6,7 @@ import { serverClient } from "@/lib/trpc/server"
import BedType from "@/components/HotelReservation/EnterDetails/BedType" import BedType from "@/components/HotelReservation/EnterDetails/BedType"
import Breakfast from "@/components/HotelReservation/EnterDetails/Breakfast" import Breakfast from "@/components/HotelReservation/EnterDetails/Breakfast"
import Details from "@/components/HotelReservation/EnterDetails/Details" import Details from "@/components/HotelReservation/EnterDetails/Details"
import HistoryStateManager from "@/components/HotelReservation/EnterDetails/HistoryStateManager"
import SectionAccordion from "@/components/HotelReservation/EnterDetails/SectionAccordion" import SectionAccordion from "@/components/HotelReservation/EnterDetails/SectionAccordion"
import Payment from "@/components/HotelReservation/SelectRate/Payment" import Payment from "@/components/HotelReservation/SelectRate/Payment"
import { getIntl } from "@/i18n" import { getIntl } from "@/i18n"
@@ -37,6 +38,7 @@ export default async function StepPage({
return ( return (
<section> <section>
<HistoryStateManager />
<SectionAccordion <SectionAccordion
header="Select bed" header="Select bed"
step={StepEnum.selectBed} step={StepEnum.selectBed}

View File

@@ -0,0 +1,39 @@
"use client"
import { useCallback, useEffect } from "react"
import { useEnterDetailsStore } from "@/stores/enter-details"
export default function HistoryStateManager() {
const setCurrentStep = useEnterDetailsStore((state) => state.setCurrentStep)
const currentStep = useEnterDetailsStore((state) => state.currentStep)
const handleBackButton = useCallback(
(event: PopStateEvent) => {
if (event.state.step) {
setCurrentStep(event.state.step)
}
},
[setCurrentStep]
)
useEffect(() => {
window.addEventListener("popstate", handleBackButton)
return () => {
window.removeEventListener("popstate", handleBackButton)
}
}, [handleBackButton])
useEffect(() => {
if (!window.history.state.step) {
window.history.replaceState(
{ step: currentStep },
"",
document.location.href
)
}
}, [currentStep])
return null
}

View File

@@ -23,12 +23,9 @@ import {
import { Hotel } from "@/types/hotel" import { Hotel } from "@/types/hotel"
function getAmenitiesList(hotel: Hotel) { function getAmenitiesList(hotel: Hotel) {
const detailedAmenities: DetailedAmenity[] = hotel.hotelFacts const detailedAmenities: DetailedAmenity[] = Object.entries(
.hotelFacilityDetail hotel.hotelFacts.hotelFacilityDetail
? Object.entries(hotel.hotelFacts.hotelFacilityDetail).map( ).map(([key, value]) => ({ name: key, ...value }))
([key, value]) => ({ name: key, ...value })
)
: []
// Remove Parking facilities since parking accordion is based on hotel.parking // Remove Parking facilities since parking accordion is based on hotel.parking
const simpleAmenities = hotel.detailedFacilities.filter( const simpleAmenities = hotel.detailedFacilities.filter(

View File

@@ -394,8 +394,8 @@ export const getHotelDataSchema = z.object({
hotelFacts: z.object({ hotelFacts: z.object({
checkin: checkinSchema, checkin: checkinSchema,
ecoLabels: ecoLabelsSchema, ecoLabels: ecoLabelsSchema,
hotelFacilityDetail: hotelFacilitySchema.optional(), hotelFacilityDetail: hotelFacilitySchema,
hotelInformation: hotelInformationSchema.optional(), hotelInformation: hotelInformationSchema,
interior: interiorSchema, interior: interiorSchema,
receptionHours: receptionHoursSchema, receptionHours: receptionHoursSchema,
yearBuilt: z.string(), yearBuilt: z.string(),