feat: SW-276 Optimized code removed vanilla implmentation
This commit is contained in:
@@ -22,10 +22,10 @@ export default function AdultSelector({ roomIndex = 0 }: AdultSelectorProps) {
|
|||||||
const adultsLabel = intl.formatMessage({ id: "Adults" })
|
const adultsLabel = intl.formatMessage({ id: "Adults" })
|
||||||
const { setValue } = useFormContext()
|
const { setValue } = useFormContext()
|
||||||
const { adults, children, childrenInAdultsBed } = useGuestsRoomsStore(
|
const { adults, children, childrenInAdultsBed } = useGuestsRoomsStore(
|
||||||
(state) => ({ ...state.rooms[roomIndex] })
|
(state) => state.rooms[roomIndex]
|
||||||
)
|
)
|
||||||
const increaseAdults = useGuestsRoomsStore.use.increaseAdults()
|
const increaseAdults = useGuestsRoomsStore((state) => state.increaseAdults)
|
||||||
const decreaseAdults = useGuestsRoomsStore.use.decreaseAdults()
|
const decreaseAdults = useGuestsRoomsStore((state) => state.decreaseAdults)
|
||||||
|
|
||||||
function increaseAdultsCount(roomIndex: number) {
|
function increaseAdultsCount(roomIndex: number) {
|
||||||
if (adults < 6) {
|
if (adults < 6) {
|
||||||
|
|||||||
@@ -30,13 +30,19 @@ export default function ChildInfoSelector({
|
|||||||
const { adults, childrenInAdultsBed } = useGuestsRoomsStore(
|
const { adults, childrenInAdultsBed } = useGuestsRoomsStore(
|
||||||
(state) => state.rooms[roomIndex]
|
(state) => state.rooms[roomIndex]
|
||||||
)
|
)
|
||||||
const isValidated = useGuestsRoomsStore.use.isValidated()
|
const {
|
||||||
const updateChildAge = useGuestsRoomsStore.use.updateChildAge()
|
isValidated,
|
||||||
const updateChildBed = useGuestsRoomsStore.use.updateChildBed()
|
updateChildAge,
|
||||||
const increaseChildInAdultsBed =
|
updateChildBed,
|
||||||
useGuestsRoomsStore.use.increaseChildInAdultsBed()
|
increaseChildInAdultsBed,
|
||||||
const decreaseChildInAdultsBed =
|
decreaseChildInAdultsBed,
|
||||||
useGuestsRoomsStore.use.decreaseChildInAdultsBed()
|
} = useGuestsRoomsStore((state) => ({
|
||||||
|
isValidated: state.isValidated,
|
||||||
|
updateChildAge: state.updateChildAge,
|
||||||
|
updateChildBed: state.updateChildBed,
|
||||||
|
increaseChildInAdultsBed: state.increaseChildInAdultsBed,
|
||||||
|
decreaseChildInAdultsBed: state.decreaseChildInAdultsBed,
|
||||||
|
}))
|
||||||
|
|
||||||
const ageList = Array.from(Array(13).keys()).map((age) => ({
|
const ageList = Array.from(Array(13).keys()).map((age) => ({
|
||||||
label: `${age}`,
|
label: `${age}`,
|
||||||
|
|||||||
@@ -22,8 +22,12 @@ export default function ChildSelector({ roomIndex = 0 }: ChildSelectorProps) {
|
|||||||
const children = useGuestsRoomsStore(
|
const children = useGuestsRoomsStore(
|
||||||
(state) => state.rooms[roomIndex].children
|
(state) => state.rooms[roomIndex].children
|
||||||
)
|
)
|
||||||
const increaseChildren = useGuestsRoomsStore.use.increaseChildren()
|
const increaseChildren = useGuestsRoomsStore(
|
||||||
const decreaseChildren = useGuestsRoomsStore.use.decreaseChildren()
|
(state) => state.increaseChildren
|
||||||
|
)
|
||||||
|
const decreaseChildren = useGuestsRoomsStore(
|
||||||
|
(state) => state.decreaseChildren
|
||||||
|
)
|
||||||
|
|
||||||
function increaseChildrenCount(roomIndex: number) {
|
function increaseChildrenCount(roomIndex: number) {
|
||||||
if (children.length < 5) {
|
if (children.length < 5) {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { useIntl } from "react-intl"
|
|||||||
|
|
||||||
import { useGuestsRoomsStore } from "@/stores/guests-rooms"
|
import { useGuestsRoomsStore } from "@/stores/guests-rooms"
|
||||||
|
|
||||||
import { CloseLarge, PlusCircleIcon } from "../Icons"
|
import { CloseLargeIcon, PlusCircleIcon } from "../Icons"
|
||||||
import Button from "../TempDesignSystem/Button"
|
import Button from "../TempDesignSystem/Button"
|
||||||
import Divider from "../TempDesignSystem/Divider"
|
import Divider from "../TempDesignSystem/Divider"
|
||||||
import Subtitle from "../TempDesignSystem/Text/Subtitle"
|
import Subtitle from "../TempDesignSystem/Text/Subtitle"
|
||||||
@@ -33,7 +33,7 @@ export default function GuestsRoomsPicker({
|
|||||||
|
|
||||||
const { getFieldState } = useFormContext<BookingWidgetSchema>()
|
const { getFieldState } = useFormContext<BookingWidgetSchema>()
|
||||||
|
|
||||||
const rooms = useGuestsRoomsStore.use.rooms()
|
const rooms = useGuestsRoomsStore((state) => state.rooms)
|
||||||
|
|
||||||
// Not in MVP
|
// Not in MVP
|
||||||
// const increaseRoom = useGuestsRoomsStore.use.increaseRoom()
|
// const increaseRoom = useGuestsRoomsStore.use.increaseRoom()
|
||||||
@@ -43,7 +43,7 @@ export default function GuestsRoomsPicker({
|
|||||||
<div className={styles.pickerContainer}>
|
<div className={styles.pickerContainer}>
|
||||||
<header className={styles.header}>
|
<header className={styles.header}>
|
||||||
<button type="button" className={styles.close} onClick={closePicker}>
|
<button type="button" className={styles.close} onClick={closePicker}>
|
||||||
<CloseLarge />
|
<CloseLargeIcon />
|
||||||
</button>
|
</button>
|
||||||
</header>
|
</header>
|
||||||
<div className={styles.contentContainer}>
|
<div className={styles.contentContainer}>
|
||||||
|
|||||||
@@ -15,10 +15,14 @@ import styles from "./guests-rooms-picker.module.css"
|
|||||||
export default function GuestsRoomsPickerForm() {
|
export default function GuestsRoomsPickerForm() {
|
||||||
const intl = useIntl()
|
const intl = useIntl()
|
||||||
const [isOpen, setIsOpen] = useState(false)
|
const [isOpen, setIsOpen] = useState(false)
|
||||||
const rooms = useGuestsRoomsStore.use.rooms()
|
const { rooms, adultCount, childCount, setIsValidated } = useGuestsRoomsStore(
|
||||||
const adultCount = useGuestsRoomsStore.use.adultCount()
|
(state) => ({
|
||||||
const childCount = useGuestsRoomsStore.use.childCount()
|
rooms: state.rooms,
|
||||||
const setIsValidated = useGuestsRoomsStore.use.setIsValidated()
|
adultCount: state.adultCount,
|
||||||
|
childCount: state.childCount,
|
||||||
|
setIsValidated: state.setIsValidated,
|
||||||
|
})
|
||||||
|
)
|
||||||
const ref = useRef<HTMLDivElement | null>(null)
|
const ref = useRef<HTMLDivElement | null>(null)
|
||||||
function handleOnClick() {
|
function handleOnClick() {
|
||||||
setIsOpen((prevIsOpen) => !prevIsOpen)
|
setIsOpen((prevIsOpen) => !prevIsOpen)
|
||||||
|
|||||||
@@ -322,6 +322,7 @@
|
|||||||
"Zoo": "Djurpark",
|
"Zoo": "Djurpark",
|
||||||
"Zoom in": "Zooma in",
|
"Zoom in": "Zooma in",
|
||||||
"Zoom out": "Zooma ut",
|
"Zoom out": "Zooma ut",
|
||||||
|
"as of today": "från och med idag",
|
||||||
"booking.adults": "{totalAdults, plural, one {# vuxen} other {# vuxna}}",
|
"booking.adults": "{totalAdults, plural, one {# vuxen} other {# vuxna}}",
|
||||||
"booking.children": "{totalChildren, plural, one {# barn} other {# barn}}",
|
"booking.children": "{totalChildren, plural, one {# barn} other {# barn}}",
|
||||||
"booking.guests": "Max {nrOfGuests, plural, one {# gäst} other {# gäster}}",
|
"booking.guests": "Max {nrOfGuests, plural, one {# gäst} other {# gäster}}",
|
||||||
@@ -347,6 +348,7 @@
|
|||||||
"special character": "speciell karaktär",
|
"special character": "speciell karaktär",
|
||||||
"spendable points expiring by": "{points} poäng förfaller {date}",
|
"spendable points expiring by": "{points} poäng förfaller {date}",
|
||||||
"to": "till",
|
"to": "till",
|
||||||
|
"uppercase letter": "stor bokstav",
|
||||||
"{amount} {currency}": "{amount} {currency}",
|
"{amount} {currency}": "{amount} {currency}",
|
||||||
"{difference}{amount} {currency}": "{difference}{amount} {currency}",
|
"{difference}{amount} {currency}": "{difference}{amount} {currency}",
|
||||||
"{width} cm × {length} cm": "{width} cm × {length} cm"
|
"{width} cm × {length} cm": "{width} cm × {length} cm"
|
||||||
|
|||||||
@@ -3,8 +3,6 @@
|
|||||||
import { produce } from "immer"
|
import { produce } from "immer"
|
||||||
import { create } from "zustand"
|
import { create } from "zustand"
|
||||||
|
|
||||||
import { createSelectors } from "./utils"
|
|
||||||
|
|
||||||
import { BedTypeEnum } from "@/types/components/bookingWidget/enums"
|
import { BedTypeEnum } from "@/types/components/bookingWidget/enums"
|
||||||
import { Child } from "@/types/components/bookingWidget/guestsRoomsPicker"
|
import { Child } from "@/types/components/bookingWidget/guestsRoomsPicker"
|
||||||
|
|
||||||
@@ -32,7 +30,7 @@ interface GuestsRooms {
|
|||||||
setIsValidated: (isValidated: boolean) => void
|
setIsValidated: (isValidated: boolean) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useGuestsRoomsStoreBase = create<GuestsRooms>((set, get) => ({
|
export const useGuestsRoomsStore = create<GuestsRooms>((set, get) => ({
|
||||||
rooms: [
|
rooms: [
|
||||||
{
|
{
|
||||||
adults: 1,
|
adults: 1,
|
||||||
@@ -144,5 +142,3 @@ export const useGuestsRoomsStoreBase = create<GuestsRooms>((set, get) => ({
|
|||||||
),
|
),
|
||||||
setIsValidated: (isValidated) => set(() => ({ isValidated })),
|
setIsValidated: (isValidated) => set(() => ({ isValidated })),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
export const useGuestsRoomsStore = createSelectors(useGuestsRoomsStoreBase)
|
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
import { StoreApi, UseBoundStore } from "zustand"
|
|
||||||
|
|
||||||
type WithSelectors<S> = S extends { getState: () => infer T }
|
|
||||||
? S & { use: { [K in keyof T]: () => T[K] } }
|
|
||||||
: never
|
|
||||||
|
|
||||||
export const createSelectors = <S extends UseBoundStore<StoreApi<object>>>(
|
|
||||||
_store: S
|
|
||||||
) => {
|
|
||||||
let store = _store as WithSelectors<typeof _store>
|
|
||||||
store.use = {}
|
|
||||||
for (let k of Object.keys(store.getState())) {
|
|
||||||
;(store.use as any)[k] = () => store((s) => s[k as keyof typeof s])
|
|
||||||
}
|
|
||||||
|
|
||||||
return store
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user