ACheckPage.js 3.03 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';
5
6
7
import 'bootstrap/dist/css/bootstrap.css';

function ACheck(props) {
8
  const [state, setState] = useState()
9
10
  const [reserve, setReserve] = useState([]);
  useEffect(() => {
Ha YeaJin's avatar
Ha YeaJin committed
11
    getReserve();
12
13
14
  }, [])

  function getReserve() {
Ha YeaJin's avatar
Ha YeaJin committed
15
16
    axios.get(`/users/admin/${props.match.params.id}`, {
      headers: { authorization: localStorage.getItem('token') },
17
18
    })
      .then(res => {
19
20
21
        if (res.status === 404) {
          alert(res.data.error)
          setState(true);
22
23
24
25
26
27
28
29
        }
        console.log(res.data);
        setReserve(res.data);
      })
      .catch(err => {
        alert(err.error)
      });
  }
30
31
  if (state) return <Redirect to="/home" />;

32
  function remove(index) {
Ha YeaJin's avatar
Ha YeaJin committed
33
34
35
36
37
38
39
40
41
42
    // axios.delete(`/reserves/${reserve[index]._id}`)
    //   .then(res => {
    //     if (res.status === 404) return alert(res.data.error)
    //     alert("삭제되었습니다!");
    //     getReserve();
    //   })
    //   .catch(err => {
    //     alert(err.error)
    //   });
    axios.put(`/reserves/${reserve[index]._id}`)
43
44
      .then(res => {
        if (res.status === 404) return alert(res.data.error)
Ha YeaJin's avatar
Ha YeaJin committed
45
        alert("승인을 거절했습니다!");
46
47
48
49
50
51
52
53
        getReserve();
      })
      .catch(err => {
        alert(err.error)
      });
  };

  function admit(index) {
Ha YeaJin's avatar
Ha YeaJin committed
54
55
56
    axios.put(`/reserves/${reserve[index]._id}`, {
      approve: true,
    })
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
      .then(res => {
        if (res.status === 404) return alert(res.data.error)
        alert("승인되었습니다!");
        getReserve();
      })
      .catch(err => {
        alert(err.error)
      });
  };

  return (
    <div>
      <Menu />
      <div className="">
        <table className="table">
          <thead>
            <tr>
              <th>대표자</th>
              <th>날짜</th>
              <th>시간</th>
              <th>강의실</th>
              <th>사용인원</th>
              <th>승인여부</th>
            </tr>
          </thead>
          <tbody>
Ha YeaJin's avatar
Ha YeaJin committed
83
84
85
86
87
88
89
90
91
92
93
94
            {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>
                      <button onClick={() => admit(index)} className="btn btn-primary">
                        승인
95
                    </button>
Ha YeaJin's avatar
Ha YeaJin committed
96
97
                      <button onClick={() => remove(index)} className="btn btn-danger">
                        거절
98
                    </button>
Ha YeaJin's avatar
Ha YeaJin committed
99
100
101
102
                    </td>
                  </tr>
                )
              })) : <div>최근 대관 신청 내역이 없습니다.</div>}
103
104
105
106
107
108
109
110
          </tbody>
        </table>

      </div>
    </div>
  )
}

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