WritePage.js 3.77 KB
Newer Older
Ha YeaJin's avatar
Ha YeaJin committed
1
2
3
4
5
import React, { useState, useEffect } from 'react';
import { Link, Redirect } from 'react-router-dom';
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
9
10
11
12
13
14
15
16
17
18
19
20
import { Field, Formik } from 'formik';

function Write() {
    const [submitData, setSubmitData] = useState(false);
    const [state, setState] = useState(false);

    if (state) {
        return <Redirect to="/notice" />;
    }
    return (
        <div>
            <Menu />
            <Container fluid>
                <Row className="justify-content-center">
Ha YeaJin's avatar
Ha YeaJin committed
21
                    <Col md={5} xs={11} className="pt-3" >
Ha YeaJin's avatar
Ha YeaJin committed
22
23
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
                        <Formik
                            initialValues={{ title: '', content: '' }}
                            validationSchema={Yup.object({
                                title: Yup.string()
                                    .required('제목을 입력해주세요.'),
                                content: Yup.string()
                                    .required('내용을 입력해주세요.'),
                            })}
                            onSubmit={(values, { setSubmitting }) => {
                                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)
                                    });

                                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
55
                                <form onSubmit={handleSubmit} className="d-flex flex-column">
Ha YeaJin's avatar
Ha YeaJin committed
56
57
58
59
60
61
62
63
                                    <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>
Ha YeaJin's avatar
Ha YeaJin committed
64

Ha YeaJin's avatar
Ha YeaJin committed
65
66
                                    <div className="form-group ">
                                        <div className={touched.name && errors.name ? "text-danger" : ""}>내용</div>
Ha YeaJin's avatar
Ha YeaJin committed
67
68
                                        <Field as="textarea" rows={8} style={{ "min-width": "100%" }}
                                        {...getFieldProps('content')} />
Ha YeaJin's avatar
Ha YeaJin committed
69
                                    </div>
Ha YeaJin's avatar
Ha YeaJin committed
70
                                    <Button className="mb-2" variant="dark" type="submit" disabled={isSubmitting}>공지 등록</Button>
Ha YeaJin's avatar
Ha YeaJin committed
71
72
73
74
75
76
77
78
79
                                </form>
                            )}
                        </Formik>
                    </Col>
                </Row>
            </Container>
        </div>
    )
}
Ha YeaJin's avatar
Ha YeaJin committed
80

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