CheckPage.js 3.74 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';
5
import { Container, Table } from 'react-bootstrap';
Lee Jin Ju's avatar
Lee Jin Ju committed
6
7
8
import styled from 'styled-components';

const Ta = styled(Table)`
Kim, Subin's avatar
Kim, Subin committed
9
  margin-top: 0.5em;
Lee Jin Ju's avatar
Lee Jin Ju committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

  & th, & td {
    padding: 0;
    vertical-align: middle;
    font-size: 0.9rem;
    margin-left : auto; margin-right : auto;
    border-spacing: initial;
  };

  & tr {
    display: d-flex;
    width: 150px;
  };

  & td {
    align-items: center;
    margin: 10px;
  };
`
Ha YeaJin's avatar
pages    
Ha YeaJin committed
29

30
function Check(props) {
CHAERIN KIM's avatar
CHAERIN KIM committed
31
    const [reserve, setReserve] = useState([]);
CHAERIN KIM's avatar
CHAERIN KIM committed
32
    const [state, setState] = useState()
CHAERIN KIM's avatar
CHAERIN KIM committed
33
34
35
    useEffect(() => {
        getReserve();
    }, [])
36

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

39
40
41
42
43
    function getReserve() {
        axios.get(`/reserves/${props.match.params.id}`, {
            headers: { authorization: localStorage.getItem('token') },
        })
            .then(res => {
CHAERIN KIM's avatar
CHAERIN KIM committed
44
45
46
47
                if (res.status === 404) {
                    alert(res.data.error);
                }
                if (res.status === 419) {
48
                    alert(res.data.error);
CHAERIN KIM's avatar
CHAERIN KIM committed
49
50
                    localStorage.clear();
                    setState(true);
51
                }
52
                const reserves = res.data.filter(function(item) {
CHAERIN KIM's avatar
CHAERIN KIM committed
53
54
55
                        return item !== '';
                      });
                setReserve(reserves);
56
57
58
59
60
            })
            .catch(err => {
                alert(err.error)
            });
    }
Choi Ga Young's avatar
Choi Ga Young committed
61
62
63
64
    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
65
                alert("삭제되었습니다!");
Choi Ga Young's avatar
Choi Ga Young committed
66
67
68
69
70
71
                getReserve();
            })
            .catch(err => {
                alert(err.error)
            });
    };
72

Ha YeaJin's avatar
pages    
Ha YeaJin committed
73
74
75
    return (
        <div>
            <Menu />
Lee Jin Ju's avatar
Lee Jin Ju committed
76
77
78
            <Container fluid>
                <Ta responsive="lg ml-2rem">
                    <thead className="thead-light">
Choi Ga Young's avatar
Choi Ga Young committed
79
                        <tr>
Lee Jin Ju's avatar
Lee Jin Ju committed
80
81
82
83
84
85
                            <th className="text-center">날짜</th>
                            <th className="text-center">시간</th>
                            <th className="text-center">강의실</th>
                            <th className="text-center">사용인원</th>
                            {/* <th>승인여부</th> */}
                            <th className="text-center">예약취소</th>
Choi Ga Young's avatar
Choi Ga Young committed
86
87
88
89
90
91
                        </tr>
                    </thead>
                    <tbody>
                        {reserve.map((reserve, index) => {
                            return (
                                <tr key={index}>
Lee Jin Ju's avatar
Lee Jin Ju committed
92
93
94
95
96
97
98
                                    <td className="text-center">{reserve.date}</td>
                                    <td className="text-center">{reserve.starttime}~{(Number(reserve.starttime) + reserve.usetime)}</td>
                                    <td className="text-center">{reserve.room}</td>
                                    <td className="text-center">{reserve.num}</td>
                                    {/* <td>{reserve.check ? (reserve.approve ? "사용가능" : "사용불가") : "승인대기중"}</td> */}
                                    <td className="text-center">
                                        <button onClick={() => remove(index)} className="btn btn-danger btn-sm">
Choi Ga Young's avatar
Choi Ga Young committed
99
100
101
102
103
104
105
                                            취소
                                        </button>
                                    </td>
                                </tr>
                            )
                        })}
                    </tbody>
Lee Jin Ju's avatar
Lee Jin Ju committed
106
107
                </Ta>
            </Container>
Ha YeaJin's avatar
pages    
Ha YeaJin committed
108
109
110
111
        </div>
    )
}

Kim, Subin's avatar
Kim, Subin committed
112
export default Check