import { Link, useHistory } from 'react-router-dom' import { useState, useEffect, useRef } from 'react' import { Modal } from 'bootstrap' import CountButton from '../components/CountButton' import SeatTable from '../components/SeatTable/SeatTable' import styles from '../components/SeatTable/seatTable.module.scss' import axios from 'axios' import { useAuth } from '../context/auth_context.js' import catchErrors from '../utils/catchErrors' 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 { const response = await axios.post('/api/theater/getInfo', { theaterName: ticketInfo.selectedTheater }) setTheaterInfo(response.data) const response2 = await axios.post('/api/reservation/findreservation', { timetable: 1 }) const reserve = response2.data.map((el) => el.row + '-' + el.col ) setReservedSeats(reserve) } catch (error) { catchErrors(error, setError) } } async function getTicketFee() { try { 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 ( <>