import { useState, useEffect, useRef } from 'react' import { Link, useHistory } from 'react-router-dom' import CountButton from '../components/CountButton' import SeatTable from '../components/SeatTable/SeatTable' import axios from 'axios' import { useAuth } from '../context/auth_context.js' import { Modal } from 'bootstrap' import catchErrors from '../utils/catchErrors' import styles from '../components/SeatTable/seatTable.module.scss' import reservationApi from '../apis/reservation.api.js' const TicketingSeatPage = ({ location }) => { const history = useHistory() const modalRef = useRef(null) const modal = useRef() const { user } = useAuth() const [error, setError] = useState() const [ticketInfo, setTicketInfo] = useState({ ...location.state }) const [theaterInfo, setTheaterInfo] = useState({ theatertypeId: 0 }) const [selectedSeats, setSelectedSeats] = useState([]) const [reservedSeats, setReservedSeats] = useState([]) const [ticketFee, setTicketFee] = useState({ youth: 0, adult: 0, senior: 0 }) const [count, setCount] = useState({ youth: 0, adult: 0, senior: 0 }) useEffect(() => { getInfo() }, []) useEffect(() => { getTicketFee() }, [theaterInfo.theatertypeId]) async function getInfo() { try { setError("") const response = await axios.post('/api/theater/getInfo', { theaterId: ticketInfo.selectedTheater }) setTheaterInfo(response.data) const response2 = await reservationApi.findReservedSeats(1); const reserve = response2.map((el) => el.row + '-' + el.col ); setReservedSeats(reserve); } catch (error) { catchErrors(error, setError) } } async function getTicketFee() { try { setError("") const response3 = await axios.get(`/api/info/ticketfee`, { params: { theaterTypeId: theaterInfo.theatertypeId } }) const basicFee = response3.data[0].day + response3.data[0].defaultPrice + response3.data[0].weekdays setTicketFee({ adult: basicFee + response3.data[0].adult, youth: basicFee + response3.data[0].youth, senior: basicFee + response3.data[0].senior }) } catch (error) { catchErrors(error, setError) } } function loginModal() { if (user.role === "member") { history.push("/payment", { ...ticketInfo, selectedSeats: selectedSeats, ...count, totalFee: count.adult * ticketFee.adult + count.youth * ticketFee.youth + count.senior * ticketFee.senior }); } else { modal.current = new Modal(modalRef.current) modal.current?.show() } } return ( <>