CinemaEdit.js 4.96 KB
Newer Older
1
import { useState, useEffect, useRef } from "react";
2
3
import TicketEditForm from "./TicketEditForm.js";
import TicketFeeTable from "./TicketFeeTable.js";
Kim, Subin's avatar
Kim, Subin committed
4
import cinemaApi from "../../apis/cinema.api.js";
Kim, Subin's avatar
Kim, Subin committed
5
import theaterApi from "../../apis/theater.api.js";
Kim, Subin's avatar
Kim, Subin committed
6
import catchErrors from "../../utils/catchErrors.js";
Kim, Subin's avatar
Kim, Subin committed
7
8
import styles from "./admin.module.scss";

Kim, Subin's avatar
Kim, Subin committed
9
const INIT_CINEMAINFO = {
10
11
12
13
14
    cinemaName: "",
    transportation: "",
    parking: "",
    address: "",
    moreFeeInfo: ""
Kim, Subin's avatar
Kim, Subin committed
15
16
}

Kim, Subin's avatar
Kim, Subin committed
17
const CinemaEdit = () => {
Kim, Subin's avatar
Kim, Subin committed
18
    const [cinemaInfo, setCinemaInfo] = useState(INIT_CINEMAINFO)
Kim, Subin's avatar
Kim, Subin committed
19
20
    const [theaterTypeList, setTheaterTypeList] = useState([])
    const [selectTheater, setSelectTheater] = useState(0)
21
    const [ticketFee, setTicketFee] = useState({})
Kim, Subin's avatar
Kim, Subin committed
22
    const [error, setError] = useState("")
23
    const formRef = useRef(null)
Kim, Subin's avatar
Kim, Subin committed
24
25
26

    useEffect(() => {
        getInfo()
Kim, Subin's avatar
Kim, Subin committed
27
        getTicketFeeInfo()
Kim, Subin's avatar
Kim, Subin committed
28
    }, [])
Kim, Subin's avatar
Kim, Subin committed
29

Kim, Subin's avatar
Kim, Subin committed
30
31
    function handleChange(e) {
        const { name, value } = e.target
Kim, Subin's avatar
Kim, Subin committed
32
        setCinemaInfo({ ...cinemaInfo, [name]: value })
Kim, Subin's avatar
Kim, Subin committed
33
34
    }

Kim, Subin's avatar
Kim, Subin committed
35
36
37
    async function getInfo() {
        try {
            setError("")
38
            const info = await cinemaApi.getCinemaInfo()
Kim, Subin's avatar
Kim, Subin committed
39
40
41
42
43
44
45
46
47
48
            if (info) setCinemaInfo(info)
            else setCinemaInfo(INIT_CINEMAINFO)
        } catch (error) {
            catchErrors(error, setError)
        }
    }

    async function handleSubmit() {
        try {
            setError("")
49
            await cinemaApi.editCinema(cinemaInfo)
Kim, Subin's avatar
Kim, Subin committed
50
51
52
53
54
55
            window.location.reload()
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Kim, Subin's avatar
Kim, Subin committed
56
57
58
59
60
61
62
63
64
    async function getTicketFeeInfo() {
        try {
            const res = await theaterApi.getTheaterType()
            setTheaterTypeList(res)
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Kim, Subin's avatar
Kim, Subin committed
65
66
67
    return (
        <>
            <h2 className="border-bottom border-2 text-center pb-2 me-2">현재 영화관 정보</h2>
Kim, Subin's avatar
Kim, Subin committed
68
            <div className="mb-3">
Kim, Subin's avatar
theater    
Kim, Subin committed
69
                <label htmlfor="cinemaName" className="form-label">영화관 이름</label>
Kim, Subin's avatar
Kim, Subin committed
70
71
72
                <input type="text" className={`form-control mb-2 ${styles.shadowNone}`} id="cinemaName" name="cinemaName" value={cinemaInfo.cinemaName} onChange={handleChange} />
                <p> 상영관 : 8개관 |  좌석 : 1,282</p>
            </div>
Kim, Subin's avatar
Kim, Subin committed
73
            <div className="mb-3">
Kim, Subin's avatar
theater    
Kim, Subin committed
74
                <label htmlfor="transportation" className="form-label">대중교통 안내</label>
Kim, Subin's avatar
Kim, Subin committed
75
                <textarea className={`form-control ${styles.shadowNone} ${styles.textarea}`} rows="7" id="transportation" name="transportation" value={cinemaInfo.transportation} onChange={handleChange}></textarea>
Kim, Subin's avatar
Kim, Subin committed
76
77
            </div>
            <div className="mb-3">
Kim, Subin's avatar
theater    
Kim, Subin committed
78
                <label htmlfor="parking" className="form-label">자가용/주차안내</label>
Kim, Subin's avatar
Kim, Subin committed
79
                <textarea className={`form-control ${styles.shadowNone} ${styles.textarea}`} rows="7" id="parking" name="parking" value={cinemaInfo.parking} onChange={handleChange}></textarea>
Kim, Subin's avatar
Kim, Subin committed
80
            </div>
Kim, Subin's avatar
theater    
Kim, Subin committed
81
            <label htmlfor="keyword" className="form-label">지도보기</label>
Kim, Subin's avatar
Kim, Subin committed
82
            <div className="input-group mb-3">
Kim, Subin's avatar
Kim, Subin committed
83
                <span className="input-group-text" id="address"><i className="bi bi-geo-alt-fill"></i></span>
Kim, Subin's avatar
Kim, Subin committed
84
85
                <input type="text" className={`form-control ${styles.shadowNone}`} id="address" name="address" value={cinemaInfo.address} onChange={handleChange} value={cinemaInfo.address} />
            </div>
86
87
            <p className="mb-0">영화관람료 설정</p>
            <p className="text-danger">*추가금액 정보를 입력바랍니다. 필요에 따라 기본가격 또한 변경 가능합니다.</p>
88
            <TicketEditForm editFee={ticketFee} formRef={formRef} />
Kim, Subin's avatar
Kim, Subin committed
89
90
91
92
93
94
95
96
            <label className="form-label">영화관람료 안내</label>
            <nav aria-label="breadcrumb">
                <ol className={"breadcrumb" + (theaterTypeList.length === 0 ? " d-flex justify-content-center" : "" )}>
                    {theaterTypeList.length !== 0 ? theaterTypeList.map(theater => <li className={`breadcrumb-item ${styles.cursor}`} key={theater.id} onClick={() => setSelectTheater(theater.id)}>{theater.theaterTypeName}</li>) 
                    : <li>등록된 관람료 관련 정보가 없습니다.</li>}
                </ol>
            </nav>
            <TicketFeeTable selectTheater={selectTheater} setEditFee={setTicketFee} formRef={formRef} />
97
            <div className="mb-3">
Kim, Subin's avatar
theater    
Kim, Subin committed
98
                <label htmlfor="moreFeeInfo" className="form-label">관람료 추가정보</label>
99
100
                <textarea className={`form-control ${styles.shadowNone} ${styles.textarea}`} rows="7" id="moreFeeInfo" name="moreFeeInfo" value={cinemaInfo.moreFeeInfo} onChange={handleChange}></textarea>
            </div>
Kim, Subin's avatar
Kim, Subin committed
101
102
            <div className="d-grid gap-2 mb-5">
                <button type="submit" className={`btn btn-dark shadow-none ${styles.customBtn}`} onClick={handleSubmit}>수정</button>
Kim, Subin's avatar
Kim, Subin committed
103
            </div>
Kim, Subin's avatar
Kim, Subin committed
104
105
106
107
        </>
    )
}
export default CinemaEdit