feat: SW-276 Optimized code

This commit is contained in:
Hrishikesh Vaipurkar
2024-10-09 12:24:50 +02:00
parent ad42440817
commit 2c6ef3fea7
3 changed files with 27 additions and 29 deletions

View File

@@ -19,7 +19,8 @@ export default function AdultSelector({ roomIndex = 0 }: AdultSelectorProps) {
const intl = useIntl()
const adultsLabel = intl.formatMessage({ id: "Adults" })
const { setValue } = useFormContext()
const { adults, children } = guestsRoomsStore().rooms[roomIndex]
const { adults, children, childrenInAdultsBed } =
guestsRoomsStore().rooms[roomIndex]
const { increaseAdults, decreaseAdults } = guestsRoomsStore()
function increaseAdultsCount(roomIndex: number) {
@@ -33,19 +34,16 @@ export default function AdultSelector({ roomIndex = 0 }: AdultSelectorProps) {
if (adults > 1) {
decreaseAdults(roomIndex)
setValue(`rooms.${roomIndex}.adults`, adults - 1)
let inAdultsBed = 0
let toUpdateIndex = -1
children.forEach((child, index) => {
if (child.bed == BedTypeEnum["In adults bed"]) {
inAdultsBed = inAdultsBed + 1
if (inAdultsBed > adults - 1) toUpdateIndex = index
}
})
if (toUpdateIndex != -1) {
setValue(
`rooms.${roomIndex}.children.${toUpdateIndex}.bed`,
children[toUpdateIndex].age < 3 ? 1 : 2
if (childrenInAdultsBed > adults) {
const toUpdateIndex = children.findIndex(
(child, index) => child.bed == BedTypeEnum["In adults bed"]
)
if (toUpdateIndex != -1) {
setValue(
`rooms.${roomIndex}.children.${toUpdateIndex}.bed`,
children[toUpdateIndex].age < 3 ? 1 : 2
)
}
}
}
}