NoticePage.js 3.04 KB
Newer Older
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';
Ha YeaJin's avatar
Ha YeaJin committed
4
import { Link } from 'react-router-dom';
5
import { Container, Row, Col, Card, Accordion, Button } from 'react-bootstrap';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
6
7

function Notice() {
8
    const [show, setShow] = useState(false);
9
    const [notices, setNotices] = useState([]);
10
    const [user, setUser] = useState({ role: "" })
11
12

    useEffect(() => {
13
        acheck();
14
15
16
        getNotice();
    }, []);

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

28
29
30
31
32
33
34
35
36
37
38
39
40
    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
    }

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

Ha YeaJin's avatar
pages    
Ha YeaJin committed
54
55
    return (
        <div>
56
            <Menu />
Ha YeaJin's avatar
Ha YeaJin committed
57
58
59
            <Container fluid>
                <Row className="justify-content-center vw-100 vh-90">
                    <Col md={7}>
60
61
62
63
64
                        <div className="px-3 pt-3 mb-3 border-bottom d-flex justify-content-between align-items-end">
                            <h2>공지사항</h2>
                            {user.role === "admin" ? (
                                <Link to="/write"> 작성</Link>) : null}
                        </div>
Ha YeaJin's avatar
Ha YeaJin committed
65
66
67
                        <Accordion>
                            {notices.map((notice, index) =>
                                <Card>
68
69
70
                                    <Card.Header className="d-flex justify-content-between">
                                        <Accordion.Toggle as={Button} variant="link" eventKey={index + 1} className={"d-inline-block " + (show ? "text-wrap" : "text-truncate")} onClick={() => setShow(!show)}>{notice.notice_title}</Accordion.Toggle>
                                        <span className="d-flex align-items-center" style={{ width: "50%" }}>{dateForm(notice.post_date)}</span>
Ha YeaJin's avatar
Ha YeaJin committed
71
72
                                    </Card.Header>
                                    <Accordion.Collapse eventKey={index + 1}>
73
                                        <Card.Body><pre>{notice.notice_content}</pre></Card.Body>
Ha YeaJin's avatar
Ha YeaJin committed
74
75
76
77
78
79
                                    </Accordion.Collapse>
                                </Card>)}
                        </Accordion>
                    </Col>
                </Row>
            </Container>
Ha YeaJin's avatar
pages    
Ha YeaJin committed
80
81
82
83
        </div>
    )
}

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