CheckPage.js 3.74 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
3
4
5
6
7
8
9
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React, { useState, useEffect } from 'react';
import Menu from '../Components/Menu';
import { Redirect } from 'react-router-dom';
import axios from 'axios';
import { Container, Table } from 'react-bootstrap';
import styled from 'styled-components';

const Ta = styled(Table)`
  margin-top: 0.5em;

  & 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;
  };
`

function Check(props) {
    const [reserve, setReserve] = useState([]);
    const [state, setState] = useState()
    useEffect(() => {
        getReserve();
    }, [])

    if (state) return <Redirect to="/" />;

    function getReserve() {
        axios.get(`/reserves/${props.match.params.id}`, {
            headers: { authorization: localStorage.getItem('token') },
        })
            .then(res => {
                if (res.status === 404) {
                    alert(res.data.error);
                }
                if (res.status === 419) {
                    alert(res.data.error);
                    localStorage.clear();
                    setState(true);
                }
                const reserves = res.data.filter(function(item) {
                        return item !== '';
                      });
                setReserve(reserves);
            })
            .catch(err => {
                alert(err.error)
            });
    }
    function remove(index) {
        axios.delete(`/reserves/${reserve[index]._id}`)
            .then(res => {
                if (res.status === 404) return alert(res.data.error)
                alert("삭제되었습니다!");
                getReserve();
            })
            .catch(err => {
                alert(err.error)
            });
    };

    return (
        <div>
            <Menu />
            <Container fluid>
                <Ta responsive="lg ml-2rem">
                    <thead className="thead-light">
                        <tr>
                            <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>
                        </tr>
                    </thead>
                    <tbody>
                        {reserve.map((reserve, index) => {
                            return (
                                <tr key={index}>
                                    <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">
                                            취소
                                        </button>
                                    </td>
                                </tr>
                            )
                        })}
                    </tbody>
                </Ta>
            </Container>
        </div>
    )
}

export default Check