import React, { useState, useEffect } from 'react';
import { Redirect } from 'react-router-dom';
import Menu from '../Components/Menu';
import * as Yup from 'yup';
import axios from 'axios';
import { Container, Row, Col, Button } from 'react-bootstrap';
import { Field, Formik } from 'formik';
function Write({ match }) {
const [state, setState] = useState(false);
const [revision, setRevision] = useState({ title: '', content: '' });
// const [isadmin, setIsadmin] = useState({ok:""});
// const [user, setUser] = useState({ role: "" })
// if (isadmin.ok==="no") return ;
useEffect(() => {
getOne(match.params.id)
}, [])
if (state) {
return ;
}
function getOne (id) {
if (id) {
axios.get(`/notices/${id}`)
.then(res => {
if (res.status !== 201) {
alert(res.data.error);
}
setRevision({ title: res.data.notice_title, content: res.data.notice_content })
})
.catch(err => {
alert(err.error)
});
}
};
// function putData (match, revision) {
// if (!match.params.id) return { title: '', content: '' }
// else return { title: revision.notice_title, content: revision.notice_content }
// };
// 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();
// setIsadmin({ok:"no"});
// }
// if (res.data.role == "admin") {
// setUser(res.data)
// }
// }).catch(err => {
// alert(err.error)
// });
// }
return (
{console.log("revision", revision.title, revision.content)}
{
if (!match.params.id) {
axios({
method: 'post',
url: '/writes',
data: values,
}).then(res => {
if (res.status === 404) return alert(res.data.error)
alert("공지 등록이 완료되었습니다.")
setState(true);
})
.catch(err => {
alert(err.error)
});
} else {
axios({
method: 'put',
url: '/writes/${id}',
data: values,
})
.then(res => {
if (res.status === 404) return alert(res.data.error)
alert("공지 수정이 완료되었습니다.")
setState(true);
})
.catch(err => {
alert(err.error)
});
}
setTimeout(() => {
setSubmitting(false);
}, 400); // finish the cycle in handler
}}
>{({
errors,
touched,
handleSubmit,
getFieldProps, // contain values, handleChange, handleBlur
isSubmitting,
}) => (
)}
)
}
export default Write