feat(SW-2863): Move contentstack router to trpc package * Add exports to packages and lint rule to prevent relative imports * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package Approved-by: Linus Flood
93 lines
2.6 KiB
TypeScript
93 lines
2.6 KiB
TypeScript
"use client"
|
|
|
|
import { RoomPackageCodeEnum } from "@scandic-hotels/trpc/enums/roomFilter"
|
|
import { AvailabilityEnum } from "@scandic-hotels/trpc/enums/selectHotel"
|
|
import { sortRoomConfigs } from "@scandic-hotels/trpc/utils/sortRoomConfigs"
|
|
|
|
import { useRatesStore } from "@/stores/select-rate"
|
|
|
|
import { RoomContext } from "@/contexts/SelectRate/Room"
|
|
|
|
import type { RoomProviderProps } from "@/types/providers/select-rate/room"
|
|
|
|
export default function RoomProvider({
|
|
children,
|
|
idx,
|
|
room,
|
|
}: RoomProviderProps) {
|
|
const { activeRoom, rateSummary, roomAvailability, roomPackages } =
|
|
useRatesStore((state) => ({
|
|
activeRoom: state.activeRoom,
|
|
rateSummary: state.rateSummary,
|
|
roomPackages: state.roomsPackages[idx],
|
|
roomAvailability: state.roomsAvailability?.[idx],
|
|
}))
|
|
const roomNr = idx + 1
|
|
|
|
const petRoomPackage = roomPackages.find(
|
|
(pkg) => pkg.code === RoomPackageCodeEnum.PET_ROOM
|
|
)
|
|
|
|
const selectedRoomsCount = rateSummary.reduce<Record<string, number>>(
|
|
(roomsCount, selectedRoom) => {
|
|
if (selectedRoom) {
|
|
if (!roomsCount[selectedRoom.roomTypeCode]) {
|
|
roomsCount[selectedRoom.roomTypeCode] = 0
|
|
}
|
|
roomsCount[selectedRoom.roomTypeCode] =
|
|
roomsCount[selectedRoom.roomTypeCode] + 1
|
|
}
|
|
return roomsCount
|
|
},
|
|
{}
|
|
)
|
|
|
|
const rooms = room.rooms
|
|
.map((r) => {
|
|
// Need to copy (shallow) since using immer with
|
|
// Zustand uses Proxy objects which results in
|
|
// properties being read-only thus causing errors
|
|
// when trying to modify roomsLeft etc.
|
|
const rCopy = { ...r }
|
|
if (selectedRoomsCount[r.roomTypeCode]) {
|
|
rCopy.roomsLeft =
|
|
rCopy.roomsLeft - selectedRoomsCount[rCopy.roomTypeCode]
|
|
|
|
const selectedRoom = rateSummary[idx]
|
|
const isCurrentlySelectedRoom =
|
|
selectedRoom && selectedRoom.roomTypeCode === rCopy.roomTypeCode
|
|
if (isCurrentlySelectedRoom) {
|
|
// Need to add 1 roomsLeft back to tally since
|
|
// our selectedRoom was included in selectedRoomsCount
|
|
rCopy.roomsLeft = rCopy.roomsLeft + 1
|
|
}
|
|
|
|
if (rCopy.roomsLeft <= 0) {
|
|
rCopy.status = AvailabilityEnum.NotAvailable
|
|
rCopy.roomsLeft = 0
|
|
}
|
|
}
|
|
|
|
return rCopy
|
|
})
|
|
.sort(sortRoomConfigs)
|
|
|
|
return (
|
|
<RoomContext.Provider
|
|
value={{
|
|
...room,
|
|
isActiveRoom: activeRoom === idx,
|
|
isMainRoom: roomNr === 1,
|
|
petRoomPackage,
|
|
roomAvailability,
|
|
roomPackages,
|
|
roomNr,
|
|
rooms,
|
|
totalRooms: room.rooms.length,
|
|
}}
|
|
>
|
|
{children}
|
|
</RoomContext.Provider>
|
|
)
|
|
}
|