WritePage.js 4.56 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';

Kim, Subin's avatar
Kim, Subin committed
9
function Write() {
Ha YeaJin's avatar
Ha YeaJin committed
10
    const [state, setState] = useState(false);
Kim, Subin's avatar
Kim, Subin committed
11
12
    const [isadmin, setIsadmin] = useState({ ok: "" });
    const [user, setUser] = useState({ name: "" })
13
14

    useEffect(() => {
Kim, Subin's avatar
Kim, Subin committed
15
        acheck();
16
    }, [])
Ha YeaJin's avatar
Ha YeaJin committed
17

Kim, Subin's avatar
Kim, Subin committed
18
19
    if (isadmin.ok === "no") return <Redirect to="/" />;

Ha YeaJin's avatar
Ha YeaJin committed
20
21
22
    if (state) {
        return <Redirect to="/notice" />;
    }
23

Kim, Subin's avatar
Kim, Subin committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    function acheck() {
        axios.get(`/users/admin/${localStorage.getItem('_id')}`, {
            headers: { authorization: localStorage.getItem('token') },
        })
            .then(res => {
                if (res.status !== 201) {
                    alert(res.data.error);
                    console.log(res.data)
                    setIsadmin({ ok: "no" });
                }
                setUser({ name: res.data.name })
            }).catch(err => {
                alert(err.error)
            });
    }
39

Ha YeaJin's avatar
Ha YeaJin committed
40
41
    return (
        <div>
Kim, Subin's avatar
Kim, Subin committed
42
            <Menu />
Ha YeaJin's avatar
Ha YeaJin committed
43
44
            <Container fluid>
                <Row className="justify-content-center">
Ha YeaJin's avatar
Ha YeaJin committed
45
                    <Col md={5} xs={11} className="pt-3" >
Ha YeaJin's avatar
Ha YeaJin committed
46
                        <Formik
Kim, Subin's avatar
Kim, Subin committed
47
48
                            initialValues={{ name: user.name, title: '', content: '' }}
                            enableReinitialize={true}
Ha YeaJin's avatar
Ha YeaJin committed
49
50
51
52
53
54
55
                            validationSchema={Yup.object({
                                title: Yup.string()
                                    .required('제목을 입력해주세요.'),
                                content: Yup.string()
                                    .required('내용을 입력해주세요.'),
                            })}
                            onSubmit={(values, { setSubmitting }) => {
Kim, Subin's avatar
Kim, Subin committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
                                console.log(values)
                                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)
                                    });

Ha YeaJin's avatar
Ha YeaJin committed
70
71
72
73
74
75
76
77
78
79
80
                                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
81
                                <form onSubmit={handleSubmit} className="d-flex flex-column">
Ha YeaJin's avatar
Ha YeaJin committed
82
83
84
85
86
87
88
89
                                    <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>
90
                                    <div className="form-group">
Ha YeaJin's avatar
Ha YeaJin committed
91
                                        <div className={touched.name && errors.name ? "text-danger" : ""}>내용</div>
Ha YeaJin's avatar
Ha YeaJin committed
92
                                        <Field as="textarea" rows={8} style={{ "min-width": "100%" }}
93
                                            {...getFieldProps('content')} />
Ha YeaJin's avatar
Ha YeaJin committed
94
                                    </div>
Kim, Subin's avatar
Kim, Subin committed
95
                                    <Button className="mb-2" variant="dark" type="submit" disabled={isSubmitting}>공지 등록</Button>
Ha YeaJin's avatar
Ha YeaJin committed
96
97
98
99
100
101
102
103
104
                                </form>
                            )}
                        </Formik>
                    </Col>
                </Row>
            </Container>
        </div>
    )
}
Ha YeaJin's avatar
Ha YeaJin committed
105

Kim, Subin's avatar
Kim, Subin committed
106
export default Write