SignupForm.js 5.7 KB
Newer Older
1
2
import { useState } from 'react';
import { Redirect } from "react-router-dom";
3
4
import { Formik } from 'formik';
import * as Yup from 'yup';
Choi Ga Young's avatar
Choi Ga Young committed
5
import authApi from '../../apis/auth.api';
Kim, Subin's avatar
Kim, Subin committed
6
import catchErrors from "../../utils/catchErrors";
Choi Ga Young's avatar
Choi Ga Young committed
7
import styles from "./form.module.scss";
8
9

const SignupForm = () => {
Choi Ga Young's avatar
Choi Ga Young committed
10
  const [success, setSuccess] = useState(false);
11
12
13
14
15
16
  const [error, setError] = useState("");

  if (success) {
    return <Redirect to="/login" />;
  }

17
  return (
Kim, Subin's avatar
Kim, Subin committed
18
19
20
    <>
      <div className="py-5">
        <h1 className="text-center">회원가입</h1>
21
22
23
      </div>
      <Formik
        initialValues={{
24
          userId: '',
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
          password: '',
          repassword: '',
          userName: '',
          userStudNum: ''
        }}
        validationSchema={Yup.object({
          userId: Yup.string()
            .required('아이디를 입력해주세요.')
            .max(15, '15자 이내로 입력해주세요.')
            .min(5, '최소 5자 이상 입력해주세요.'),
          password: Yup.string()
            .required('비밀번호를 입력해주세요.')
            .min(8, '최소 8자 이상 입력해주세요.'),
          repassword: Yup.string()
            .required('비밀번호를 입력해주세요.')
            .min(8, '최소 8자 이상 입력해주세요.')
            .oneOf([Yup.ref("password"), null], '비밀번호가 일치하지 않습니다.'),
          userName: Yup.string()
            .required('이름을 입력해주세요.'),
          userStudNum: Yup.string()
            .required('학번을 입력해주세요.')
            .min(7, '학번을 정확히 입력해주세요.'),
        })}
Choi Ga Young's avatar
Choi Ga Young committed
48
        onSubmit={async (values, { setSubmitting, resetForm }) => {
Choi Ga Young's avatar
Choi Ga Young committed
49
          try {
50
            setError("")
Choi Ga Young's avatar
Choi Ga Young committed
51
52
            const result = await authApi.signup(values)
            // console.log('회원가입 요청 후 result 확인', result, '|', result.status)
53
54
55
56
            if (result.status === 201) {
              alert("회원가입이 완료되었습니다.")
              setSuccess(true)
            }
Choi Ga Young's avatar
Choi Ga Young committed
57
          } catch (error) {
58
            catchErrors(error, setError)
Choi Ga Young's avatar
Choi Ga Young committed
59
            resetForm();
Choi Ga Young's avatar
Choi Ga Young committed
60
          }
61
62
63
64
65
66
          setTimeout(() => {
            setSubmitting(false);
          }, 400);
        }}
      >
        {formik => (
Kim, Subin's avatar
Kim, Subin committed
67
          <form onSubmit={formik.handleSubmit} className="mt-3">
68
69
70
71
            <div className="mb-3 d-flex flex-row">
              <label className="form-label" style={{ width: "100px" }}>아이디</label>
              <div className="flex-col">
                <input type="text" name="userId"
Kim, Subin's avatar
Kim, Subin committed
72
                  className={`form-control shadow-none rounded-0 ${styles.textInput}`} autoComplete="off"
73
74
                  {...formik.getFieldProps('userId')} />
                {formik.touched.userId && formik.errors.userId ? (
75
                  <div className="text-danger mt-1" style={{ fontSize: "10px" }}>{formik.errors.userId}</div>
76
77
78
79
80
81
82
                ) : null}
              </div>
            </div>
            <div className="mb-3 d-flex flex-row">
              <label className="form-label" style={{ width: "100px" }}>비밀번호</label>
              <div className="flex-col">
                <input type="password" name="password"
Choi Ga Young's avatar
Choi Ga Young committed
83
                  className={`form-control shadow-none rounded-0 ${styles.textInput}`}
84
85
                  {...formik.getFieldProps('password')} />
                {formik.touched.password && formik.errors.password ? (
86
                  <div className="text-danger mt-1" style={{ fontSize: "10px" }}>{formik.errors.password}</div>
87
88
                ) : null}
              </div>
89
            </div>
90
91
92
93
            <div className="mb-3 d-flex flex-row">
              <label className="form-label" style={{ width: "100px", wordBreak: "keep-all" }}>비밀번호 확인</label>
              <div className="flex-col">
                <input type="password" name="repassword"
Choi Ga Young's avatar
Choi Ga Young committed
94
                  className={`form-control shadow-none rounded-0 ${styles.textInput}`}
95
96
                  {...formik.getFieldProps('repassword')} />
                {formik.touched.repassword && formik.errors.repassword ? (
97
                  <div className="text-danger mt-1" style={{ fontSize: "10px" }}>{formik.errors.repassword}</div>
98
99
                ) : null}
              </div>
100
            </div>
101
102
103
104
            <div className="mb-3 d-flex flex-row">
              <label className="form-label" style={{ width: "100px" }}>이름</label>
              <div className="flex-col">
                <input type="text" name="userName"
Kim, Subin's avatar
Kim, Subin committed
105
                  className={`form-control shadow-none rounded-0 ${styles.textInput}`} autoComplete="off"
106
107
                  {...formik.getFieldProps('userName')} />
                {formik.touched.userName && formik.errors.userName ? (
108
                  <div className="text-danger mt-1" style={{ fontSize: "10px" }}>{formik.errors.userName}</div>
109
110
                ) : null}
              </div>
111
            </div>
112
113
114
115
            <div className="mb-3 d-flex flex-row">
              <label className="form-label" style={{ width: "100px" }}>학번</label>
              <div className="flex-col">
                <input type="text" name="userStudNum"
Kim, Subin's avatar
Kim, Subin committed
116
                  className={`form-control shadow-none rounded-0 ${styles.textInput}`} autoComplete="off"
117
118
                  {...formik.getFieldProps('userStudNum')} />
                {formik.touched.userStudNum && formik.errors.userStudNum ? (
119
                  <div className="text-danger mt-1" style={{ fontSize: "10px" }}>{formik.errors.userStudNum}</div>
120
121
                ) : null}
              </div>
122
            </div>
123
            <div className="d-grid gap-2 ">
Kim, Subin's avatar
Kim, Subin committed
124
              <button type="submit" className="btn btn-crimson mt-5">확인</button>
125
126
127
128
            </div>
          </form>
        )}
      </Formik>
Kim, Subin's avatar
Kim, Subin committed
129
    </>
130
131
132
  );
}

Kim, Subin's avatar
Kim, Subin committed
133
export default SignupForm;