ACheckPage.js 2.54 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(() => {
11
    getReserve(); 
12
13
14
  }, [])

  function getReserve() {
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
33
34
35
36
37
38
39
40
41
42
43
44
  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)
      });
  };

  function admit(index) {
45
    axios.put(`/reserves/${reserve[index]._id}`)
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
      .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>
            {reserve.map((reserve, index) => {
              return (
                <tr key={index}>
75
                  <td>{reserve.user.name}</td>
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
                  <td>{reserve.date}</td>
                  <td>{reserve.time}</td>
                  <td>{reserve.room}</td>
                  <td>{reserve.num}</td>
                  <td>
                    <button onClick={() => admit(index)} className="btn btn-primary">
                      승인
                    </button>
                    <button onClick={() => remove(index)} className="btn btn-danger">
                      거절
                    </button>
                  </td>
                </tr>
              )
            })}
          </tbody>
        </table>

      </div>
    </div>
  )
}

export default ACheck