WritePage.js 6.4 KB
Newer Older
Ha YeaJin's avatar
Ha YeaJin committed
1
import React, { useState, useEffect } from 'react';
2
import { Redirect } from 'react-router-dom';
Ha YeaJin's avatar
Ha YeaJin committed
3
4
5
import Menu from '../Components/Menu';
import * as Yup from 'yup';
import axios from 'axios';
Ha YeaJin's avatar
Ha YeaJin committed
6
import { Container, Row, Col, Button } from 'react-bootstrap';
Ha YeaJin's avatar
Ha YeaJin committed
7
8
import { Field, Formik } from 'formik';

9
function Write({ match }) {
Ha YeaJin's avatar
Ha YeaJin committed
10
    const [state, setState] = useState(false);
11
12
13
14
15
16
17
18
19
    const [revision, setRevision] = useState({ title: '', content: '' });
    // const [isadmin, setIsadmin] = useState({ok:""});
    // const [user, setUser] = useState({ role: "" })

    // if (isadmin.ok==="no") return <Redirect to="/" />;

    useEffect(() => {
        getOne(match.params.id)
    }, [])
Ha YeaJin's avatar
Ha YeaJin committed
20
21
22
23

    if (state) {
        return <Redirect to="/notice" />;
    }
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

    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)
    //         });  
    // }

Ha YeaJin's avatar
Ha YeaJin committed
63
64
    return (
        <div>
65
66
            {console.log("revision", revision.title, revision.content)}
            <Menu/>
Ha YeaJin's avatar
Ha YeaJin committed
67
68
            <Container fluid>
                <Row className="justify-content-center">
Ha YeaJin's avatar
Ha YeaJin committed
69
                    <Col md={5} xs={11} className="pt-3" >
Ha YeaJin's avatar
Ha YeaJin committed
70
                        <Formik
71
                            initialValues={{title: revision.title, content: revision.content}}
Ha YeaJin's avatar
Ha YeaJin committed
72
73
74
75
76
77
78
                            validationSchema={Yup.object({
                                title: Yup.string()
                                    .required('제목을 입력해주세요.'),
                                content: Yup.string()
                                    .required('내용을 입력해주세요.'),
                            })}
                            onSubmit={(values, { setSubmitting }) => {
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
                                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)
                                        });
                                }
Ha YeaJin's avatar
Ha YeaJin committed
107
108
109
110
111
112
113
114
115
116
117
                                setTimeout(() => {
                                    setSubmitting(false);
                                }, 400);  // finish the cycle in handler
                            }}
                        >{({
                            errors,
                            touched,
                            handleSubmit,
                            getFieldProps,  // contain values, handleChange, handleBlur
                            isSubmitting,
                        }) => (
Ha YeaJin's avatar
Ha YeaJin committed
118
                                <form onSubmit={handleSubmit} className="d-flex flex-column">
Ha YeaJin's avatar
Ha YeaJin committed
119
120
121
122
123
124
125
126
                                    <div className="form-group">
                                        <div className={touched.name && errors.name ? "text-danger" : ""}>제목</div>
                                        <input className={(touched.name && errors.name ? 'form-control is-invalid' : "form-control")}
                                            type="text"
                                            title="title"
                                            {...getFieldProps('title')}
                                            placeholder="제목" />
                                    </div>
127
128
                                    {console.log(revision.title)}
                                    <div className="form-group">
Ha YeaJin's avatar
Ha YeaJin committed
129
                                        <div className={touched.name && errors.name ? "text-danger" : ""}>내용</div>
Ha YeaJin's avatar
Ha YeaJin committed
130
                                        <Field as="textarea" rows={8} style={{ "min-width": "100%" }}
131
                                            {...getFieldProps('content')} />
Ha YeaJin's avatar
Ha YeaJin committed
132
                                    </div>
133
                                    <Button className="mb-2" variant="dark" type="submit" disabled={isSubmitting}>{match.params.id ? "공지 수정" : "공지 등록"}</Button>
Ha YeaJin's avatar
Ha YeaJin committed
134
135
136
137
138
139
140
141
142
                                </form>
                            )}
                        </Formik>
                    </Col>
                </Row>
            </Container>
        </div>
    )
}
Ha YeaJin's avatar
Ha YeaJin committed
143

Ha YeaJin's avatar
Ha YeaJin committed
144
export default Write