SeatTable.js 2.2 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
2
3
4
5
import { useState } from 'react'
import styles from './seatTable.module.scss'

const SeatTable = (props) => {
    const table = []
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    if (props.theaterInfo) {
        for (let rowIndex = 0; rowIndex < props.theaterInfo.rows; rowIndex++) {
            table.push(<span className="me-3" style={{ color: "gray" }}>{String.fromCharCode(rowIndex + 65)}</span>)
            for (let colIndex = 0; colIndex < props.theaterInfo.columns; colIndex++) {
                table.push(
                    <span>
                        {props.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={props.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
22
23
24
        }
    }

25

Jiwon Yoon's avatar
Jiwon Yoon committed
26
27
    function handleClick(event) {
        const num = Object.values(props.count).reduce((a, b) => (a + b))
28
        if (props.selectedSeats.find(el => el === event.target.name + '-' + event.target.id)) {
Jiwon Yoon's avatar
Jiwon Yoon committed
29
            //제거
30
            const deleted = props.selectedSeats.filter((element) => element !== event.target.name + '-' + event.target.id);
Jiwon Yoon's avatar
Jiwon Yoon committed
31
32
33
34
35
36
            props.setSelectedSeats(deleted)
        } else {
            if (props.selectedSeats.length > num - 1) {
                alert("선택한 좌석이 예매인원보다 많습니다.")
            } else {
                //추가
37
                props.setSelectedSeats([...props.selectedSeats, event.target.name + '-' + event.target.id])
Jiwon Yoon's avatar
Jiwon Yoon committed
38
39
40
41
42
43
            }
        }

    }
    return (
        <div className="text-center">
44
            {/* {console.log(props.theaterInfo)} */}
Jiwon Yoon's avatar
Jiwon Yoon committed
45
46
47
48
49
50
51
52
            {console.log(props.selectedSeats)}
            <div className="mb-2" style={{ backgroundColor: "gray" }}>Screen</div>
            {table}
        </div>
    )
}

export default SeatTable