TicketFeeTable.js 5.42 KB
Newer Older
1
2
3
4
import { useState, useEffect } from "react";
import cinemaApi from "../../apis/cinema.api.js";
import catchErrors from "../../utils/catchErrors.js";

Kim, Subin's avatar
Kim, Subin committed
5
const TicketFeeTable = ({ setEditFee }) => {
6
7
8
9
10
11
12
13
14
    const [ticketFee, setTicketFee] = useState([])
    const [error, setError] = useState("")

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

    async function getInfo() {
        const res = await cinemaApi.getTicketFee()
Kim, Subin's avatar
Kim, Subin committed
15
        setTicketFee(res)
16
17
18
19
20
21
    }

    async function editRow(theaterType) {
        try {
            setError("")
            const res = await cinemaApi.getTicketFeeOne(theaterType)
Kim, Subin's avatar
Kim, Subin committed
22
            setEditFee({ ...res })
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
        } catch (error) {
            catchErrors(error, setError)
        }
    }

    async function deleteData(theaterType) {
        try {
            setError("")
            await cinemaApi.removeTicketFee(theaterType)
            alert("해당 관람료 정보를 성공적으로 삭제했습니다.")
            getInfo()
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Kim, Subin's avatar
Kim, Subin committed
39
40
41
42
    function priceToString(price) {
        return price.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    }

43
    return (
Kim, Subin's avatar
Kim, Subin committed
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        <table className="table caption-top text-center align-middle">
            <caption className="text-dark">영화관람료 안내</caption>
            <thead className="table-dark">
                <tr>
                    <th>상영관 종류</th>
                    <th>주중 / 주말</th>
                    <th>시간대</th>
                    <th>청소년</th>
                    <th>일반</th>
                    <th>경로</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                {ticketFee.length !== 0 ? ticketFee.map(info =>
                    <>
                        <tr>
                            <td rowSpan="6">{info.theaterType}</td>
                            <td rowSpan="3">주중(~)</td>
                            <td>조조 (06:00 ~ )</td>
                            <td>{priceToString(info.weekdays + info.morning + info.youth + info.defaultPrice)}</td>
                            <td>{priceToString(info.weekdays + info.morning + info.adult + info.defaultPrice)}</td>
                            <td>{priceToString(info.weekdays + info.morning + info.senior + info.defaultPrice)}</td>
                            <td rowSpan="6">
                                <div className="d-flex flex-column">
                                    <button type="button" className="btn btn-primary my-1" onClick={() => editRow(info.theaterType)}>수정</button>
                                    <button type="button" className="btn btn-danger my-1" onClick={() => deleteData(info.theaterType)}>삭제</button>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>일반 (11:00 ~ )</td>
                            <td>{priceToString(info.weekdays + info.day + info.youth + info.defaultPrice)}</td>
                            <td>{priceToString(info.weekdays + info.day + info.adult + info.defaultPrice)}</td>
                            <td>{priceToString(info.weekdays + info.day + info.senior + info.defaultPrice)}</td>
                        </tr>
                        <tr>
                            <td>심야 (00:00 ~ )</td>
                            <td>{priceToString(info.weekdays + info.night + info.youth + info.defaultPrice)}</td>
                            <td>{priceToString(info.weekdays + info.night + info.adult + info.defaultPrice)}</td>
                            <td>{priceToString(info.weekdays + info.night + info.senior + info.defaultPrice)}</td>
                        </tr>
                        <tr>
                            <td rowSpan="3">주말(~)  공휴일</td>
                            <td>조조 (06:00 ~ )</td>
                            <td>{priceToString(info.weekend + info.morning + info.youth + info.defaultPrice)}</td>
                            <td>{priceToString(info.weekend + info.morning + info.adult + info.defaultPrice)}</td>
                            <td>{priceToString(info.weekend + info.morning + info.senior + info.defaultPrice)}</td>
                        </tr>
                        <tr>
                            <td>일반 (11:00 ~ )</td>
                            <td>{priceToString(info.weekend + info.day + info.youth + info.defaultPrice)}</td>
                            <td>{priceToString(info.weekend + info.day + info.adult + info.defaultPrice)}</td>
                            <td>{priceToString(info.weekend + info.day + info.senior + info.defaultPrice)}</td>
                        </tr>
                        <tr>
                            <td>심야 (00:00 ~ )</td>
                            <td>{priceToString(info.weekend + info.night + info.youth + info.defaultPrice)}</td>
                            <td>{priceToString(info.weekend + info.night + info.adult + info.defaultPrice)}</td>
                            <td>{priceToString(info.weekend + info.night + info.senior + info.defaultPrice)}</td>
                        </tr>
                    </>)
                    : <tr>
                        <td colSpan="7">등록된 관람료 관련 정보가 없습니다.</td>
                    </tr>}
            </tbody>
        </table>
111
112
113
    )
}

Kim, Subin's avatar
Kim, Subin committed
114
export default TicketFeeTable