ACheckPage.js 2.7 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

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

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

31
  function remove(index) {
Ha YeaJin's avatar
Ha YeaJin committed
32
    axios.put(`/reserves/${reserve[index]._id}`)
33
34
      .then(res => {
        if (res.status === 404) return alert(res.data.error)
Ha YeaJin's avatar
Ha YeaJin committed
35
        alert("승인을 거절했습니다!");
36
37
38
39
40
41
42
43
        getReserve();
      })
      .catch(err => {
        alert(err.error)
      });
  };

  function admit(index) {
Ha YeaJin's avatar
Ha YeaJin committed
44
45
46
    axios.put(`/reserves/${reserve[index]._id}`, {
      approve: true,
    })
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
      .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
73
74
75
76
77
78
79
80
81
82
83
84
            {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">
                        승인
85
                    </button>
Ha YeaJin's avatar
Ha YeaJin committed
86
87
                      <button onClick={() => remove(index)} className="btn btn-danger">
                        거절
88
                    </button>
Ha YeaJin's avatar
Ha YeaJin committed
89
90
91
92
                    </td>
                  </tr>
                )
              })) : <div>최근 대관 신청 내역이 없습니다.</div>}
93
94
95
96
97
98
99
100
          </tbody>
        </table>

      </div>
    </div>
  )
}

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