TheaterTable.js 2.51 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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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)
        }
    }

    async function editTheater() {
        try {
            setError("")
            const res = await theaterApi.getOne()
            setEdit({ ...res })
            formRef?.current.scrollIntoView({ behavior: "smooth", block: "center" })
        } catch (error) {
            catchErrors(error, setError)
        }
    }

    async function deleteTheater() {
        try {
            setError("")
            await theaterApi.remove()
            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>
                        <td>ads</td>
                        <td>ads</td>
                        <td>ads</td>
                        <td>
                            <div className="d-flex flex-column">
                                <button type="button" className="btn btn-primary my-1" onClick={() => editTheater()}>수정</button>
                                <button type="button" className="btn btn-danger my-1" onClick={() => deleteTheater()}>삭제</button>
                            </div>
                        </td>
                    </tr>)
                    : <tr>
                        <td colSpan="4">등록된 상영관 정보가 없습니다.</td>
                    </tr>}
            </tbody>
        </table>
    )
}

export default TheaterTable