CheckPage.js 3.77 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
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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;
  };
`
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
                }
                console.log(res.data);
53
                const reserves = res.data.filter(function(item) {
CHAERIN KIM's avatar
CHAERIN KIM committed
54
55
56
                        return item !== '';
                      });
                setReserve(reserves);
57
58
59
60
61
            })
            .catch(err => {
                alert(err.error)
            });
    }
Choi Ga Young's avatar
Choi Ga Young committed
62
63
64
65
    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
66
                alert("삭제되었습니다!");
Choi Ga Young's avatar
Choi Ga Young committed
67
68
69
70
71
72
                getReserve();
            })
            .catch(err => {
                alert(err.error)
            });
    };
73

Ha YeaJin's avatar
pages    
Ha YeaJin committed
74
75
76
    return (
        <div>
            <Menu />
Lee Jin Ju's avatar
Lee Jin Ju committed
77
78
79
            <Container fluid>
                <Ta responsive="lg ml-2rem">
                    <thead className="thead-light">
Choi Ga Young's avatar
Choi Ga Young committed
80
                        <tr>
Lee Jin Ju's avatar
Lee Jin Ju committed
81
82
83
84
85
86
                            <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
87
88
89
90
91
92
                        </tr>
                    </thead>
                    <tbody>
                        {reserve.map((reserve, index) => {
                            return (
                                <tr key={index}>
Lee Jin Ju's avatar
Lee Jin Ju committed
93
94
95
96
97
98
99
                                    <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
100
101
102
103
104
105
106
                                            취소
                                        </button>
                                    </td>
                                </tr>
                            )
                        })}
                    </tbody>
Lee Jin Ju's avatar
Lee Jin Ju committed
107
108
                </Ta>
            </Container>
Ha YeaJin's avatar
pages    
Ha YeaJin committed
109
110
111
112
        </div>
    )
}

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