ModifyPage.js 5.23 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 <Redirect to="/" />;

    if (state) {
        return <Redirect to="/notice" />;
    }

    function getOne(id) {
        if (id) {
Yoon, Daeki's avatar
Yoon, Daeki committed
28
            axios.get(`/api/notices/${match.params.id}`)
Yoon, Daeki's avatar
Yoon, Daeki committed
29
30
31
32
33
34
35
36
37
38
39
40
41
                .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() {
Yoon, Daeki's avatar
Yoon, Daeki committed
42
        axios.get(`/api/users/admin/${localStorage.getItem('_id')}`, {
Yoon, Daeki's avatar
Yoon, Daeki committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
            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 (
        <div>
            <Menu />
            <Container fluid>
                {console.log(modification)}
                <Row className="justify-content-center">
                    <Col md={5} xs={11} className="pt-3" >
                        <Formik
                            initialValues={{ name: user.name, title: modification.title, content: modification.content }}
                            enableReinitialize={true}
                            validationSchema={Yup.object({
                                title: Yup.string()
                                    .required('제목을 입력해주세요.'),
                                content: Yup.string()
                                    .required('내용을 입력해주세요.'),
                            })}
                            onSubmit={(values, { setSubmitting }) => {
                                axios({
                                    method: 'put',
Yoon, Daeki's avatar
Yoon, Daeki committed
76
                                    url: `/api/writes/${match.params.id}`,
Yoon, Daeki's avatar
Yoon, Daeki committed
77
78
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
                                    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,
                        }) => (
                                <form onSubmit={handleSubmit} className="d-flex flex-column">
                                    <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')}
                                            disabled />
                                    </div>
                                    <div className="form-group">
                                        <div className={touched.name && errors.name ? "text-danger" : ""}>내용</div>
                                        <Field as="textarea" rows={8} style={{ "min-width": "100%" }}
                                            {...getFieldProps('content')} />
                                    </div>
                                    <Button className="mb-2" variant="dark" type="submit" disabled={isSubmitting}>공지 수정</Button>
                                </form>
                            )}
                        </Formik>
                    </Col>
                </Row>
            </Container>
        </div>
    )
}

export default Modify