NoticePage.js 2.64 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import React, { useState, useEffect } from 'react';
import Menu from '../Components/Menu';
import axios from 'axios';
import { Link, Redirect } from 'react-router-dom';
import { Container, Row, Col, Accordion, Button } from 'react-bootstrap';
import CARD from '../Components/Card';

function Notice() {
    const [notices, setNotices] = useState([]);
    const [user, setUser] = useState({ role: "" })
    const [state, setState] = useState()

    useEffect(() => {
        acheck();
        getNotice();
    }, []);

    if (state) return <Redirect to="/" />;

    function acheck() {
Yoon, Daeki's avatar
Yoon, Daeki committed
21
        axios.get(`/api/users/${localStorage.getItem('_id')}`, {
Yoon, Daeki's avatar
Yoon, Daeki committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
            headers: { authorization: localStorage.getItem('token') },
        })
            .then(res => {
                if (res.status !== 201) {
                    alert(res.data.error);
                    localStorage.clear();
                    setState(true);
                }
                if (res.data.role == "admin") {
                    setUser(res.data)
                }
            }).catch(err => {
                alert(err.error)
            });
    }
Yoon, Daeki's avatar
reload    
Yoon, Daeki committed
37
38
39
40
41
42
43
44
45
46
47
48
49
    
    function remove (card_id) {
      axios.delete(`/api/notices/${card_id}`)
          .then(res => {
              if (res.status === 404) return alert(res.data.error)
              alert("삭제되었습니다!");
              getNotice();
          })
          .catch(err => {
              alert(err.error)
          });
  }

Yoon, Daeki's avatar
Yoon, Daeki committed
50
51

    function getNotice() {
Yoon, Daeki's avatar
Yoon, Daeki committed
52
        axios.get(`/api/notices`)
Yoon, Daeki's avatar
Yoon, Daeki committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
            .then(res => {
              console.log('res.data=', res.data)
                if (res.status !== 201) {
                    alert(res.data.error);
                }
                setNotices(res.data);
            })
            .catch(err => {
                alert(err.error)
            });
    }

    return (
        <div>
            <Menu />
            <Container fluid>
                <Row className="justify-content-center vw-100 vh-90">
                    <Col md={7}>
                        <h2 className="p-3 border-bottom d-flex justify-content-between">공지사항 {user.role === "admin" ? (
                            <Button as={Link} to="/write"> 작성</Button>) : null}</h2>
                        <Accordion>
Yoon, Daeki's avatar
reload    
Yoon, Daeki committed
74
                            {notices.map((notice, index) => <CARD card_id={notice._id} card_index={index} title={notice.notice_title} date={notice.post_date} content={notice.notice_content} admin={user.role} remove={remove} />
Yoon, Daeki's avatar
Yoon, Daeki committed
75
76
77
78
79
80
81
82
83
84
                            )}
                        </Accordion>
                    </Col>
                </Row>
            </Container>
        </div>
    )
}

export default Notice