SeatTable.js 1.93 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
2
import styles from './seatTable.module.scss'

Kim, Subin's avatar
Kim, Subin committed
3
const SeatTable = ({ theaterInfo, count, setSelectedSeats, selectedSeats, reservedSeats }) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
4
    const table = []
Kim, Subin's avatar
Kim, Subin committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    
    if (theaterInfo) {
        for (let rowIndex = 0; rowIndex < theaterInfo.rows; rowIndex++) {
            table.push(<span className="me-3" style={{ color: "gray" }}>{String.fromCharCode(rowIndex + 65)}</span>)
            for (let colIndex = 0; colIndex < theaterInfo.columns; colIndex++) {
                table.push(<span>
                        {reservedSeats.find(el => el === String(rowIndex + 1) + '-' + String(colIndex + 1))
                            ?
                            <button className={styles.btnBlock} name={rowIndex + 1} id={colIndex + 1} type="button" disabled>{colIndex + 1}</button>
                            :
                            <button className={selectedSeats.find(el => el === String(rowIndex + 1) + '-' + String(colIndex + 1)) ? styles.on : styles.btn} name={rowIndex + 1} id={colIndex + 1} type="button" onClick={handleClick}> {colIndex + 1} </button>
                        }
                    </span>)
            }
            table.push(<br />)
Jiwon Yoon's avatar
Jiwon Yoon committed
20
21
22
23
        }
    }

    function handleClick(event) {
Kim, Subin's avatar
Kim, Subin committed
24
25
26
27
        const num = Object.values(count).reduce((a, b) => (a + b))
        if (selectedSeats.find(el => el === event.target.name + '-' + event.target.id)) {
            const deleted = selectedSeats.filter((element) => element !== event.target.name + '-' + event.target.id);
            setSelectedSeats(deleted)
Jiwon Yoon's avatar
Jiwon Yoon committed
28
        } else {
Kim, Subin's avatar
Kim, Subin committed
29
30
            if (selectedSeats.length > num - 1) alert("선택한 좌석이 예매인원보다 많습니다.")
            else setSelectedSeats([...selectedSeats, event.target.name + '-' + event.target.id])
Jiwon Yoon's avatar
Jiwon Yoon committed
31
32
        }
    }
Kim, Subin's avatar
Kim, Subin committed
33

Jiwon Yoon's avatar
Jiwon Yoon committed
34
35
36
37
38
39
40
41
42
    return (
        <div className="text-center">
            <div className="mb-2" style={{ backgroundColor: "gray" }}>Screen</div>
            {table}
        </div>
    )
}

export default SeatTable