CheckPage.js 3.08 KB
Newer Older
Ha YeaJin's avatar
pages    
Ha YeaJin committed
1
2
import React, { useState, useEffect } from 'react';
import Menu from '../Components/Menu';
CHAERIN KIM's avatar
CHAERIN KIM committed
3
import { Redirect } from 'react-router-dom';
4
import axios from 'axios';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
5

6
function Check(props) {
CHAERIN KIM's avatar
CHAERIN KIM committed
7
    const [reserve, setReserve] = useState([]);
CHAERIN KIM's avatar
CHAERIN KIM committed
8
    const [state, setState] = useState()
CHAERIN KIM's avatar
CHAERIN KIM committed
9
10
11
    useEffect(() => {
        getReserve();
    }, [])
12

CHAERIN KIM's avatar
CHAERIN KIM committed
13
14
    if (state) return <Redirect to="/" />;

15
16
17
18
19
    function getReserve() {
        axios.get(`/reserves/${props.match.params.id}`, {
            headers: { authorization: localStorage.getItem('token') },
        })
            .then(res => {
CHAERIN KIM's avatar
CHAERIN KIM committed
20
21
22
23
                if (res.status === 404) {
                    alert(res.data.error);
                }
                if (res.status === 419) {
24
                    alert(res.data.error);
CHAERIN KIM's avatar
CHAERIN KIM committed
25
26
                    localStorage.clear();
                    setState(true);
27
28
                }
                console.log(res.data);
CHAERIN KIM's avatar
CHAERIN KIM committed
29
30
31
32
                const reserves=res.data.filter(function(item) {
                        return item !== '';
                      });
                setReserve(reserves);
33
34
35
36
37
            })
            .catch(err => {
                alert(err.error)
            });
    }
Choi Ga Young's avatar
Choi Ga Young committed
38
39
40
41
    function remove(index) {
        axios.delete(`/reserves/${reserve[index]._id}`)
            .then(res => {
                if (res.status === 404) return alert(res.data.error)
CHAERIN KIM's avatar
CHAERIN KIM committed
42
                alert("삭제되었습니다!");
Choi Ga Young's avatar
Choi Ga Young committed
43
44
45
46
47
48
                getReserve();
            })
            .catch(err => {
                alert(err.error)
            });
    };
49

Ha YeaJin's avatar
pages    
Ha YeaJin committed
50
51
52
    return (
        <div>
            <Menu />
CHAERIN KIM's avatar
CHAERIN KIM committed
53
            <div className="">
Choi Ga Young's avatar
Choi Ga Young committed
54
55
56
57
                <table className="table">
                    <thead>
                        <tr>
                            <th>날짜</th>
CHAERIN KIM's avatar
CHAERIN KIM committed
58
                            <th>시간</th>
Choi Ga Young's avatar
Choi Ga Young committed
59
                            <th>강의실</th>
CHAERIN KIM's avatar
CHAERIN KIM committed
60
61
                            <th>사용인원</th>
                            <th>승인여부</th>
Choi Ga Young's avatar
Choi Ga Young committed
62
63
64
65
66
67
68
69
                            <th>예약취소</th>
                        </tr>
                    </thead>
                    <tbody>
                        {reserve.map((reserve, index) => {
                            return (
                                <tr key={index}>
                                    <td>{reserve.date}</td>
70
                                    <td>{reserve.starttime}~{(Number(reserve.starttime) + reserve.usetime)}</td>
Choi Ga Young's avatar
Choi Ga Young committed
71
                                    <td>{reserve.room}</td>
CHAERIN KIM's avatar
CHAERIN KIM committed
72
                                    <td>{reserve.num}</td>
73
                                    <td>{reserve.check ? (reserve.approve ? "사용가능" : "사용불가") : "승인대기중"}</td>
Choi Ga Young's avatar
Choi Ga Young committed
74
75
76
77
78
79
80
81
82
83
84
                                    <td>
                                        <button onClick={() => remove(index)} className="btn btn-danger">
                                            취소
                                        </button>
                                    </td>
                                </tr>
                            )
                        })}
                    </tbody>
                </table>

Ha YeaJin's avatar
pages    
Ha YeaJin committed
85
86
87
88
89
            </div>
        </div>
    )
}

Choi Ga Young's avatar
Choi Ga Young committed
90
export default Check