ChangePage.js 3.23 KB
Newer Older
1
2
3
4
5
6
import React, { useState } from 'react';
import { Formik } from 'formik';
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
function Change(props) {
  const [state, setState] = useState();
  if (state) {
  return <Redirect to="/" />;
  }
  console.log(props)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

  return (
    <div className="d-flex flex-column justify-content-between vh-100">
      <Formik
        initialValues={{ password: '' }}
        validationSchema={Yup.object({
          password: Yup.string()
            .required('비밀번호를 입력해주세요.')
            .min(8, '8자 이상 입력해주세요.'),
          password2: Yup.string()
            .required('비밀번호를 다시 입력해주세요.')
            .min(8, '8자 이상 입력해주세요.')
            .oneOf([Yup.ref("password"), null], '비밀번호가 일치하지 않습니다.'),
        })}
        onSubmit={(values, { setSubmitting }) => {
29
          axios.put(`/users/change/${props.location.state.id}`, { ...values },
30
31
32
33
34
          )
            .then(res => {
              console.log(res.data);
              if (res.status === 404) return alert(res.data.error)
              alert("회원정보가 수정되었습니다!")
35
              setState(true);
36
37
38
39
40
41
42
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
76
77
78
79
80
81
            })
            .catch(err => {
              alert(err.error)
            });


          setTimeout(() => {
            setSubmitting(false);
          }, 400);  // finish the cycle in handler
        }}
      >
        {({
          errors,
          touched,
          handleSubmit,
          getFieldProps,  // contain values, handleChange, handleBlur
          isSubmitting,
        }) => (
            <div className="row justify-content-center align-items-center">
              <form onSubmit={handleSubmit} className="col-sm-3">
                <div className="form-group mb-4">
                  <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">
                  <input
                    className={(touched.password2 && errors.password2 ? 'form-control is-invalid' : "form-control")}
                    type="password"
                    name="password2"
                    {...getFieldProps('password2')}
                    placeholder="새 비밀번호를 다시 입력해주세요."
                  />
                  {touched.password2 && errors.password2 ? (
                    <div className="invalid-feedback text-left">{errors.password2}</div>
                  ) : null}
                </div>
                <button type="submit" className="btn btn-light" disabled={isSubmitting}>저장</button>
              </form>
82
            </div>
83
84
85
86
          )}
      </Formik>
    </div >
  );
87
88
}

89

90
export default Change;