ACheckPage.js 3.33 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)
      });
  }
50
51
  if (state) return <Redirect to="/home" />;

52
  function remove(index) {
Ha YeaJin's avatar
Ha YeaJin committed
53
54
55
56
57
58
59
60
61
62
    // 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}`)
63
64
      .then(res => {
        if (res.status === 404) return alert(res.data.error)
Ha YeaJin's avatar
Ha YeaJin committed
65
        alert("승인을 거절했습니다!");
66
67
68
69
70
71
72
73
        getReserve();
      })
      .catch(err => {
        alert(err.error)
      });
  };

  function admit(index) {
Ha YeaJin's avatar
Ha YeaJin committed
74
75
76
    axios.put(`/reserves/${reserve[index]._id}`, {
      approve: true,
    })
77
78
79
80
81
82
83
84
85
86
87
88
89
      .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
90
91
92
93
      <Container fluid>
        <Ta responsive="lg">
          <thead className="thead-light">
            
94
95
96
97
98
            <tr>
              <th>대표자</th>
              <th>날짜</th>
              <th>시간</th>
              <th>강의실</th>
Lee Jin Ju's avatar
Lee Jin Ju committed
99
100
              <th>사용 인원</th>
              <th>승인 여부</th>
101
102
103
            </tr>
          </thead>
          <tbody>
Ha YeaJin's avatar
Ha YeaJin committed
104
105
106
107
108
109
110
111
112
113
            {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
114
                      <button onClick={() => admit(index)} className="btn btn-primary btn-sm">
Ha YeaJin's avatar
Ha YeaJin committed
115
                        승인
116
                    </button>
Lee Jin Ju's avatar
Lee Jin Ju committed
117
                      <button onClick={() => remove(index)} className="btn btn-danger btn-sm">
Ha YeaJin's avatar
Ha YeaJin committed
118
                        거절
119
                    </button>
Ha YeaJin's avatar
Ha YeaJin committed
120
121
122
123
                    </td>
                  </tr>
                )
              })) : <div>최근 대관 신청 내역이 없습니다.</div>}
124
          </tbody>
Lee Jin Ju's avatar
Lee Jin Ju committed
125
126
        </Ta>
      </Container>
127
128
129
130
    </div>
  )
}

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