11"use client" ;
22
33import { useState , useCallback , memo , useMemo } from "react" ;
4+ import { useQueryClient } from "@tanstack/react-query" ;
45import { cn } from "@/shared/utils/cn" ;
56import { SectionButtons } from "@/entities/booking/ui/SectionButtons" ;
6- import { SectionType , Section , SECTIONS , SEAT_INFO } from "@/entities/booking/model/types" ;
7+ import { SectionType , Section , Seat , SECTIONS , SEAT_INFO , SEAT_STATUS } from "@/entities/booking/model/types" ;
8+ import { usePrefetchAllSeats } from "@/widgets/booking/lib/useSeatState" ;
79
810interface SelectSectionProps {
911 onSectionSelect ?: ( section : SectionType ) => void ;
@@ -12,6 +14,9 @@ interface SelectSectionProps {
1214
1315export const SelectSection = memo < SelectSectionProps > ( ( { onSectionSelect, className } ) => {
1416 const [ selectedSection , setSelectedSection ] = useState < SectionType > ( null ) ;
17+ const queryClient = useQueryClient ( ) ;
18+
19+ const { isLoading : isPrefetching } = usePrefetchAllSeats ( ) ;
1520
1621 const handleSectionSelect = useCallback (
1722 ( section : SectionType ) => {
@@ -23,12 +28,31 @@ export const SelectSection = memo<SelectSectionProps>(({ onSectionSelect, classN
2328
2429 const seatInfoMap = useMemo ( ( ) => {
2530 const map : Record < Section , string > = { } as Record < Section , string > ;
31+
32+ if ( isPrefetching ) {
33+ SECTIONS . forEach ( section => {
34+ const seatInfo = SEAT_INFO [ section ] ;
35+ map [ section ] = `${ seatInfo . occupied } /${ seatInfo . total } ` ;
36+ } ) ;
37+ return map ;
38+ }
39+
2640 SECTIONS . forEach ( section => {
27- const seatInfo = SEAT_INFO [ section ] ;
28- map [ section ] = `${ seatInfo . occupied } /${ seatInfo . total } ` ;
41+ const total = SEAT_INFO [ section ] . total ;
42+
43+ const cachedSeats = queryClient . getQueryData < Seat [ ] > ( [ "seatState" , section ] ) ;
44+
45+ if ( cachedSeats ) {
46+ const occupied = cachedSeats . filter ( seat => seat . status === SEAT_STATUS . UNAVAILABLE ) . length ;
47+ map [ section ] = `${ occupied } /${ total } ` ;
48+ } else {
49+ const seatInfo = SEAT_INFO [ section ] ;
50+ map [ section ] = `${ seatInfo . occupied } /${ seatInfo . total } ` ;
51+ }
2952 } ) ;
53+
3054 return map ;
31- } , [ ] ) ;
55+ } , [ isPrefetching , queryClient ] ) ;
3256
3357 return (
3458 < div className = { cn ( "w-full" , className ) } >
0 commit comments