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 Modify({ match }) {
    const [state, setState] = useState(false);
    const [modification, setModification] = useState({ title: "", content: "" });
    const [isadmin, setIsadmin] = useState({ ok: "" });
    const [user, setUser] = useState({ name: "", role: "" })
    useEffect(() => {
        acheck();
        getOne(match.params.id);
    }, [])
    if (isadmin.ok === "no") return ;
    if (state) {
        return ;
    }
    function getOne(id) {
        if (id) {
            axios.get(`/app/rental/api/notices/${match.params.id}`)
                .then(res => {
                    if (res.status !== 201) {
                        alert(res.data.error);
                    }
                    setModification({ title: res.data.notice_title, content: res.data.notice_content })
                })
                .catch(err => {
                    alert(err.error)
                });
        }
    };
    function acheck() {
        axios.get(`/app/rental/api/users/admin/${localStorage.getItem('_id')}`, {
            headers: { authorization: localStorage.getItem('token') },
        })
            .then(res => {
                if (res.status !== 201) {
                    alert(res.data.error);
                    setIsadmin({ ok: "no" });
                }
                setUser({ name: res.data.name, role: res.data.role })
            }).catch(err => {
                alert(err.error)
            });
    }
    return (
        
            
            
                {console.log(modification)}
                
                    
                         {
                                axios({
                                    method: 'put',
                                    url: `/app/rental/api/writes/${match.params.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 Modify