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, Card, Accordion, Button } from 'react-bootstrap';
function Notice() {
const [show, setShow] = useState(false);
const [notices, setNotices] = useState([]);
const [user, setUser] = useState({ role: "" })
const [state, setState] = useState()
useEffect(() => {
acheck();
getNotice();
}, []);
if (state) return ;
function acheck() {
axios.get(`/users/${localStorage.getItem('_id')}`, {
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)
});
}
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
}
function getNotice() {
axios.get(`/notices`)
.then(res => {
if (res.status !== 201) {
// alert(res.data.error);
}
setNotices(res.data);
})
.catch(err => {
alert(err.error)
});
}
return (
공지사항
{user.role === "admin" ? (
글 작성) : null}
{notices.map((notice, index) =>
setShow(!show)}>{notice.notice_title}
{dateForm(notice.post_date)}
{notice.notice_content}
)}
)
}
export default Notice;