TheaterEditForm.js 4.34 KB
Newer Older
Kim, Subin's avatar
theater    
Kim, Subin committed
1
2
3
4
5
6
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 INIT_THEATER = {
Kim, Subin's avatar
Kim, Subin committed
7
    id: 0,
Kim, Subin's avatar
theater    
Kim, Subin committed
8
    theaterName: "",
Kim, Subin's avatar
Kim, Subin committed
9
    theatertypeId: 0,
Kim, Subin's avatar
theater    
Kim, Subin committed
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
    rows: 1,
    columns: 1
}

const TheaterEditForm = ({ edit, formRef }) => {
    const [theater, setTheater] = useState(INIT_THEATER)
    const [types, setTypes] = useState([])
    const [error, setError] = useState("")

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

    useEffect(() => {
        setTheater({ ...theater, ...edit })
    }, [edit])

    async function getTypeList() {
        try {
            setError("")
            const resTypes = await theaterApi.getTheaterType()
            setTypes(resTypes)
        } catch (error) {
            catchErrors(error, setError)
        }
    }

    function handleChange(e) {
        const { name, value } = e.target
        setTheater({ ...theater, [name]: value })
    }

    async function handleSubmit(e) {
        e.preventDefault()
        try {
            setError("")
            await theaterApi.sendData(theater)
            alert("해당 상영관 정보 등록이 성공적으로 완료되었습니다.")
            window.location.reload()
        } catch (error) {
            catchErrors(error, setError)
        }
    }

    return (
Kim, Subin's avatar
Kim, Subin committed
55
56
        <form ref={formRef} className="mb-5" onSubmit={handleSubmit}>
            <div className="d-flex justify-content-lg-between row row-cols-2 row-cols-lg-5 gx-0 gy-2 gy-lg-0 mb-2 mb-lg-3">
Kim, Subin's avatar
theater    
Kim, Subin committed
57
                <label htmlfor="theaterName" className="col-3 col-lg-auto col-form-label">상영관 이름</label>
Kim, Subin's avatar
Kim, Subin committed
58
                <div className="col-8 col-lg-4">
Kim, Subin's avatar
theater    
Kim, Subin committed
59
60
                    <input className={`form-control ${styles.shadowNone}`} id="theaterName" name="theaterName" type="text" value={theater.theaterName} onChange={handleChange} />
                </div>
Kim, Subin's avatar
Kim, Subin committed
61
                <lebel htmlfor="theaterName" className="col-auto col-form-label mx-2 mx-lg-0"></lebel>
Kim, Subin's avatar
theater    
Kim, Subin committed
62
63
                <label htmlfor="theaterType" className="col-3 col-lg-auto col-form-label text-lg-center">상영관 종류</label>
                <div className="col-9 col-lg-5">
Kim, Subin's avatar
Kim, Subin committed
64
                    <select className={`form-select ${styles.shadowNone} ${styles.selectInput}`} id="theatertypeId" name="theatertypeId" value={theater.theatertypeId} onChange={handleChange} aria-label="select theaterType" defaultValue="0">
Kim, Subin's avatar
theater    
Kim, Subin committed
65
66
67
68
                        {types.length !== 0 ?
                            types.map((type, index) => {
                                if (index === 0) return <>
                                    <option value="0" disabled>상영관 종류를 선택해주십시오.</option>
Kim, Subin's avatar
Kim, Subin committed
69
                                    <option value={type.id}>{type.theaterTypeName}</option>
Kim, Subin's avatar
theater    
Kim, Subin committed
70
                                </>
Kim, Subin's avatar
Kim, Subin committed
71
                                else return <option value={type.id}>{type.theaterTypeName}</option>
Kim, Subin's avatar
theater    
Kim, Subin committed
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
                            })
                            : <option value="0" disabled>서버에 등록된 상영관 종류가 없습니다.</option>}
                    </select>
                </div>
            </div>
            <div className="d-flex flex-wrap row row-cols-2 gx-0 gy-2 gy-lg-0">
                <label htmlfor="seatInfo" className="col-3 col-lg-auto col-form-label me-lg-4">좌석 정보</label>
                <div className="d-flex col-9 col-sm-5">
                    <div className="col-3 col-lg-2">
                        <input className={`form-control ${styles.shadowNone}`} id="rows" name="rows" type="number" min="1" max="26" value={theater.rows} onChange={handleChange} />
                    </div>
                    <label htmlfor="rows" className="col-form-label mx-2"></label>
                    <div className="col-3 col-lg-2">
                        <input className={`form-control ${styles.shadowNone}`} id="columns" name="columns" type="number" min="1" value={theater.columns} onChange={handleChange} />
                    </div>
                    <label htmlfor="columns" className="col-form-label mx-2"></label>
                </div>
                <div className="col-12 col-sm-auto ms-sm-auto">
                    <button type="submit" className={`btn btn-dark w-100 ${styles.customBtn}`}>추가</button>
                </div>
            </div>
        </form>
    )
}

export default TheaterEditForm