ChangePage.js 4.12 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import React, { useState } from 'react';
import { Formik } from 'formik';
import * as Yup from 'yup';
import axios from 'axios';
import Menu from '../Components/Menu';
import { Redirect } from 'react-router-dom';
import { Container, Button, Navbar, Col } from 'react-bootstrap';
import styled from 'styled-components';

const Check = styled.div`
  & #reCheck::after {
    content: '새로운 비밀번호를 다시 입력하세요';
  }

Yoon, Daeki's avatar
Yoon, Daeki committed
15
  & #reCheck:not(.right)::after {
Yoon, Daeki's avatar
Yoon, Daeki committed
16
    color: red;
Yoon, Daeki's avatar
Yoon, Daeki committed
17
    content: "비밀번호가 일치하지 않습니다";
Yoon, Daeki's avatar
Yoon, Daeki committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  }
`

function Change(props) {
  const [state, setState] = useState();
  const [checkPw, setCheckPw] = useState(true);

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

  return (
    <div className="">
      {(localStorage.getItem("token") !== null) ? (
Yoon, Daeki's avatar
Yoon, Daeki committed
32
33
34
35
36
37
        <Menu />
      ) : (
          <Menu expand="md" variant="dark">
            <Navbar.Brand>회원가입</Navbar.Brand>
          </Menu>
        )}
Yoon, Daeki's avatar
Yoon, Daeki committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
      <Container fluid className="p-0 vh-90">
        <Check className="row justify-content-center m-0">
          <Col md={4} className="pt-5">
            <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 }) => {
Yoon, Daeki's avatar
Yoon, Daeki committed
53
                axios.put(`/app/rental/api/users/change/${props.location.state.id}`, { ...values },
Yoon, Daeki's avatar
Yoon, Daeki committed
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
                )
                  .then(res => {
                    console.log(res.data);
                    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.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="새로운 비밀번호"
                      />
                    </div>

                    <div className="form-group">
Yoon, Daeki's avatar
Yoon, Daeki committed
90
                      {touched.password2 && errors.password2 ? setCheckPw(false) : setCheckPw(true)}
Yoon, Daeki's avatar
Yoon, Daeki committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
                      <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>
                    <Button type="submit" variant="secondary" disabled={isSubmitting}>저장하기</Button>
                  </form>
                )}
            </Formik>
          </Col>
        </Check>
      </Container>
    </div >
  );
}

export default Change