From 2c6ef3fea7a320f40526a4086e14ce35a8852ca9 Mon Sep 17 00:00:00 2001 From: Hrishikesh Vaipurkar Date: Wed, 9 Oct 2024 12:24:50 +0200 Subject: [PATCH] feat: SW-276 Optimized code --- .../GuestsRoomsPicker/AdultSelector/index.tsx | 24 +++++++-------- .../ChildSelector/ChildInfoSelector.tsx | 2 +- stores/guests-rooms.ts | 30 +++++++++---------- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/components/GuestsRoomsPicker/AdultSelector/index.tsx b/components/GuestsRoomsPicker/AdultSelector/index.tsx index acac7bf92..ef00e9332 100644 --- a/components/GuestsRoomsPicker/AdultSelector/index.tsx +++ b/components/GuestsRoomsPicker/AdultSelector/index.tsx @@ -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 + ) + } } } } diff --git a/components/GuestsRoomsPicker/ChildSelector/ChildInfoSelector.tsx b/components/GuestsRoomsPicker/ChildSelector/ChildInfoSelector.tsx index 289d5761c..bf64e7dd7 100644 --- a/components/GuestsRoomsPicker/ChildSelector/ChildInfoSelector.tsx +++ b/components/GuestsRoomsPicker/ChildSelector/ChildInfoSelector.tsx @@ -62,7 +62,7 @@ export default function ChildInfoSelector({ } function updateSelectedBed(bed: number) { - if (bed == BedTypeEnum["In adults bed"] && childrenInAdultsBed < adults) { + if (bed == BedTypeEnum["In adults bed"]) { increaseChildInAdultsBed(roomIndex) } else if (child.bed == BedTypeEnum["In adults bed"]) { decreaseChildInAdultsBed(roomIndex) diff --git a/stores/guests-rooms.ts b/stores/guests-rooms.ts index 403cbd73c..b5062b923 100644 --- a/stores/guests-rooms.ts +++ b/stores/guests-rooms.ts @@ -53,22 +53,22 @@ export const guestsRoomsStore = create((set, get) => ({ produce((state: GuestsRooms) => { state.rooms[roomIndex].adults = state.rooms[roomIndex].adults - 1 state.adultCount = state.adultCount - 1 - let childrenInAdultsBed = 0 - state.rooms[roomIndex].children = state.rooms[roomIndex].children.map( - (child) => { - if (child.bed == BedTypeEnum["In adults bed"]) { - childrenInAdultsBed = childrenInAdultsBed + 1 - if (childrenInAdultsBed > state.rooms[roomIndex].adults) - child.bed = - child.age < 3 - ? BedTypeEnum["In crib"] - : BedTypeEnum["In extra bed"] - } - return child - } - ) - state.rooms[roomIndex].childrenInAdultsBed = + if ( + state.rooms[roomIndex].childrenInAdultsBed > state.rooms[roomIndex].adults + ) { + const toUpdateIndex = state.rooms[roomIndex].children.findIndex( + (child) => child.bed == BedTypeEnum["In adults bed"] + ) + if (toUpdateIndex != -1) { + state.rooms[roomIndex].children[toUpdateIndex].bed = + state.rooms[roomIndex].children[toUpdateIndex].age < 3 + ? BedTypeEnum["In crib"] + : BedTypeEnum["In extra bed"] + state.rooms[roomIndex].childrenInAdultsBed = + state.rooms[roomIndex].adults + } + } }) ), increaseChildren: (roomIndex) =>