NoticePage.js 3.39 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';
CHAERIN KIM's avatar
CHAERIN KIM committed
4
import { Link, Redirect } 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: "" })
CHAERIN KIM's avatar
CHAERIN KIM committed
11
    const [state, setState] = useState()
12
13

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

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

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

38
39
40
41
42
43
44
45
46
47
48
49
50
    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
    }

51
52
53
54
    function getNotice() {
        axios.get(`/notices`)
            .then(res => {
                if (res.status !== 201) {
Ha YeaJin's avatar
Ha YeaJin committed
55
                    // alert(res.data.error);
56
57
58
59
60
61
62
                }
                setNotices(res.data);
            })
            .catch(err => {
                alert(err.error)
            });
    }
63

Ha YeaJin's avatar
pages    
Ha YeaJin committed
64
65
    return (
        <div>
66
            <Menu />
Ha YeaJin's avatar
Ha YeaJin committed
67
68
69
            <Container fluid>
                <Row className="justify-content-center vw-100 vh-90">
                    <Col md={7}>
70
71
72
73
74
                        <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
75
76
77
                        <Accordion>
                            {notices.map((notice, index) =>
                                <Card>
78
79
80
                                    <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
81
82
                                    </Card.Header>
                                    <Accordion.Collapse eventKey={index + 1}>
83
                                        <Card.Body><pre>{notice.notice_content}</pre></Card.Body>
Ha YeaJin's avatar
Ha YeaJin committed
84
85
86
87
88
89
                                    </Accordion.Collapse>
                                </Card>)}
                        </Accordion>
                    </Col>
                </Row>
            </Container>
Ha YeaJin's avatar
pages    
Ha YeaJin committed
90
91
92
93
        </div>
    )
}

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