SignupPage.js 9.8 KB
Newer Older
Ha YeaJin's avatar
pages    
Ha YeaJin committed
1
import React, { useState } from 'react';
2
import { Field, Formik } from 'formik';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
3
4
5
6
import * as Yup from 'yup';
import axios from 'axios';
import 'bootstrap/dist/css/bootstrap.css';
import { Link, Redirect } from 'react-router-dom';
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import styled from 'styled-components';

const Nav = styled.nav`
    background-color: #7B031D;
    height: 10vh;

    a {
        color: #ffffff;
    }
`

const Wow = styled.div`
    height: 90vh;

    & #reCheck::after {
        content: '비밀번호를 다시 입력하세요';
    }

    & #reCheck:not(.right) {
        content: '비밀번호가 다릅니다.';
        color: red;
    }

    & .asd {
        display: flex;
        flex-direction: column;
    }
`
Ha YeaJin's avatar
pages    
Ha YeaJin committed
35
36

function Signup() {
37
    const [state, setState] = useState(false);
38
    const [checkPw, setCheckPw] = useState(true);
Ha YeaJin's avatar
pages    
Ha YeaJin committed
39

40
    if (state) {
41
        return <Redirect to="/login" />;
42
    }
Ha YeaJin's avatar
pages    
Ha YeaJin committed
43
44

    return (
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
76
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
        <div className="vh-100">
            <Nav className="navbar sticky-top navbar-expand-md">
                <a class="navbar-brand" href="#">회원가입</a>
            </Nav>
            <div className="container-fluid">
                <Wow className="row justify-content-center rrooww">
                    <div className="col-md-4 col-12 d-flex align-items-center h-100">
                        <Formik
                            initialValues={{ name: '', id: '', password: '', password2: '', question: '', answer: '' }}
                            validationSchema={Yup.object({
                                name: Yup.string()
                                    .required('이름을 입력해주세요.'),
                                id: Yup.string()
                                    .required('학번을 입력해주세요.'),
                                password: Yup.string()
                                    .required('비밀번호를 입력해주세요.')
                                    .min(8, '8자 이상 입력해주세요.'),
                                password2: Yup.string()
                                    .required('비밀번호를 다시 입력해주세요.')
                                    .min(8, '8자 이상 입력해주세요.')
                                    .oneOf([Yup.ref("password"), null], '비밀번호가 일치하지 않습니다.'),
                                answer: Yup.string()
                                    .required('답변을 입력해주세요.'),
                            })}
                            onSubmit={(values, { setSubmitting }) => {
                                axios({
                                    method: 'post',
                                    url: '/users',
                                    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="asd">
                                        {/* col-sm-3 */}

                                        <div className="form-group ">
                                            {/*  mb-4 */}
                                            <div className={touched.name && errors.name ? "text-danger" : ""}>이름을 입력하세요</div>
                                            <input
                                                className={(touched.name && errors.name ? 'form-control is-invalid' : "form-control")}
                                                type="text"
                                                name="name"
                                                {...getFieldProps('name')}
                                                placeholder="이름" />
                                        </div>
                                        <div className="form-group">
                                            {/*  mb-4 */}
                                            <div className={touched.id && errors.id ? "text-danger" : ""}>학번을 입력하세요</div>

                                            <input
                                                className={(touched.id && errors.id ? 'form-control is-invalid' : "form-control")}
                                                type="text"
                                                name="id"
                                                {...getFieldProps('id')}
                                                placeholder="학번/교번"
                                            />
                                            {/* {touched.id && errors.id ? (
                                                    <div className="invalid-feedback text-left">{errors.id}</div>
                                                ) : null} */}
                                        </div>
                                        <div className="form-group">
                                            {/*  mb-4 */}
                                            <div className={touched.password && errors.password ? "text-danger" : ""}>비밀번호를 입력하세요(8자리 이상)</div>

                                            <input
                                                className={(touched.password && errors.password ? 'form-control is-invalid' : "form-control")}
                                                type="password"
                                                name="password"
                                                {...getFieldProps('password')}
                                                placeholder="비밀번호"
                                            />
                                            {/* {touched.password && errors.password ? (
                                                    <div className="invalid-feedback text-left">{errors.password}</div>
                                                ) : null} */}
                                        </div>
                                        <div className="form-group">
                                            {/*  mb-4 */}
                                            {touched.password2 && errors.password2 ? setCheckPw(false) : null}
                                            <div id="reCheck" className={checkPw ? "right" : "err"}></div>
                                            <input
                                                className={touched.password2 && errors.password2 ? "form-control is-invalid" : "form-control"}
                                                type="password"
                                                name="password2"
                                                {...getFieldProps('password2')}
                                                placeholder="비밀번호 확인"
                                            />

                                        </div>
                                        <div className="form-group">
                                            {/*  mb-4 */}
                                            <label>본인 확인 질문</label>
                                            <Field as="select" name="question">
                                                <option value="">질문을 선택하세요</option>
                                                <option value="life">자신의 인생 좌우명은?</option>
                                                <option value="school">자신이 다녔던 초등학교의 이름은?</option>
                                                <option value="place">기억에 남는 추억의 장소는?</option>
                                            </Field>
                                        </div>
                                        <div className="form-group">
                                            {/*  mb-4 */}
                                            <div className={touched.answer && errors.answer ? "text-danger" : ""}>답변을 입력해주세요.</div>

                                            <input
                                                className={(touched.answer && errors.answer ? 'form-control is-invalid' : "form-control")}
                                                type="text"
                                                name="answer"
                                                {...getFieldProps('answer')}
                                                placeholder="Input answer" />
                                        </div>
                                        <button type="submit" className="btn btn-dark" disabled={isSubmitting}>회원가입</button>
                                        <button class="btn btn-light"><Link to="/login">로그인</Link></button>
                                        <button class="btn btn-light"><Link to="/"></Link></button>
                                    </form>
                                )}
                        </Formik>
                    </div>
                </Wow>
            </div>
        </div >

Ha YeaJin's avatar
pages    
Ha YeaJin committed
184
185
186
187
188
    );
}


export default Signup;