|
| 1 | +import { useParams } from 'react-router-dom'; |
| 2 | + |
| 3 | +import { BASE_URL } from '@/api/axios.ts'; |
| 4 | +import type { PostSeatData } from '@/api/booking.ts'; |
| 5 | +import { postSeat } from '@/api/booking.ts'; |
| 6 | + |
| 7 | +import useSSE from '@/hooks/useSSE.tsx'; |
| 8 | + |
| 9 | +import { toast } from '@/components/Toast/index.ts'; |
| 10 | +import Icon from '@/components/common/Icon.tsx'; |
| 11 | + |
| 12 | +import type { SelectedSeat } from '@/pages/ReservationPage/SectionAndSeat.tsx'; |
| 13 | + |
| 14 | +import { API } from '@/constants/index.ts'; |
| 15 | +import type { Section } from '@/type/index.ts'; |
| 16 | +import { useMutation, useMutationState } from '@tanstack/react-query'; |
| 17 | + |
| 18 | +interface SeatMapProps { |
| 19 | + selectedSection: Section; |
| 20 | + selectedSectionIndex: number; |
| 21 | + setSelectedSeats: (seats: SelectedSeat[]) => void; |
| 22 | + maxSelectCount: number; |
| 23 | + selectedSeats: SelectedSeat[]; |
| 24 | +} |
| 25 | + |
| 26 | +export default function SeatMap({ |
| 27 | + selectedSection, |
| 28 | + selectedSectionIndex, |
| 29 | + setSelectedSeats, |
| 30 | + maxSelectCount, |
| 31 | + selectedSeats, |
| 32 | +}: SeatMapProps) { |
| 33 | + const { eventId } = useParams(); |
| 34 | + const { mutate: pickSeat } = useMutation({ |
| 35 | + mutationFn: postSeat, |
| 36 | + mutationKey: PICK_SEAT_MUTATION_KEY_LIST, |
| 37 | + onError: (_, data) => { |
| 38 | + const { seatIndex, sectionIndex } = data; |
| 39 | + const filtered = selectedSeats.filter( |
| 40 | + (seat) => seat.seatIndex !== seatIndex || seat.sectionIndex !== sectionIndex, |
| 41 | + ); |
| 42 | + setSelectedSeats([...filtered]); |
| 43 | + toast.error('좌석 선택/취소에 실패했습니다'); |
| 44 | + }, |
| 45 | + throwOnError: false, |
| 46 | + }); |
| 47 | + |
| 48 | + const reservingList = useMutationState<PostSeatData>({ |
| 49 | + filters: { |
| 50 | + mutationKey: PICK_SEAT_MUTATION_KEY_LIST, |
| 51 | + status: 'pending', |
| 52 | + predicate: (mutation) => { |
| 53 | + return mutation.state.variables.expectedStatus === 'reserved'; |
| 54 | + }, |
| 55 | + }, |
| 56 | + select: (mutation) => mutation.state.variables as PostSeatData, |
| 57 | + }); |
| 58 | + const { data, isLoading } = useSSE<{ seatStatus: boolean[][] }>({ |
| 59 | + sseURL: `${BASE_URL}${API.BOOKING.GET_SEATS_SSE(Number(eventId))}`, |
| 60 | + }); |
| 61 | + |
| 62 | + const seatStatusList = data ? data.seatStatus : null; |
| 63 | + const selectedSeatStatus = seatStatusList ? seatStatusList[selectedSectionIndex] : null; |
| 64 | + const canRender = isLoading === false && seatStatusList && seatStatusList.length !== 0; |
| 65 | + |
| 66 | + return ( |
| 67 | + <> |
| 68 | + {canRender ? ( |
| 69 | + renderSeatMap( |
| 70 | + selectedSection, |
| 71 | + selectedSectionIndex, |
| 72 | + selectedSeatStatus!, |
| 73 | + setSelectedSeats, |
| 74 | + maxSelectCount, |
| 75 | + selectedSeats, |
| 76 | + pickSeat, |
| 77 | + Number(eventId!), |
| 78 | + reservingList, |
| 79 | + ) |
| 80 | + ) : ( |
| 81 | + <Loading /> |
| 82 | + )} |
| 83 | + </> |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +const Loading = () => { |
| 88 | + return ( |
| 89 | + <div className="absolute flex w-full items-center justify-center"> |
| 90 | + <div className="flex flex-col items-center gap-4"> |
| 91 | + <Icon iconName="Loading" className="h-16 w-16 animate-spin" color={'primary'} /> |
| 92 | + <span className="text-heading3 text-typo">loading..</span> |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + ); |
| 96 | +}; |
| 97 | + |
| 98 | +const renderSeatMap = ( |
| 99 | + selectedSection: Section, |
| 100 | + selectedSectionIndex: number, |
| 101 | + seatStatus: boolean[], |
| 102 | + setSelectedSeats: (seats: SelectedSeat[]) => void, |
| 103 | + maxSelectCount: number, |
| 104 | + selectedSeats: SelectedSeat[], |
| 105 | + pickSeat: ( |
| 106 | + data: PostSeatData, |
| 107 | + mutateOption?: { |
| 108 | + onSuccess?: () => void; |
| 109 | + onError?: () => void; |
| 110 | + }, |
| 111 | + ) => void, |
| 112 | + eventId: number, |
| 113 | + reservingList: PostSeatData[], |
| 114 | +) => { |
| 115 | + let columnCount = 1; |
| 116 | + const { name, seats, colLen } = selectedSection; |
| 117 | + |
| 118 | + return seats.map((seat, index) => { |
| 119 | + const rowsCount = Math.floor(index / colLen) + 1; |
| 120 | + const isNewLine = index % colLen === 0; |
| 121 | + if (isNewLine) columnCount = 1; |
| 122 | + const seatName = seat ? `${name}구역 ${rowsCount}행 ${columnCount}열` : null; |
| 123 | + const isMine = seatName && selectedSeats.some((selected) => selected.name == seatName); |
| 124 | + |
| 125 | + const isReserving = reservingList.some( |
| 126 | + (reserve) => reserve.seatIndex === index && reserve.sectionIndex === selectedSectionIndex, |
| 127 | + ); |
| 128 | + const isOthers = !seatStatus[index]; |
| 129 | + //TODO 삼항 연산자 제거 |
| 130 | + const stateClass = !seat |
| 131 | + ? 'bg-transparent pointer-events-none' |
| 132 | + : isReserving |
| 133 | + ? 'bg-warning pointer-events-none' |
| 134 | + : isMine |
| 135 | + ? 'bg-success cursor-pointer' |
| 136 | + : isOthers |
| 137 | + ? `bg-surface-sub pointer-events-none` |
| 138 | + : 'bg-primary cursor-pointer'; |
| 139 | + if (seat) columnCount++; |
| 140 | + return ( |
| 141 | + <div |
| 142 | + className={`h-6 w-6 ${stateClass}`} |
| 143 | + data-name={seatName} |
| 144 | + onClick={() => { |
| 145 | + const selectedCount = selectedSeats.length; |
| 146 | + if (isMine) { |
| 147 | + const filtered = selectedSeats.filter((seat) => seatName !== seat.name); |
| 148 | + pickSeat( |
| 149 | + { |
| 150 | + sectionIndex: selectedSectionIndex, |
| 151 | + seatIndex: index, |
| 152 | + expectedStatus: 'deleted', |
| 153 | + eventId, |
| 154 | + }, |
| 155 | + { |
| 156 | + onSuccess: () => { |
| 157 | + toast.warning(`${seatName!} 좌석을 취소했습니다`); |
| 158 | + }, |
| 159 | + }, |
| 160 | + ); |
| 161 | + setSelectedSeats(filtered); |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + if (maxSelectCount <= selectedCount) return; |
| 166 | + pickSeat( |
| 167 | + { |
| 168 | + sectionIndex: selectedSectionIndex, |
| 169 | + seatIndex: index, |
| 170 | + expectedStatus: 'reserved', |
| 171 | + eventId, |
| 172 | + }, |
| 173 | + { |
| 174 | + onSuccess: () => { |
| 175 | + toast.success(`${seatName!} 좌석 선택에\n성공했습니다`); |
| 176 | + }, |
| 177 | + }, |
| 178 | + ); |
| 179 | + setSelectedSeats([ |
| 180 | + ...selectedSeats, |
| 181 | + { seatIndex: index, sectionIndex: selectedSectionIndex, name: seatName! }, |
| 182 | + ]); |
| 183 | + }} |
| 184 | + /> |
| 185 | + ); |
| 186 | + }); |
| 187 | +}; |
| 188 | + |
| 189 | +const PICK_SEAT_MUTATION_KEY_LIST = ['seat']; |
0 commit comments