TimeTable.js 4.29 KB
Newer Older
1
import { useState, useEffect } from "react";
Kim, Subin's avatar
Kim, Subin committed
2
import { useHistory } from "react-router-dom";
3
import moment from 'moment';
4
import HorizontalCalender from "../Calender/HorizontalCalender.js";
5
6
import timetableApi from "../../apis/timetable.api.js";
import catchErrors from "../../utils/catchErrors.js";
7
import styles from "./admin.module.scss";
8

Kim, Subin's avatar
Kim, Subin committed
9
const TimeTable = ({ ticketInfo = { movieId: 0 }, setTicketInfo }) => {
10
11
12
    const [selectDate, setSelectDate] = useState(moment().format('YYYY-MM-DD'))
    const [timeList, setTimeList] = useState([])
    const [error, setError] = useState("")
Kim, Subin's avatar
Kim, Subin committed
13
    const history = useHistory()
14
15

    useEffect(() => {
Kim, Subin's avatar
Kim, Subin committed
16
        getTimeTable(selectDate, ticketInfo.movieId)
17
18
    }, [selectDate])

Kim, Subin's avatar
Kim, Subin committed
19
20
21
22
23
    useEffect(() => {
        getTimeTable(selectDate, ticketInfo.movieId)
    }, [ticketInfo.movieId])

    async function getTimeTable(selectDate, movieId) {
24
25
        try {
            setError("")
Kim, Subin's avatar
Kim, Subin committed
26
            const res = await timetableApi.getAll(selectDate, movieId)
27
28
29
30
31
32
            setTimeList(res)
        } catch (error) {
            catchErrors(error, setError)
        }
    }

33
    async function deleteTime(timeId) {
34
35
        try {
            setError("")
36
            await timetableApi.remove(timeId)
37
            alert('해당 상영시간표 정보를 성공적으로 삭제하였습니다.')
Kim, Subin's avatar
Kim, Subin committed
38
            window.location.reload()
39
40
41
42
43
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Kim, Subin's avatar
Kim, Subin committed
44
45
46
47
48
49
50
51
52
53
54
55
    function handleClick(time) {
        const date = new Date(time.start_time)
        let hours = date.getHours()
        let mins = date.getMinutes()

        if (hours <= 9) hours = '0' + hours
        if (mins <= 9) mins = '0' + mins

        setTicketInfo({
            ...ticketInfo,
            timetableId: time.id,
            time: time.date.split('T')[0] + " " + hours + ":" + mins,
Jiwon Yoon's avatar
Jiwon Yoon committed
56
            selectedTheater: time.theater.theaterName,
Kim, Subin's avatar
Kim, Subin committed
57
            theaterId: time.theaterId,
Jiwon Yoon's avatar
Jiwon Yoon committed
58
59
            partTime: time.partTime,
            week: time.week
Kim, Subin's avatar
Kim, Subin committed
60
61
62
        })
    }

Kim, Subin's avatar
Kim, Subin committed
63
    return (
Kim, Subin's avatar
Kim, Subin committed
64
        <>
65
            <HorizontalCalender selectDate={selectDate} setSelectDate={setSelectDate} />
66
            {timeList.length !== 0 ?
Kim, Subin's avatar
Kim, Subin committed
67
                timeList.map(el => <div className={"mt-4" + (/theater/g.test(history.location.pathname) ? " text-start" : "")}>
Kim, Subin's avatar
Kim, Subin committed
68
69
                    <h5 className="mb-0">{el.theaterName}  / <p className="d-inline fs-6 mb-0">{el.theaterTypeName}</p></h5>
                    {el.timetable.map(time => {
Kim, Subin's avatar
Kim, Subin committed
70
                        if (/ticket/g.test(history.location.pathname))
Kim, Subin's avatar
Kim, Subin committed
71
72
                            return <div className="d-inline-flex m-2">
                                <div className={`card text-dark ${styles.cursor}`} onClick={() => handleClick(time)}>
Kim, Subin's avatar
Kim, Subin committed
73
                                    <div className="card-body py-1"><img src={`${time.partTime === "morning" ? '/images/sun.svg' : time.partTime === "night" ? '/images/moon.svg' : '...'} `} style={{ width: '20px' }} alt="" />{moment(time.start_time).format('HH:mm')} ~ {moment(time.end_time).format('HH:mm')}</div>
Kim, Subin's avatar
Kim, Subin committed
74
75
76
77
                                    <div className="card-footer text-center py-1">{time.theater.rows * time.theater.columns - time.reservations} / {time.theater.rows * time.theater.columns}</div>
                                </div>
                            </div>
                        else return <div className="d-inline-flex flex-column m-2">
Kim, Subin's avatar
Kim, Subin committed
78
                            {/theater/g.test(history.location.pathname) ? null : <div className="d-flex justify-content-end">
Kim, Subin's avatar
Kim, Subin committed
79
                                <button type="button" className={`btn btn-dark btn-sm shadow-none ${styles.customBtn}`} onClick={() => deleteTime(time.id)}>X</button>
Kim, Subin's avatar
Kim, Subin committed
80
81
                            </div>}
                            <div className={"card" + (/theater/g.test(history.location.pathname) ? " text-dark" : "")}>
Kim, Subin's avatar
Kim, Subin committed
82
83
84
                                <div className="card-body py-1">{moment(time.start_time).format('HH:mm')} ~ {moment(time.end_time).format('HH:mm')}</div>
                                <div className="card-footer text-center py-1">{time.title}</div>
                            </div>
85
                        </div>
Kim, Subin's avatar
Kim, Subin committed
86
                    })}
87
                </div>)
Kim, Subin's avatar
Kim, Subin committed
88
                : <p className="text-center mt-5 mb-0">서버에 저장되어 있는 상영시간표가 존재하지 않습니다.</p>}
Kim, Subin's avatar
Kim, Subin committed
89
        </>
Kim, Subin's avatar
Kim, Subin committed
90
91
92
93
    )
}

export default TimeTable