TheaterTable.js 2.67 KB
Newer Older
Kim, Subin's avatar
theater    
Kim, Subin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { useState, useEffect } from "react";
import theaterApi from "../../apis/theater.api.js";
import catchErrors from "../../utils/catchErrors.js";
import styles from "./admin.module.scss";

const TheaterTable = ({ setEdit, formRef }) => {
    const [theaterList, setTheaterList] = useState([])
    const [error, setError] = useState("")

    useEffect(() => {
        getTheaterList()
    }, [])

    async function getTheaterList() {
        try {
            setError("")
            const list = await theaterApi.getAll()
            setTheaterList(list)
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Kim, Subin's avatar
Kim, Subin committed
24
    async function editTheater(theaterId) {
Kim, Subin's avatar
theater    
Kim, Subin committed
25
26
        try {
            setError("")
Kim, Subin's avatar
Kim, Subin committed
27
            const res = await theaterApi.getOne(theaterId)
Kim, Subin's avatar
theater    
Kim, Subin committed
28
29
30
31
32
33
34
            setEdit({ ...res })
            formRef?.current.scrollIntoView({ behavior: "smooth", block: "center" })
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Kim, Subin's avatar
Kim, Subin committed
35
    async function deleteTheater(theaterId) {
Kim, Subin's avatar
theater    
Kim, Subin committed
36
37
        try {
            setError("")
Kim, Subin's avatar
Kim, Subin committed
38
            await theaterApi.remove(theaterId)
Kim, Subin's avatar
theater    
Kim, Subin committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
            alert("해당 상영관 정보를 성공적으로 삭제했습니다.")
            getTheaterList()
        } catch (error) {
            catchErrors(error, setError)
        }
    }

    return (
        <table className={`table text-center align-middle ${styles.tableForm}`}>
            <thead className={`table-dark align-middle ${styles.dNone}`}>
                <tr>
                    <th>상영관 이름</th>
                    <th>상영관 종류</th>
                    <th>좌석 정보</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                {theaterList.length !== 0 ? theaterList.map(info =>
                    <tr>
Kim, Subin's avatar
Kim, Subin committed
59
60
61
                        <td>{info.theaterName}</td>
                        <td>{info.theatertype.theaterTypeName}</td>
                        <td>{info.rows} {info.columns}<br /> {info.rows*info.columns}</td>
Kim, Subin's avatar
theater    
Kim, Subin committed
62
63
                        <td>
                            <div className="d-flex flex-column">
Kim, Subin's avatar
Kim, Subin committed
64
65
                                <button type="button" className="btn btn-primary my-1" onClick={() => editTheater(info.id)}>수정</button>
                                <button type="button" className="btn btn-danger my-1" onClick={() => deleteTheater(info.id)}>삭제</button>
Kim, Subin's avatar
theater    
Kim, Subin committed
66
67
68
69
70
71
72
73
74
75
76
77
                            </div>
                        </td>
                    </tr>)
                    : <tr>
                        <td colSpan="4">등록된 상영관 정보가 없습니다.</td>
                    </tr>}
            </tbody>
        </table>
    )
}

export default TheaterTable