ACheckPage.js 3.04 KB
Newer Older
1
2
3
import React, { useState, useEffect } from 'react';
import Menu from '../Components/Menu';
import axios from 'axios';
4
import { Link, Redirect } from 'react-router-dom';
Lee Jin Ju's avatar
Lee Jin Ju committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { Container, Table } from 'react-bootstrap';
import styled from 'styled-components';

const Ta = styled(Table)`
  & th, & td {
    padding: 0;
    vertical-align: middle;
  };

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

  & td {
    align-items: center;
    width: 70px;
  };
  
`
25
26

function ACheck(props) {
27
  const [state, setState] = useState()
28
  const [reserve, setReserve] = useState([]);
Lee Jin Ju's avatar
Lee Jin Ju committed
29

30
  useEffect(() => {
Ha YeaJin's avatar
Ha YeaJin committed
31
    getReserve();
32
33
34
  }, [])

  function getReserve() {
Ha YeaJin's avatar
Ha YeaJin committed
35
36
    axios.get(`/users/admin/${props.match.params.id}`, {
      headers: { authorization: localStorage.getItem('token') },
37
38
    })
      .then(res => {
39
40
41
        if (res.status === 404) {
          alert(res.data.error)
          setState(true);
42
43
44
45
46
47
48
49
        }
        console.log(res.data);
        setReserve(res.data);
      })
      .catch(err => {
        alert(err.error)
      });
  }
CHAERIN KIM's avatar
CHAERIN KIM committed
50
  if (state) return <Redirect to="/" />;
51

52
  function remove(index) {
Ha YeaJin's avatar
Ha YeaJin committed
53
    axios.put(`/reserves/${reserve[index]._id}`)
54
55
      .then(res => {
        if (res.status === 404) return alert(res.data.error)
Ha YeaJin's avatar
Ha YeaJin committed
56
        alert("승인을 거절했습니다!");
57
58
59
60
61
62
63
64
        getReserve();
      })
      .catch(err => {
        alert(err.error)
      });
  };

  function admit(index) {
Ha YeaJin's avatar
Ha YeaJin committed
65
66
67
    axios.put(`/reserves/${reserve[index]._id}`, {
      approve: true,
    })
68
69
70
71
72
73
74
75
76
77
78
79
80
      .then(res => {
        if (res.status === 404) return alert(res.data.error)
        alert("승인되었습니다!");
        getReserve();
      })
      .catch(err => {
        alert(err.error)
      });
  };

  return (
    <div>
      <Menu />
Lee Jin Ju's avatar
Lee Jin Ju committed
81
82
83
84
      <Container fluid>
        <Ta responsive="lg">
          <thead className="thead-light">
            
85
86
87
88
89
            <tr>
              <th>대표자</th>
              <th>날짜</th>
              <th>시간</th>
              <th>강의실</th>
Lee Jin Ju's avatar
Lee Jin Ju committed
90
91
              <th>사용 인원</th>
              <th>승인 여부</th>
92
93
94
            </tr>
          </thead>
          <tbody>
Ha YeaJin's avatar
Ha YeaJin committed
95
96
97
98
99
100
101
102
103
104
            {reserve != "" ? (
              reserve.map((reserve, index) => {
                return (
                  <tr key={index}>
                    <td>{reserve.user.name}</td>
                    <td>{reserve.date}</td>
                    <td>{reserve.starttime}~{(Number(reserve.starttime) + reserve.usetime)}</td>
                    <td>{reserve.room}</td>
                    <td>{reserve.num}</td>
                    <td>
Lee Jin Ju's avatar
Lee Jin Ju committed
105
                      <button onClick={() => admit(index)} className="btn btn-primary btn-sm">
Ha YeaJin's avatar
Ha YeaJin committed
106
                        승인
107
                    </button>
Lee Jin Ju's avatar
Lee Jin Ju committed
108
                      <button onClick={() => remove(index)} className="btn btn-danger btn-sm">
Ha YeaJin's avatar
Ha YeaJin committed
109
                        거절
110
                    </button>
Ha YeaJin's avatar
Ha YeaJin committed
111
112
113
114
                    </td>
                  </tr>
                )
              })) : <div>최근 대관 신청 내역이 없습니다.</div>}
115
          </tbody>
Lee Jin Ju's avatar
Lee Jin Ju committed
116
117
        </Ta>
      </Container>
118
119
120
121
    </div>
  )
}

Ha YeaJin's avatar
Ha YeaJin committed
122
export default ACheck