feat: SW-276 Optimized code

This commit is contained in:
Hrishikesh Vaipurkar
2024-10-10 15:00:53 +02:00
parent f1f1434c9d
commit d52a347904
4 changed files with 16 additions and 11 deletions

View File

@@ -26,7 +26,7 @@ export default function ChildInfoSelector({
const ageLabel = intl.formatMessage({ id: "Age" })
const ageReqdErrMsg = intl.formatMessage({ id: "Child age is required" })
const bedLabel = intl.formatMessage({ id: "Bed" })
const { setValue } = useFormContext()
const { setValue, trigger } = useFormContext()
const { adults, childrenInAdultsBed } = useGuestsRoomsStore(
(state) => state.rooms[roomIndex]
)
@@ -45,9 +45,7 @@ export default function ChildInfoSelector({
function updateSelectedAge(age: number) {
updateChildAge(age, roomIndex, index)
setValue(`rooms.${roomIndex}.children.${index}.age`, age, {
shouldTouch: true,
})
setValue(`rooms.${roomIndex}.children.${index}.age`, age)
const availableBedTypes = getAvailableBeds(age)
updateSelectedBed(availableBedTypes[0].value)
}
@@ -60,6 +58,7 @@ export default function ChildInfoSelector({
}
updateChildBed(bed, roomIndex, index)
setValue(`rooms.${roomIndex}.children.${index}.bed`, bed)
trigger()
}
const allBedTypes: ChildBed[] = [

View File

@@ -14,12 +14,13 @@ import ChildInfoSelector from "./ChildInfoSelector"
import styles from "./child-selector.module.css"
import { BookingWidgetSchema } from "@/types/components/bookingWidget"
import { ChildSelectorProps } from "@/types/components/bookingWidget/guestsRoomsPicker"
export default function ChildSelector({ roomIndex = 0 }: ChildSelectorProps) {
const intl = useIntl()
const childrenLabel = intl.formatMessage({ id: "Children" })
const { setValue } = useFormContext()
const { setValue, trigger } = useFormContext<BookingWidgetSchema>()
const { children } = useGuestsRoomsStore((state) => state.rooms[roomIndex])
const { increaseChildren, decreaseChildren } = useGuestsRoomsStore()
@@ -30,6 +31,7 @@ export default function ChildSelector({ roomIndex = 0 }: ChildSelectorProps) {
age: -1,
bed: -1,
})
trigger()
}
}
function decreaseChildrenCount(roomIndex: number) {
@@ -38,6 +40,7 @@ export default function ChildSelector({ roomIndex = 0 }: ChildSelectorProps) {
let newChildrenList = JSON.parse(JSON.stringify(children))
newChildrenList.pop()
setValue(`rooms.${roomIndex}.children`, newChildrenList)
trigger()
}
}