Merged in feat/SW-1077-enter-details-edit-room (pull request #1360)
Feat/SW-1077 enter details edit room * feat(SW-1077): persist state when changing rooms * fix: issue with step state when closing accordion and transition to correct room when modifying step Approved-by: Pontus Dreij
This commit is contained in:
@@ -3,7 +3,8 @@ import { useEffect, useRef } from "react"
|
||||
|
||||
import { createDetailsStore } from "@/stores/enter-details"
|
||||
import {
|
||||
checkIsSameRoom,
|
||||
checkIsSameBedTypes,
|
||||
checkIsSameBooking as checkIsSameBooking,
|
||||
clearSessionStorage,
|
||||
readFromSessionStorage,
|
||||
} from "@/stores/enter-details/helpers"
|
||||
@@ -11,6 +12,7 @@ import {
|
||||
import { DetailsContext } from "@/contexts/Details"
|
||||
|
||||
import type { DetailsStore } from "@/types/contexts/enter-details"
|
||||
import { StepEnum } from "@/types/enums/step"
|
||||
import type { DetailsProviderProps } from "@/types/providers/enter-details"
|
||||
import type { InitialState } from "@/types/stores/enter-details"
|
||||
|
||||
@@ -58,19 +60,73 @@ export default function EnterDetailsProvider({
|
||||
if (!storedValues) {
|
||||
return
|
||||
}
|
||||
const isSameRoom = checkIsSameRoom(storedValues.booking, booking)
|
||||
if (!isSameRoom) {
|
||||
const isSameBooking = checkIsSameBooking(storedValues.booking, booking)
|
||||
if (!isSameBooking) {
|
||||
clearSessionStorage()
|
||||
return
|
||||
}
|
||||
|
||||
const state = storeRef.current?.getState()
|
||||
storeRef.current?.setState({
|
||||
...state,
|
||||
rooms: storedValues.rooms,
|
||||
bookingProgress: storedValues.bookingProgress,
|
||||
const updatedRooms = storedValues.rooms.map((storedRoom, idx) => {
|
||||
const currentRoom = booking.rooms[idx]
|
||||
const roomData = roomsData[idx]
|
||||
|
||||
if (!storedRoom.bedType) {
|
||||
return storedRoom
|
||||
}
|
||||
|
||||
const isSameBedTypes = checkIsSameBedTypes(
|
||||
storedRoom.bedType.roomTypeCode,
|
||||
currentRoom.roomTypeCode
|
||||
)
|
||||
if (isSameBedTypes) {
|
||||
return storedRoom
|
||||
}
|
||||
|
||||
if (roomData?.bedTypes?.length === 1 && roomData.bedTypes[0]) {
|
||||
return {
|
||||
...storedRoom,
|
||||
bedType: {
|
||||
roomTypeCode: roomData.bedTypes[0].value,
|
||||
description: roomData.bedTypes[0].description,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Remove bed type selection if bedtypes change
|
||||
return {
|
||||
...storedRoom,
|
||||
bedType: undefined,
|
||||
}
|
||||
})
|
||||
}, [booking])
|
||||
|
||||
const updatedProgress = {
|
||||
...storedValues.bookingProgress,
|
||||
roomStatuses: storedValues.bookingProgress.roomStatuses.map(
|
||||
(status, idx) => {
|
||||
const hasValidBedType = Boolean(updatedRooms[idx].bedType)
|
||||
if (hasValidBedType) return status
|
||||
|
||||
return {
|
||||
...status,
|
||||
steps: {
|
||||
...status.steps,
|
||||
[StepEnum.selectBed]: {
|
||||
step: StepEnum.selectBed,
|
||||
isValid: false,
|
||||
},
|
||||
},
|
||||
currentStep: StepEnum.selectBed,
|
||||
isComplete: false,
|
||||
}
|
||||
}
|
||||
),
|
||||
}
|
||||
|
||||
storeRef.current?.setState({
|
||||
rooms: updatedRooms,
|
||||
bookingProgress: updatedProgress,
|
||||
})
|
||||
}, [booking, roomsData])
|
||||
|
||||
return (
|
||||
<DetailsContext.Provider value={storeRef.current}>
|
||||
|
||||
Reference in New Issue
Block a user