NoticePage.js 2.89 KB
Newer Older
1
import React, { useState, useEffect, useRef } from 'react';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
2
import Menu from '../Components/Menu';
3
import axios from 'axios';
Ha YeaJin's avatar
Ha YeaJin committed
4
5
import { Link } from 'react-router-dom';
import { Container, Row, Col, Card, Accordion, Button } from 'react-bootstrap';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
6
7

function Notice() {
8
    const [notices, setNotices] = useState([]);
Choi Ga Young's avatar
Choi Ga Young committed
9
    const [user, setUser] = useState({ role: "" })
10
11

    useEffect(() => {
Choi Ga Young's avatar
Choi Ga Young committed
12
        acheck();
13
14
15
        getNotice();
    }, []);

Choi Ga Young's avatar
Choi Ga Young committed
16
17
18
19
20
21
22
23
24
25
26
    function acheck() {
        axios.get(`/users/${localStorage.getItem('_id')}`)
            .then(res => {
                if (res.data.role == "admin") {
                    setUser(res.data)
                }
            }).catch(err => {
                alert(err.error)
            });
    }

27
28
29
30
31
32
33
34
35
36
37
38
39
    function dateForm(day) {
        const post_day = new Date(day);
        let year = post_day.getFullYear();
        let month = post_day.getMonth() + 1;
        let date = post_day.getDate();

        month = month < 10 ? '0' + month : month;
        date = date < 10 ? '0' + date : date;

        const new_date = year + "-" + month + "-" + date;
        return new_date
    }

40
41
42
43
    function getNotice() {
        axios.get(`/notices`)
            .then(res => {
                if (res.status !== 201) {
Ha YeaJin's avatar
Ha YeaJin committed
44
                    // alert(res.data.error);
45
46
47
48
49
50
51
                }
                setNotices(res.data);
            })
            .catch(err => {
                alert(err.error)
            });
    }
Ha YeaJin's avatar
pages    
Ha YeaJin committed
52
53
54
    return (
        <div>
            <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}>
Choi Ga Young's avatar
Choi Ga Young committed
58
59
60
61
62
63
64
65
66
                        <div className="p-3 border-bottom justify-content space-between">
                            <div>
                                <h2>공지사항</h2>
                            </div>
                            <div>
                                {user.role === "admin" ? (
                                    <Link to="/write"> 작성</Link>) : null}
                            </div>
                        </div>
Ha YeaJin's avatar
Ha YeaJin committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
                        <Accordion>
                            {notices.map((notice, index) =>
                                <Card>
                                    <Card.Header>
                                        <Accordion.Toggle as={Button} variant="link" eventKey={index + 1}>{notice.notice_title} <span className="text-right">{dateForm(notice.post_date)}</span></Accordion.Toggle>
                                    </Card.Header>
                                    <Accordion.Collapse eventKey={index + 1}>
                                        <Card.Body>{notice.notice_content}</Card.Body>
                                    </Accordion.Collapse>
                                </Card>)}
                        </Accordion>
                    </Col>
                </Row>
            </Container>
Ha YeaJin's avatar
pages    
Ha YeaJin committed
81
82
83
84
        </div>
    )
}

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