Merged in feat/SW-1889 (pull request #1670)
Feat/SW-1889 * fix: remove download invoice from confirmation page * feat: remove EnterDetails Accordions Approved-by: Simon.Emanuelsson
This commit is contained in:
@@ -7,11 +7,7 @@ import type { Price } from "@/types/components/hotelReservation/price"
|
||||
import type { SelectRateSearchParams } from "@/types/components/hotelReservation/selectRate/selectRate"
|
||||
import { CurrencyEnum } from "@/types/enums/currency"
|
||||
import { StepEnum } from "@/types/enums/step"
|
||||
import type {
|
||||
DetailsState,
|
||||
PersistedState,
|
||||
RoomState,
|
||||
} from "@/types/stores/enter-details"
|
||||
import type { PersistedState, RoomState } from "@/types/stores/enter-details"
|
||||
import type { SafeUser } from "@/types/user"
|
||||
|
||||
export function extractGuestFromUser(user: NonNullable<SafeUser>) {
|
||||
@@ -513,54 +509,12 @@ export function findNextInvalidStep(roomState: RoomState) {
|
||||
)
|
||||
}
|
||||
|
||||
export const selectNextStep = (room: RoomState) => {
|
||||
if (room.currentStep === null) {
|
||||
throw new Error("getNextStep: currentStep is null")
|
||||
}
|
||||
|
||||
if (!room.steps[room.currentStep]?.isValid) {
|
||||
return room.currentStep
|
||||
}
|
||||
|
||||
const stepsArray = Object.values(room.steps)
|
||||
const currentIndex = stepsArray.findIndex(
|
||||
(step) => step?.step === room.currentStep
|
||||
)
|
||||
if (currentIndex === stepsArray.length - 1) {
|
||||
return null
|
||||
}
|
||||
|
||||
const nextInvalidStep = stepsArray
|
||||
.slice(currentIndex + 1)
|
||||
.find((step) => !step.isValid)
|
||||
|
||||
return nextInvalidStep?.step ?? null
|
||||
}
|
||||
|
||||
export const checkRoomProgress = (steps: RoomState["steps"]) => {
|
||||
return Object.values(steps)
|
||||
.filter(Boolean)
|
||||
.every((step) => step.isValid)
|
||||
}
|
||||
|
||||
export function handleStepProgression(room: RoomState, state: DetailsState) {
|
||||
const isAllRoomsCompleted = state.rooms.every((r) => r.isComplete)
|
||||
if (isAllRoomsCompleted) {
|
||||
room.currentStep = null
|
||||
state.canProceedToPayment = true
|
||||
} else if (room.isComplete) {
|
||||
room.currentStep = null
|
||||
const nextRoomIndex = state.rooms.findIndex((r) => !r.isComplete)
|
||||
state.activeRoom = nextRoomIndex
|
||||
|
||||
const nextRoom = state.rooms[nextRoomIndex]
|
||||
const nextStep = selectNextStep(nextRoom)
|
||||
nextRoom.currentStep = nextStep
|
||||
} else if (selectNextStep(room)) {
|
||||
room.currentStep = selectNextStep(room)
|
||||
}
|
||||
}
|
||||
|
||||
export function readFromSessionStorage(): PersistedState | undefined {
|
||||
if (typeof window === "undefined") {
|
||||
return undefined
|
||||
|
||||
@@ -15,10 +15,8 @@ import {
|
||||
calculateVoucherPrice,
|
||||
checkRoomProgress,
|
||||
extractGuestFromUser,
|
||||
findNextInvalidStep,
|
||||
getRoomPrice,
|
||||
getTotalPrice,
|
||||
handleStepProgression,
|
||||
writeToSessionStorage,
|
||||
} from "./helpers"
|
||||
|
||||
@@ -114,7 +112,6 @@ export function createDetailsStore(
|
||||
})
|
||||
|
||||
return create<DetailsState>()((set) => ({
|
||||
activeRoom: 0,
|
||||
booking: initialState.booking,
|
||||
breakfastPackages,
|
||||
canProceedToPayment: false,
|
||||
@@ -141,72 +138,8 @@ export function createDetailsStore(
|
||||
delete steps[StepEnum.breakfast]
|
||||
}
|
||||
|
||||
const currentStep =
|
||||
Object.values(steps).find((step) => !step.isValid)?.step ??
|
||||
StepEnum.selectBed
|
||||
|
||||
return {
|
||||
actions: {
|
||||
setStep(step) {
|
||||
return set(
|
||||
produce((state: DetailsState) => {
|
||||
const isSameRoom = idx === state.activeRoom
|
||||
const room = state.rooms[idx]
|
||||
if (isSameRoom) {
|
||||
// Closed same accordion as was open
|
||||
if (step === room.currentStep) {
|
||||
if (room.isComplete) {
|
||||
// Room is complete, move to next room or payment
|
||||
const nextRoomIdx = state.rooms.findIndex(
|
||||
(r) => !r.isComplete
|
||||
)
|
||||
state.activeRoom = nextRoomIdx
|
||||
// Done, proceed to payment
|
||||
if (nextRoomIdx === -1) {
|
||||
room.currentStep = null
|
||||
} else {
|
||||
const nextRoom = state.rooms[nextRoomIdx]
|
||||
const nextInvalidStep = findNextInvalidStep(nextRoom)
|
||||
nextRoom.currentStep = nextInvalidStep
|
||||
}
|
||||
} else {
|
||||
room.currentStep = findNextInvalidStep(room)
|
||||
}
|
||||
} else {
|
||||
if (room.steps[step]?.isValid) {
|
||||
room.currentStep = step
|
||||
} else {
|
||||
room.currentStep = findNextInvalidStep(room)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const arePreviousRoomsCompleted = state.rooms
|
||||
.slice(0, idx)
|
||||
.every((room) => room.isComplete)
|
||||
if (arePreviousRoomsCompleted) {
|
||||
state.activeRoom = idx
|
||||
if (room.steps[step]?.isValid) {
|
||||
room.currentStep = step
|
||||
} else {
|
||||
room.currentStep = findNextInvalidStep(room)
|
||||
}
|
||||
} else {
|
||||
const firstIncompleteRoom = state.rooms.findIndex(
|
||||
(r) => !r.isComplete
|
||||
)
|
||||
state.activeRoom = firstIncompleteRoom
|
||||
if (firstIncompleteRoom === -1) {
|
||||
// All rooms are done, proceed to payment
|
||||
room.currentStep = null
|
||||
} else {
|
||||
const nextRoom = state.rooms[firstIncompleteRoom]
|
||||
nextRoom.currentStep = findNextInvalidStep(nextRoom)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
},
|
||||
updateBedType(bedType) {
|
||||
return set(
|
||||
produce((state: DetailsState) => {
|
||||
@@ -220,10 +153,7 @@ export function createDetailsStore(
|
||||
state.rooms[idx].isComplete = true
|
||||
}
|
||||
|
||||
handleStepProgression(state.rooms[idx], state)
|
||||
|
||||
writeToSessionStorage({
|
||||
activeRoom: state.activeRoom,
|
||||
booking: state.booking,
|
||||
rooms: state.rooms,
|
||||
})
|
||||
@@ -340,10 +270,7 @@ export function createDetailsStore(
|
||||
state.rooms[idx].isComplete = true
|
||||
}
|
||||
|
||||
handleStepProgression(currentRoom, state)
|
||||
|
||||
writeToSessionStorage({
|
||||
activeRoom: state.activeRoom,
|
||||
booking: state.booking,
|
||||
rooms: state.rooms,
|
||||
})
|
||||
@@ -408,10 +335,7 @@ export function createDetailsStore(
|
||||
state.rooms[idx].isComplete = true
|
||||
}
|
||||
|
||||
handleStepProgression(state.rooms[idx], state)
|
||||
|
||||
writeToSessionStorage({
|
||||
activeRoom: state.activeRoom,
|
||||
booking: state.booking,
|
||||
rooms: state.rooms,
|
||||
})
|
||||
@@ -461,10 +385,7 @@ export function createDetailsStore(
|
||||
state.rooms[idx].isComplete = true
|
||||
}
|
||||
|
||||
handleStepProgression(state.rooms[idx], state)
|
||||
|
||||
writeToSessionStorage({
|
||||
activeRoom: state.activeRoom,
|
||||
booking: state.booking,
|
||||
rooms: state.rooms,
|
||||
})
|
||||
@@ -490,8 +411,6 @@ export function createDetailsStore(
|
||||
comment: "",
|
||||
},
|
||||
},
|
||||
|
||||
currentStep,
|
||||
isComplete: false,
|
||||
steps,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user