NoticePage.js 2.25 KB
Newer Older
Kim, Subin's avatar
내꺼    
Kim, Subin committed
1
import React, { useState, useEffect } from 'react';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
2
import Menu from '../Components/Menu';
3
import axios from 'axios';
CHAERIN KIM's avatar
CHAERIN KIM committed
4
import { Link, Redirect } from 'react-router-dom';
Kim, Subin's avatar
내꺼    
Kim, Subin committed
5
import { Container, Row, Col, Card, Accordion, Button } from 'react-bootstrap';
Ha YeaJin's avatar
Ha YeaJin committed
6
import CARD from '../Components/Card';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
7
8

function Notice() {
Kim, Subin's avatar
내꺼    
Kim, Subin committed
9
    const [show, setShow] = useState(false);
10
    const [notices, setNotices] = useState([]);
Kim, Subin's avatar
내꺼    
Kim, Subin committed
11
    const [user, setUser] = useState({ role: "" })
CHAERIN KIM's avatar
CHAERIN KIM committed
12
    const [state, setState] = useState()
13
14

    useEffect(() => {
Kim, Subin's avatar
내꺼    
Kim, Subin committed
15
        acheck();
16
17
18
        getNotice();
    }, []);

CHAERIN KIM's avatar
CHAERIN KIM committed
19
20
    if (state) return <Redirect to="/" />;

Kim, Subin's avatar
내꺼    
Kim, Subin committed
21
    function acheck() {
CHAERIN KIM's avatar
CHAERIN KIM committed
22
23
24
        axios.get(`/users/${localStorage.getItem('_id')}`, {
            headers: { authorization: localStorage.getItem('token') },
        })
Kim, Subin's avatar
내꺼    
Kim, Subin committed
25
            .then(res => {
CHAERIN KIM's avatar
CHAERIN KIM committed
26
27
28
29
30
                if (res.status !== 201) {
                    alert(res.data.error);
                    localStorage.clear();
                    setState(true);
                }
Kim, Subin's avatar
내꺼    
Kim, Subin committed
31
32
33
34
35
36
37
38
                if (res.data.role == "admin") {
                    setUser(res.data)
                }
            }).catch(err => {
                alert(err.error)
            });
    }

39
40
41
42
    function getNotice() {
        axios.get(`/notices`)
            .then(res => {
                if (res.status !== 201) {
Ha YeaJin's avatar
Ha YeaJin committed
43
                    // alert(res.data.error);
44
45
46
47
48
49
50
                }
                setNotices(res.data);
            })
            .catch(err => {
                alert(err.error)
            });
    }
Kim, Subin's avatar
내꺼    
Kim, Subin committed
51

Ha YeaJin's avatar
pages    
Ha YeaJin committed
52
53
    return (
        <div>
Kim, Subin's avatar
내꺼    
Kim, Subin committed
54
            <Menu />
Ha YeaJin's avatar
Ha YeaJin committed
55
56
57
            <Container fluid>
                <Row className="justify-content-center vw-100 vh-90">
                    <Col md={7}>
Ha YeaJin's avatar
Ha YeaJin committed
58
59
                        <h2 className="p-3 border-bottom d-flex justify-content-between">공지사항 {user.role === "admin" ? (
                            <Button as={Link} to="/write"> 작성</Button>) : null}</h2>
Ha YeaJin's avatar
Ha YeaJin committed
60
                        <Accordion>
Ha YeaJin's avatar
Ha YeaJin committed
61
62
                            {notices.map((notice, index) => <CARD card_index={index} title={notice.notice_title} date={notice.post_date} content={notice.notice_content} />
                            )}
Ha YeaJin's avatar
Ha YeaJin committed
63
64
65
66
                        </Accordion>
                    </Col>
                </Row>
            </Container>
Ha YeaJin's avatar
pages    
Ha YeaJin committed
67
68
69
70
        </div>
    )
}

Lee Jin Ju's avatar
Lee Jin Ju committed
71
export default Notice;