WritePage.js 4.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
56
57
58
59
60
61
62
63
64
65
66
67
68
                                // <Form onSubmit={handleSubmit}>
                                //     <Form.Group>
                                //         <Form.Label variant={touched.name && errors.name ? "danger" : ""}>제목</Form.Label>
                                //         <Form.Control className={(touched.name && errors.name ? 'form-control is-invalid' : "form-control")}
                                //             type="text"
                                //             placeholder="제목"
                                //             title="title"
                                //             {...getFieldProps('title')} />
                                //     </Form.Group>
                                //     <Form.Group>
                                //         <Form.Label>내용</Form.Label>
                                //         <Form.Control as="textarea" rows={10} />
                                //     </Form.Group>
                                // </Form>
Ha YeaJin's avatar
Ha YeaJin committed
69

Ha YeaJin's avatar
Ha YeaJin committed
70
                                <form onSubmit={handleSubmit} className="d-flex flex-column">
Ha YeaJin's avatar
Ha YeaJin committed
71
72
73
74
75
76
77
78
                                    <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
79

Ha YeaJin's avatar
Ha YeaJin committed
80
81
                                    <div className="form-group ">
                                        <div className={touched.name && errors.name ? "text-danger" : ""}>내용</div>
Ha YeaJin's avatar
Ha YeaJin committed
82
83
                                        <Field as="textarea" rows={8} style={{ "min-width": "100%" }}
                                        {...getFieldProps('content')} />
Ha YeaJin's avatar
Ha YeaJin committed
84
                                    </div>
Ha YeaJin's avatar
Ha YeaJin committed
85
                                    <Button className="mb-2" variant="dark" type="submit" disabled={isSubmitting}>공지 등록</Button>
Ha YeaJin's avatar
Ha YeaJin committed
86
87
88
89
90
91
92
93
94
                                </form>
                            )}
                        </Formik>
                    </Col>
                </Row>
            </Container>
        </div>
    )
}
Ha YeaJin's avatar
Ha YeaJin committed
95

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